0

So i want to get input from user into my string and print it:

char *fname ;
getname(fname);
printf("%s", &fname);

void getname(char *fnam e)
{
    fname = (char *)malloc(30);
    gets(fname);
}

So i have error:

uninitialized local variable 'fname'

EDIT:

i only have this code and see whats wrong at this code, not to use different approach.

Dean Movy
  • 139
  • 1
  • 9

3 Answers3

4

C is pass by value. So nothing changes to fname. To make changes you have to pass the address.

getname(&fname);

And then

#define LEN 30
void getname(char **fname)
{
    *fname = malloc(LEN);
    if((*fname) == NULL ){ perror("malloc"); exit(1);}
    if(fgets(*fname,LEN,stdin)==NULL){
       fpritnf(stderr,"Error in input");
       exit(1);
    }
}

And the printf usage in this context would be

printf("%s", fname);

Don't use gets it's deprecated. And free the dynamically allocated memory when done working with it.

in main()

char *fname ;
getname(&fname);
printf("%s", fname);
free(fname);

Also casting malloc is unnecessary because void* to char* conversion is done implicitly.

user2736738
  • 30,591
  • 5
  • 42
  • 56
1

Don't use 'gets', it's deprecated. 'fname' is a pointer to unitialized memory. Use this:

char *fname = malloc(30);
getname(fname);
printf("%s", fname);

void getname(char *fname)
{
    fgets(fname, 30, stdin);
}
Josh Abraham
  • 959
  • 1
  • 8
  • 18
0

You need to have a main() function. C doesn't execute random code that's not inside a function. Try rewriting it to look like this:

void getname(char *fname)
{
    fname = (char *)malloc(30);
    gets(fname);
}
int main(void){
    char* fname;
    getname(fname);
    puts(fname);
}

Ok, now we can analyze it. What happens is that when you pass a pointer (or any variable) to a function, the pointer gets copied. When you do fname = malloc(30);, the fname in the function gets set to the pointer from malloc, but the original fname in main remains the same. So when getname returns to main, fname still is uninitialized.

What you want to do is either allocate fname in main, or return a pointer from getname. So something like this would work:

void getname(char *fname)
{
    gets(fname);
}
int main(void){
    char* fname = malloc(30); // OR
    char fname2[30];
    getname(fname);
    puts(fname);
}

And so would this:

char* getname(void)
{
    fname = (char *)malloc(30);
    gets(fname);
    return fname;
}
int main(void){
    char* fname = getname();
    puts(fname);
}
C_Elegans
  • 1,113
  • 8
  • 15
  • `fname = (char *)malloc(30);` If you are correcting your answer, than please do it proper. Any way you should inform the OP about `gets` and more important is that none of this answers informs the OP about `free` and about cast of `malloc`. – Michi Feb 06 '18 at 18:26
  • The first variant is flat out broken, just like the code in the question. Since you're using `gets()` still, that too is broken — see [Why is `gets()` so dangerous it should never be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Feb 07 '18 at 06:12