Like Tree2Likes
  • 2 Post By ssharish2005

Strings: char* or char vectors ?

This is a discussion on Strings: char* or char vectors ? within the C Programming forums, part of the General Programming Boards category; When I write some lines as Code: int main(int argc, char **argv) { char *string = "Bla0 Bla1 Bla2 Bla3"; ...

  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
    21,717
    The problem is that in the first example, you attempt to modify a string literal, and that is not allowed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,572
    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,290
    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%.

  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,682
    >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
    AndrewHunter and dancp like this.
    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,866
    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, 05: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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21