0
 char *userinput(void){
        char input[22];
        fgets(input,22,stdin);
        char *inputpoint;
        inputpoint=input;
        return inputpoint;
}

This function is supposed to simply recieve a user input string and return it. I call the function in main as follows

        float arrests;
        float money;
        //print menu
        menuprint();
        //Take first User input    
        char *inputpoint;
        inputpoint=userinput();
        char input[22];
        input=inputpoint;
        printf("\nYou entered: %s\n",input);        

return 0;
}

I am relatively new to coding especially pointers. I don't know why this code isn't printing out the string inputted. Please help

user2985363
  • 75
  • 1
  • 4
  • `input` is allocated on `userinput`s stack, so when it is returned (and the stack frame is unwound) you will just get a pointer to garbage. – DJG Jan 29 '14 at 23:15
  • Once `userinput` exits all automatic variables within are no longer addressable. Now consider what that function *returns*, then reread my first sentence. The address returned is indeterminate once the function exists, and as such any usage of it is **undefined behavior**. – WhozCraig Jan 29 '14 at 23:18
  • The pointer returned to your main is _probably_ still within bounds and pointing to _something_ on the stack, but who knows what it's actually pointing to (probably something in printf's stack). We won't even discuss the folly of input=inputpoint... you're trying to overwrite what should be a read-only pointer, and not transferring the string to inputl. – Phil Perry Jan 29 '14 at 23:22
  • relevant: http://c-faq.com/malloc/retaggr2.html – Brave Sir Robin Jan 29 '14 at 23:28

3 Answers3

1

You haven't stated what actual trouble you're having, but in this case it's quite clear. If you had enabled warnings on your compiler, it should have told you that you are returning the address of a local variable. This is undefined behaviour, and you're not allowed to do it.

There are two common approaches here. One is to provide a location by passing it as a function parameter:

void userinput( char input[22] )
{
    fgets(input, 22, stdin);
}

The other is to dynamically allocate. In this case, you could declare your function as it currently is, but do a malloc, or just use strdup:

char *userinput(void)
{
    char input[22];
    fgets(input, 22, stdin);
    return strdup(input);
}

Note that if you use dynamic memory, you need to clean it up when you're finished with it:

char *inputpoint = userinput();
printf("\nYou entered: %s\n", inputpoint); 
free(inputpoint);

Now I notice the other problem:

input = inputpoint;

The above is assigning a pointer to an array. That's not legal, and your compiler should issue an error. Obviously you are wanting to copy the string. You can use strncpy for this:

strncpy( input, inputpoint, 22 );
paddy
  • 60,864
  • 6
  • 61
  • 103
0

I see many errors here.

It appears you have not declared input point. Or is it declared somewhere else?

Also why are you doing your read input like that. Seems way too complex for such a simple task. See this solution below and the discussion beneath it.

void read(char *out){

  scanf("%22s",&out[0]);
  printf("%s\n", out);

}

int main(void) {

  char  chars2[22];
  read(chars2);
  printf("%s\n", chars2);
  return 0
}

A core problem with your solution is that in C functions cant return arrays, unlike many other languages like Java that permit this. So we have to get more creative. What we can do is pass our read method a memory reference to the value we want to update via a parameter.

The & (address of) operator allows the scanf to access the memory location of the first array element so it can fill the array with what ever is input. The maximum length of the input is 22 characters to match the char array. So if more are input is ignores anything after 22.

Andrew S
  • 2,847
  • 3
  • 33
  • 50
0

The basic problem is that you're returning a pointer to a local variable. When userinput returns, its stack frame is released to be reused by the next function called (printf), but the pointer still points at it, so you print garbage or crash.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226