Thread: C Strings and * Operator

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    20

    Red face C Strings and * Operator

    How come you do not have to use the dereference operator when printing the string my_string in the following code?

    Code:
    char *my_string = malloc(sizeof(*my_string)*100);
    
    printf("Enter a string.\n");
    scanf("%s", my_string);
    printf("%s", my_string);
    I am learning C and do not quite understand this. I appreciate the help.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Because the printf modifier %s expects a char*, not a plain char. And my_string in your example is a char*.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    OK I see. Thanks a lot.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The reason it expects char* is because char can only hold one character. So, in C, a string is a pointer to where the first character in an array resides. It then "travels" the memory, reading one byte after another until it reaches a '\0' character. That is why it requires a char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    Good explanation. Thanks for the help guys.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    yep, same reason why scanf("%s", my_string); doesn't require a reference operator (&) when you pass a string to it, because array identifiers are really pointers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's called the address of operator.
    References doesn't exist in C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM
  3. oper overloading || Distances vs. Strings
    By xion in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2004, 05:23 PM
  4. help writing function definitions
    By jlmac2001 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2003, 09:44 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

Tags for this Thread