0

I read a few similar questions, C: differences between char pointer and array, What is the difference between char s[] and char *s?, What is the difference between char array[] and char *array? but none of them seem to clear my doubt.

I'm aware that

char *s = "Hello world";

makes the string immutable whereas

char s[] = "Hello world";

can be modified.

My doubt is if I do char stringA[LEN]; and char* stringB[LEN]; Are they any different? Or does stringB again becomes immutable as in the case before?

Community
  • 1
  • 1
Vivek Vijayan
  • 337
  • 5
  • 19
  • The difference is that an array is an array and a pointer is a pointer. A pointer is not an array, and an array is not a pointer. – Lundin Apr 07 '16 at 11:05

5 Answers5

6

Let me give you a visual explanation:

As you can see, a is an array of 4 characters, whereas b is an array of 4 character pointers, each pointing to the beginning of a C string. Each one of those strings can have a different length.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
3

Are they any different?

Yes.
Both variables stringA and stringB are arrays. stringA is an array of char of size LEN and stringB is an array of char * of size LEN.

char and char * are two different types. stringA can hold only one character string of length LEN while elements of stingB can point to LEN number of strings.

Or does stringB again becomes immutable as in the case before?

Whether strings pointed by elements of stringB is mutable or not will depend on how memory is allocated. If they are initialized with string literals

char* stringB[LEN] = { "Apple", "Bapple", "Capple"};  

then they are immutable. In case of

for(int i = 0; i < LEN; i++)
    stringB[i] = malloc(30)  // Allocating 30 bytes for each element  

strcpy(stringB[0], "Apple");
strcpy(stringB[1], "Bapple");
strcpy(stringB[2], "Capple");  

they are mutable.

haccks
  • 104,019
  • 25
  • 176
  • 264
3

They are not the same.

Here stringA is an array of char, that means printing stringA[0] will show the letter S:

char stringA[] = "Something";

Whereas printing stringB[0] here will show Something (array of pointer to char):

char* stringB[] = { "Something", "Else" };
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
2

They are not having the same datatype, less being comparable.

  • char stringA[LEN]; is a char array of LEN length. (Array of chars)

  • char* stringB[LEN]; is a char * array of LEN length. (Array of char pointers)

FWIW, in case of char *s = "Hello world"; s is a pointer which points to a string literal which is non-modifiable. The pointer itself can certainly be changed. Only the content it points to (values) cannot be changed.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

The reason


char* s = "Hello World!";

is immutable is because "Hello World!" is stored in RO(Read Only) memory, sow hen you try to change it, it throws an error. Declaring it as a pointer to the first element of an "array" is NOT the reason it's immutable. You seem to be slightly confused as well.

Assuming your questions is exactly what you mean,


    char stringA[LEN] = "ABC";

is a string in traditional C style, but stringB as you've defined isn't a string, it's an array of strings -


    char* stringB[LEN] = {"ABC", "DEF"};

Assuming you mean what I think you mean,


    char stringA[LEN] = "Hello World!";
    char *stringB = malloc(LEN);
    strcpy(stringB, stringA);

In this case, stringB IS mutable, since it refers to writable memory.

Shahe Ansar
  • 314
  • 2
  • 12