0

what is the correct way to define a string in C? using:

char string[10];

or

char *string="???";

If I use an array, I can use any pointer to point to it and then manipulate it. It seems like using the second one will cause trouble because we didn't allocate memory for that. I am taught that array is just a pointer value, I thought these two are the same before. Until I did something like string* = *XXXX, and realize it didn't work like a pointer.

Mat
  • 202,337
  • 40
  • 393
  • 406
qwr qwr
  • 1,049
  • 4
  • 22
  • 46
  • 4
    Arrays are not pointers and pointers are not arrays. You might like section 6 of the [comp.lang.c FAQ](http://c-faq.com/). – pmg Apr 01 '12 at 14:27
  • 2
    "???" is a literal and is allocated in static storage. And an array behaves like a pointer (and vice-versa) in many circumstances, but there are several differences that will bite you when you let your guard down. – Hot Licks Apr 01 '12 at 14:27
  • 2
    possible duplicate of [How to declare strings in C](http://stackoverflow.com/questions/8732325/how-to-declare-strings-in-c) – Oliver Charlesworth Apr 01 '12 at 14:27
  • Which is itself a duplicate of http://stackoverflow.com/questions/4680431/memory-allocation-char-and-char – blueshift Apr 01 '12 at 14:32
  • And that is a duplicate of http://stackoverflow.com/questions/1449480/what-is-the-difference-between-the-ways-to-create-a-string-in-c – Mr Lister Apr 01 '12 at 14:53
  • *"I am taught that array is just a pointer value ..."* You have been lied to. – Keith Thompson Apr 01 '12 at 15:41
  • possible duplicate of [What is the difference between char a\[\] = "string" and char *p = "string"?](http://stackoverflow.com/questions/9460260/what-is-the-difference-between-char-a-string-and-char-p-string) – Alok Save Apr 01 '12 at 15:52

2 Answers2

1

As @affenlehrer points out, how you "define" a string depends on how you want to use it. In reality, 'defining' a string in C really just amounts to putting it in quotes somewhere in your program. You should probably read more about how memory works and is allocated in C, but if you write:

char *ptr = "???"

What happens is that the compiler will take the string "???" (which is really four bytes of data, three '?'s followed by one zero byte for the NUL terminator). It will insert that at some static place in your program (in something called the .bss segment), and when your program starts running, the value of ptr will be initialized to point to that location in memory. This means you have a pointer to four bytes of memory, and if you try to write outside of those bytes, your program is doing something bad (and probably violating memory safety).

On the other hand, if you write

char string[10];

Then this basically tells the compiler to go allocate some space in your program of 10 bytes, and make the variable 'string' point to it. It depends where you put this: if you put it inside a function, then you will have a stack allocated buffer of 10 bytes. If you manipulate this buffer inside a function, and then don't do anything with the pointer afterwards, you're all fine. However, if you pass back the address of string -- or use the pointer in any way -- after the function returns, you're in the wrong. This is because, after the function returns, you lose all of the stack allocated variables.

Kristopher Micinski
  • 7,572
  • 3
  • 29
  • 34
0

There are even more ways to create strings in C (e.g. using malloc). What is your usecase? Basically you need a place in memory where the data is stored (on the stack, on the heap, static as in your second example) and then a character pointer to the first character of your string. Most string related functions will "see" the end of the string by the trailing '\0', in some other cases (mostly general purpose data related functions) you also have to provide the length of the string.

katzenversteher
  • 810
  • 6
  • 13