Thread: scanf and string pointer

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    37

    scanf and string pointer

    Dear all:

    Following is the program I wrote:

    Code:
    #include <stdio.h>
    
    int main () {
    
            char *string;
    
            printf("Enter something: ");
            scanf("%s", string);
    
            printf("The string you entered is %s\n", string);
    
            return 0;
    }
    But the output does not print the string I entered:


    Enter something: string
    The string you entered is (null)


    If I change "char *string" to "char string[10]", it works fine, can anyone help me, thanks in advance

  2. #2
    Registered User
    Join Date
    May 2011
    Posts
    37
    I realised that I haven't initialised the string pointer, if I do the following, it works.

    Code:
    #include <stdio.h>
    
    int main () {
    
            char string[10];
            char *strptr=string;
    
            printf("Enter something: ");
            scanf("%s", strptr);
    
            printf("The string you entered is %s\n", string);
    
            return 0;
    }
    Quote Originally Posted by barramundi9 View Post
    Dear all:

    Following is the program I wrote:

    Code:
    #include <stdio.h>
    
    int main () {
    
            char *string;
    
            printf("Enter something: ");
            scanf("%s", string);
    
            printf("The string you entered is %s\n", string);
    
            return 0;
    }
    But the output does not print the string I entered:


    Enter something: string
    The string you entered is (null)


    If I change "char *string" to "char string[10]", it works fine, can anyone help me, thanks in advance
    Last edited by barramundi9; 05-31-2011 at 03:45 AM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's because char*string does not allocate memory space for your string... it's merely a pointer to nothing.

    C has no string type and is only marginally aware of text in programs. C-Strings are actually nothing but arrays of char sized integers. The string library functions (incl. scanf() and printf() ) merely treat them as strings by writing a 0 at the end of the used space in the char array.

    If you want to use char *string ... you also have to call malloc() to set aside space for your char array and then free() it when you're done with it.

    EDIT: I see from your second post you found an interesting solution... but in that case why not write to the char array directly? You can simply use scanf("%s", string); or use scanf("%9s",string) if you're (appropriately) worried about buffer overflows. An array's name is simply a pointer to it's first element, so you can work it directly with any of the library functions.
    Last edited by CommonTater; 05-31-2011 at 03:51 AM.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    37
    Hmm ...

    I just tried the same codes on my windows machine using Cygwin + Mintty and it worked!!

    Really weird, any idea as to why it didn't work on my linux machine but it worked on my Cygwin on windows???

    Thanks

    Quote Originally Posted by CommonTater View Post
    That's because char*string does not allocate memory space for your string... it's merely a pointer to nothing.

    C has no string type and is only marginally aware of text in programs. C-Strings are actually nothing but arrays of char sized integers. The string library functions (incl. scanf() and printf() ) merely treat them as strings by writing a 0 at the end of the used space in the char array.

    If you want to use char *string ... you also have to call malloc() to set aside space for your char array and then free() it when you're done with it.

    EDIT: I see from your second post you found an interesting solution... but in that case why not write to the char array directly? You can simply use scanf("%s", string); or use scanf("%9s",string) if you're (appropriately) worried about buffer overflows. An array's name is simply a pointer to it's first element, so you can work it directly with any of the library functions.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What doesn't work? what happened?
    Be specific.
    You can only input 9 chars. if using #2 code

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    37
    Sorry, I was referring #1 code

    Code:
    #include <stdio.h>
    
    int main () {
    
            char *string;
    
            printf("Enter something: ");
            scanf("%s", string);
    
            printf("The string you entered is %s\n", string);
    
            return 0;
    }
    On my Linux server, the output was :

    Enter something: string
    The string you entered is (null)


    But it worked (printed the string) on my windows pc with Cygwin+Mintty, maybe it has something to do with the compiler?


    Quote Originally Posted by Bayint Naung View Post
    What doesn't work? what happened?
    Be specific.
    You can only input 9 chars. if using #2 code

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Undefined behavior. don/t play with it.
    You aren't going to learn anything

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by barramundi9 View Post
    Sorry, I was referring #1 code
    But it worked (printed the string) on my windows pc with Cygwin+Mintty, maybe it has something to do with the compiler?
    No, it didn't work... Windows just didn't error off on it (and it should have).

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    37
    Yes, it did because the string was printed, that was why I thought it was weird but I thought what Bayint Naung said was right, not worth checking out ...


    Quote Originally Posted by CommonTater View Post
    No, it didn't work... Windows just didn't error off on it (and it should have).

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by barramundi9 View Post
    Yes, it did because the string was printed
    Today's C lesson is: Correct output does not make a correct program.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by barramundi9 View Post
    Yes, it did because the string was printed, that was why I thought it was weird but I thought what Bayint Naung said was right, not worth checking out ...
    And when it turns out that uninitialized pointer is aimed directly at your program's stack, overwriting crucual data from other parts of the program? What then?

    In a trivial example like yours it is likely you will get away with using uninitialized memory --that is, memory you don't own-- without any noticeable consequences... try that in a larger project, oh, lets say your company's inventory tracking, and watch what happens : "Hey Gerald, why does it say we have 430,006 of those chrome widgets?"

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    37
    Er ... what I mean is that I'll give up on researching this technical glitch and forever initialise my pointers from now on ...


    Quote Originally Posted by CommonTater View Post
    And when it turns out that uninitialized pointer is aimed directly at your program's stack, overwriting crucual data from other parts of the program? What then?

    In a trivial example like yours it is likely you will get away with using uninitialized memory --that is, memory you don't own-- without any noticeable consequences... try that in a larger project, oh, lets say your company's inventory tracking, and watch what happens : "Hey Gerald, why does it say we have 430,006 of those chrome widgets?"

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Actually your compiler will give warning. if you try to use un-initialize variables.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    Actually your compiler will give warning. if you try to use un-initialize variables.
    Don't you mean "compiler should give warning" ... whether it does or not depends on his compiler and the warning levels he's chosen...

    You'd be surprised how many people simply turn off the warnings instead of fixing their code.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by barramundi9 View Post
    Er ... what I mean is that I'll give up on researching this technical glitch and forever initialise my pointers from now on ...
    It's not a technical glitch... it's a difference between OSs ... and it's a part of the C language that you must somehow initialize all variables before using them... It won't do it for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer, struct, scanf
    By Alex1357 in forum C Programming
    Replies: 7
    Last Post: 05-06-2011, 09:17 AM
  2. Replies: 5
    Last Post: 03-23-2009, 03:44 PM
  3. scanf and pointer
    By BlackSlash12 in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 03:33 PM
  4. char pointer working in scanf but not in cin.
    By sawer in forum C++ Programming
    Replies: 14
    Last Post: 06-15-2006, 02:15 AM
  5. scanf to struct pointer
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 12-20-2004, 10:22 AM