Thread: getting a char from a string

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    27

    getting a char from a string

    I know I can use pointers to get every character from a particular string such as

    Code:
    char * string = "hello";
    char * i;
    i = string;
    for(;*i != '\0'; i++)
    {
      printf("%c",*i);
    }
    but I was wondering if there was another way to do this

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Albinoswordfish View Post
    I know I can use pointers to get every character from a particular string such as

    Code:
    char * string = "hello";
    char * i;
    i = string;
    for(;*i != '\0'; i++)
    {
      printf("%c",*i);
    }
    but I was wondering if there was another way to do this
    What's wrong with the pointer way? As long as we don't know that, we can't know in what way it should be 'better', can we? Because it won't be better, it will just be more suitable in certain situations. For instance, you could use "string[3]" to get the 4th character...

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And why do this:
    Code:
    i = string;
    for(;*i != '\0'; i++)
    when the obvious loop would be:
    Code:
    for(i = string;*i != '\0'; i++)
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    27
    I was just wondering if there was a standard function I could use

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Albinoswordfish View Post
    I was just wondering if there was a standard function I could use
    Why would you want a function for that? Pointers are extremely fast, function calls waaaaaaaay slower.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    27
    well lets say your given a string that is not null terminated would it still be possible with pointer arithmetic

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Albinoswordfish
    well lets say your given a string that is not null terminated would it still be possible with pointer arithmetic
    It would not be a string any more, but if you know the length of the char array, you can of course use pointer arithmetic safely.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    It would not be a string any more, but if you know the length of the char array, you can of course use pointer arithmetic safely.
    Why would it make it any less of a string? I am pretty sure it would still be a string, just not a 0-terminated string anymore.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    12

    use char[] instead of *char

    Why do you want to use *char = "string" ??

    This way as you said, you are creating a read only string. So leave apart pointer arthimetic and changing '\0' character, you won't be able to change byte...

    better use char[] = "string"

    and then you don't need to worry about anything ...

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vkaushal21 View Post
    Why do you want to use *char = "string" ??

    This way as you said, you are creating a read only string. So leave apart pointer arthimetic and changing '\0' character, you won't be able to change byte...

    better use char[] = "string"

    and then you don't need to worry about anything ...
    That is subjective, and depends on the needs of the code. One is better sometimes (e.g. char [] is better when you need to modify the string), the other is better at other times. Although to be strict, it probably should be const char * = "string";

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by vkaushal21 View Post
    Why do you want to use *char = "string" ??

    This way as you said, you are creating a read only string. So leave apart pointer arthimetic and changing '\0' character, you won't be able to change byte...

    better use char[] = "string"

    and then you don't need to worry about anything ...
    He's only preforming read-only options. Then it would be really bad practice to use char something[], imho.

    He should really use "const char" in stead of char.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EVOEx
    Why would it make it any less of a string? I am pretty sure it would still be a string, just not a 0-terminated string anymore.
    Because by definition, in C, "a string is a contiguous sequence of characters terminated by and including the first null character".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    Because by definition, in C, "a string is a contiguous sequence of characters terminated by and including the first null character".
    Hmmm ok. Then I can somewhat agree to what you said . But you can still call it a string as well if you want. Wikipedia: "a string is an ordered sequence of symbols". And my lecturers taught me the same thing.
    So, while it may not be a string according to the C standard (so not a C String), it is still a string according to many other definitions.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EVOEx
    So, while it may not be a string according to the C standard (so not a C String), it is still a string according to many other definitions.
    Of course, but if you do not use the standard definition, then you are running the risk of miscommunication. For example, the definition that you quoted allows us to call arrays of non-character integers strings, and indeed they are strings of integers. We could even zero terminate them and thus talk about "0-terminated strings".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Quote Originally Posted by EVOEx View Post
    He's only preforming read-only options. Then it would be really bad practice to use char something[], imho.

    He should really use "const char" in stead of char.
    yes, I agree with you two... const char * is a better option...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM