0
    void add()
    {    
    char name[50], surname[50], usern[50];

    int id, birth, amount;
    printf("Enter your name, surname and your year of birth:\n");
    scanf("%s %s %d", &name, &surname, &birth);
    printf("Enter your ID, username and your total amount:\n");
    scanf("%d %s %d", &id, &usern, &amount);
    const char *pass1=function(usern);  
    }

  const char *function (char usern[50])
  {
    char temp;
    int i=0;
    int j;
    j = strlen(usern) - 1;
    while (i < j) 
    {
      temp = usern[i];
      usern[i] = usern[j];
      usern[j] = temp;
      i++;
      j--;
    }
    return usern;   
  }

I call 'add' from 'main' to print those things and then I call 'function' to return me the usern string but something goes wrong.

I get the error when compiling:

[Warning] initialization makes pointer from integer without a cast--> const char *pass1=function(usern);
[Error] conflicting types for 'function'--> const char *function (char usern[50])

6
  • What is going wrong? What are you doing with const char *pass1? What is the expected output and what output are you getting? A broad statement like "something goes wrong" will likely get you no help. More details about the problem on the other hand will definitely help you nail the problem. :) Commented Dec 29, 2015 at 11:55
  • hello, the warnings are: [Warning] initialization makes pointer from integer without a cast--> const char *pass1=function(usern); and [Error] conflicting types for 'function'--> const char *function (char usern[50]) Commented Dec 29, 2015 at 11:57
  • i want to return the string "usern" in "pass1" by calling this function ('function') Commented Dec 29, 2015 at 12:00
  • The error is because you are trying to use function() before defining it. You can define the function before you use it, or add a function protoype before you use it: const char* function(char usern[50]); Also note @Bathsheba 's answer about possible undefined behaviour. Commented Dec 29, 2015 at 12:05
  • thank you @Sumit Chakraborty, I forgot the "const char *" in front of the definition. Now i use " printf ("%s", pass1); " in add and it prints what i wanted :) Commented Dec 29, 2015 at 12:16

2 Answers 2

5

The error messages you see are due the result of not declaring the function function before using it. So the compiler implicitly declares a prototype with int as the default type for function. But the actual return type of function conflicts with the implicit int type. So you get those errors.

Note that this implicit int rule is no longer valid as it's been removed since C99. This used to be the case in C89/C90.

The solution is to provide a prototype for it. Add this at the top of your source file (or include it in a header file if you have one).:

const char *function (char *);
Sign up to request clarification or add additional context in comments.

6 Comments

yeah, i declared the funtion as you say and it works but without * before variable const char* function(char usern[50]);
@sniperalex117 const char *function(char *); is equivalent to const char* function(char usern[50]);... What you need to understand is that usern is not an array in the context of function; you can't pass an array to function. You can, however, mimic passing an array by passing a pointer.
@sniperalex117 const char* function(char usern[50]);, const char* function(char *usern); and const char* function(char *); are equivalent in the function prototype. The parameter name is optional in prototypes. An array decays into a pointer to its first element when passed to a function. So the fact you specify the size in the defintion as const char* function(char usern[50]){ ..} is irrelevant and will be ignored by the compiler. So usern is actually a pointer in function.
Also, if an answer solves your problem you should accept it.
ohh ok then, but how is usern a pointer? pass1 is declared as a pointer.
|
0

The lifetime for which the return of function is valid is the same as the lifetime of the parameter usern that's passed to it.

This is because no deep copy of the character array is taken.

If you attempt to return pass1 from add and use it, then the program behaviour would be undefined since usern would then be out of scope.

The normal thing to do here in C is to malloc a new string, returning a pointer to it. Don't forget to call free at some point.

2 Comments

Actually, the argument that usern denotes is a pointer, not an array, and that pointer points to the object that is declared in add. You can prove this by checking sizeof usern.
Also, it's not at all normal to malloc a new string. For a start, strings are not types of objects, but instead types of values, and malloc allocates objects, not values. More importantly, consider every standard library function in C: Does fgets, for example, malloc anything? No, yet it produces a string, doesn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.