Thread: Strings: char* or char vectors ?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Rome, Italy
    Posts
    8

    Strings: char* or char vectors ?

    When I write some lines as

    Code:
    int main(int argc, char **argv) {
    	char *string = "Bla0 Bla1 Bla2 Bla3";
    	char *temp;
    	temp = strtok(string, " ");
    }
    I've an invalid address error on the strtok call.

    But if I write

    Code:
    int main(int argc, char **argv) {
    	char string[128] = "Bla0 Bla1 Bla2 Bla3";
    	char *temp;
    	temp = strtok(string, " ");
    }
    the program runs correctly.

    What's the problem? Can a string be indifferently represented either by a char pointer either by a char vector?

    (I'm sorry for the English ... )

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that in the first example, you attempt to modify a string literal, and that is not allowed.
    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

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    char *string = "Bla0 Bla1 Bla2 Bla3";
    Is a pointer to literal constant.

    strtok modifies the string passed to it; if it is a constant it can not modify it.
    Note: Some compilers will allow a literal constant to be modified; but, I think the standard changed and the newer compilers do NOT allow it or give a warning if it is allowed.

    Tim S.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    Note: Some compilers will allow a literal constant to be modified; but, I think the standard changed and the newer compilers do NOT allow it or give a warning if it is allowed.
    Modifying a literal gives undefined behaviour in every C standard since 1989.

    Some compilers (whether predating ISO standards or not) do allow modification of string literals. Technically, that is an acceptable outcome, given that the result is actually undefined ("undefined" in the standard means "anything goes").
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Location
    Rome, Italy
    Posts
    8
    Thank you for the replies!

    I thought it was a memory access problem caused by the pointer (in the first case).

    In the second case, I put the string into a chars vector. Isn't a literal, in this case?

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >In the second case, I put the string into a chars vector. Isn't a literal, in this case?
    In your first case, you use pointer to point to literal string which is nothing but constant saved in Read-only section of data segment in the process memory. To which you can only read but cannot alter or modify the original string.

    Where as in the second case, the literal string will be stored in the Read-Write section of data segment, as you declare string buffer to which you copy the data to. Since it lives in the read-write location of the process memory, you can call strtok which allows modifying the string.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    I am kinda new to this too; but, in the first case, the pointer only points to the first letter in that array. The memory for the entire array is seen by the pointer, it knows its there, but it just points to the first element of the array. In the second case, its not a pointer, so the entire array is "seen" by strtok, not just the first letter.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Linux Trojan
    In the second case, its not a pointer, so the entire array is "seen" by strtok, not just the first letter.
    Actually, when the array is passed as an argument to strtok, it is converted to a pointer to its first element, so there is no real difference in this sense.
    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

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Linux Trojan View Post
    I am kinda new to this too; but, in the first case, the pointer only points to the first letter in that array. The memory for the entire array is seen by the pointer, it knows its there, but it just points to the first element of the array. In the second case, its not a pointer, so the entire array is "seen" by strtok, not just the first letter.
    Are you not reading the replies before making these comments? You are wrong on all accounts. In both cases it is evaluated as a pointer by the compiler. The problem, as was stated multiple times above is that you cannot modify a string literal. May I suggest your review CProgramming.com's C tutorials.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Linux Trojan View Post
    I am kinda new to this too; but, in the first case, the pointer only points to the first letter in that array. The memory for the entire array is seen by the pointer, it knows its there, but it just points to the first element of the array. In the second case, its not a pointer, so the entire array is "seen" by strtok, not just the first letter.
    Nope... in the first case his char* is actually a pointer to read-only memory in the PE image. He's not allowed to modify it and strtok() inserts nulls, modifying the string.

    In the second case his char[] is an array of characters in read-write memory on the stack. Now he can modify it.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    Tater: this is very helpful info, ty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings and char[]
    By grantmitchell in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2008, 06:28 PM
  2. strings and char's
    By mikecompsc in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2007, 01:26 AM
  3. Help with char strings
    By Probose in forum C++ Programming
    Replies: 10
    Last Post: 09-23-2006, 03:38 AM
  4. Saving text into char* vectors
    By Niara in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2005, 03:50 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM