5

I have a misunderstanding with respect to the following concepts in C.

To define a string you can do one of the following AFAIK :

define an array of chars like this one here :

char array[];

Or define a string to a char array. Let us say like this one :

char * str,

Why do I see some books using the pointers method while others use the normal definitions ?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
saltosian
  • 95
  • 1
  • 1
  • 3
  • 9
    `char * str` is a pointer to `char` type, not "string to char array". – t0mm13b May 09 '16 at 10:04
  • 1
    @sameerasy that's not completely correct, `char* str` could also point to a `char array[]`, so it depends where it points to. – alain May 09 '16 at 10:08
  • 1
    @sameerasy not really. What is i `malloc()` and `strcpy()`, or `strdup()`? – Sourav Ghosh May 09 '16 at 10:08
  • @sameerasy that is not correct, a pointer to `char` can have their reference addresses changed so it is mutable. Immutable can be applied if it is a `const`. – t0mm13b May 09 '16 at 10:09
  • You have to define, at least, a size for your array. `char array[];` have to be be `char array[size];` or `char array[] = "test";` or `char array[] = {'t', 'e', 's', 't', '\0'};` – LPs May 09 '16 at 10:11
  • 1
    I think people are downvoting because the question seem to be very broad and inaccurate.. it would be better if you post some real situation and how you use chars in that .. – nayana May 09 '16 at 10:14
  • @PaulRooney Not at all. You could write `void test (char data[])` where data is a pointer at the end. – LPs May 09 '16 at 10:30
  • This, was one of my answers which should help guide the differences. [C Strings Confusion](http://stackoverflow.com/questions/2124935/c-strings-confusion/2125429#2125429) – t0mm13b May 09 '16 at 10:32
  • For the record, "put on hold as unclear what you're asking" is itself rather unclear, if not downright misleading. It was clear enough that the OP does not have a good understanding of the distinction between `char []` and `char *`, and that's what they were asking about. The question was put on hold because people didn't think it was SO's job to teach the OP that. – Steve Summit May 09 '16 at 14:42

2 Answers2

15

A short answer won't do your question justice. You're asking about two of the most significant, but often misunderstood, aspects of C.

First of all, in C, by definition, a string is an array of characters, terminated with a nul character, '\0'.

So if you say

char string1[] = "Hello";

it's as if you had said

char string1[] = {'H', 'e', 'l', 'l', 'o', '\0'};

When you use a string constant like "Hello" in your program, the compiler automatically constructs the array for you, as a convenience.

Next we have the character pointer type, char *. You called this a "string to a char array", but it's really a pointer to a single char. People often refer to char * as being C's "string type", and in a way it is, but it can be a misleading thing to say, because not every char * is a string, and more importantly C has very few mechanisms for automatically managing strings for you. (What I mean is that, in C, you can't just sling strings around as if they were a basic data type, like you can in C++ or BASIC. You typically have to worry about where they're stored, how they're allocated.)

But let's go back to the definition of a string. If a string is an array of characters, then why would a pointer to characters be useful for manipulating strings at all? And the answer is, pointers are always useful for manipulating arrays in C; pointers are so good at manipulating arrays that sometimes it seems as if they are arrays. (But don't worry, they're not.)

I can't delve into a full treatise on the relationship between arrays and pointers in C here, but a couple of points must be made:

If you say something like

char *string2 = "world";

what actually happens (what the compiler does for you automatically) is as if you had written

static char __temporaryarray[] = "world";
char *string2 = __temporaryarray;

And, actually, since string2 is a pointer to a character (that is, what it points at is characters, not whole arrays of characters), it's really:

char *string2 = &__temporaryarray[0];

(That is, the pointer actually points to the array's first element.)

So since a string is always an array, when you mentioned the string "world" in a context where you weren't using it to initialize an array, C went ahead and created the array for you, and then made the pointer point to it. C does this whenever you have a string constant lying around in your program. So you can later say

string2 = "sailor";

and you can also say things like

if(strcmp(string1, "Goodbye") == 0) { ... }

Just don't do

char *string3;

strcpy(string3, "Rutabaga");        /* WRONG!! *//

That's very wrong, and won't work reliably at all, because nobody allocates any memory for string3 to point to, for strcpy to copy the string into.


So to answer your question, you will sometimes see strings created or manipulated as if they are arrays, because that's what they actually are. But you will often see them manipulated using pointers (char *), because this can be extremely convenient, as long as you know what you are doing.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
2

Why do I see some books using the pointers method while others use the normal definitions ?

Neither of those is more "normal" than the other. A string in C is purely a contiguous (uninterrupted) series of char terminated with a 0 char. You can refer to that with a char[], or you can refer to it with a char *, or a const char *, etc. They're all just ways of referring to the block of memory with the series of chars in it. Each is appropriate in different contexts, depending on how the code in question receives the string and what it's doing with it.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875