Thread: question

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    14

    question

    what are two differences between these two statements.
    be easy im a new learner

    char string[]="This is a string";
    char *string = "This is a string";

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Gahhhhhhhhhhhhh. Stop with the posts already just put all of these in one thread.
    One is a char array one is a pointer. The array one can be modified and the pointer one cannot.
    Woop?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Sounds suspiciously like a homework question into which you have put little effort. But in any case...

    Statement one is assigning that value to an array of characters.
    Statement two is assigning the address at which that constant is stored in memory to a pointer.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    http://www.eskimo.com/~scs/C-faq/q6.2.html

    Suggest you read the whole FAQ before asking any further questions, unless you want to continue getting flamed.

  5. #5
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    but as i remember, in old DOS compiler (turbo C 2.01) *char also can be modified!

  6. #6
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >but as i remember, in old DOS compiler (turbo C 2.01) *char also can be modified!
    On some compilers it can and others it can't. The standard says it shouldn't be modified if you want portability, so modify your string literals at your own risk.

    Cheers!

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    1
    a short answer:

    A string is a "array of characters".

    here string[] and *string are nothing but the name given to memory location where your
    string is stored.

    1) In string[] you use the array notation to manipulate string and
    2) In *string you use the pointer notation for manipulating string

    actually,
    both string[0] and *string are same.

    i would suggest you to read some material for "relationship between array and
    pointers".

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not exactly. It's already been explained.
    Code:
    char foo[] = "This is a string. It is also an array.";
    The above is an array, whose size is the length of the string of characters, plus one for the null character. You can modify its contents, so long as you stay within the provided length.
    Code:
    char foo[200] =  "This is a string. It is also an array."
    This is the same as the previous, however its size is 200 characters in length. Its contents also can be modified.
    Code:
    char *foo = "This is a string of characters. It is not an array.";
    foo is a pointer to a character. It currently points to a string literal. The literal itself is not to be modified. You can however make this pointer point some place else.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM