Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char *array and char array[]

Tags:

if I write this

 char *array = "One good thing about music"; 

I actually create an array? I mean it's the same like this?

char array[] = {"One", "good", "thing", "about", "music"}; 
like image 412
yaylitzis Avatar asked Dec 03 '13 09:12

yaylitzis


People also ask

Are char * and char [] the same?

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test" , while the pointer simply refers to the contents of the string (which in this case is immutable).

What is the difference between char * array and char array []?

What is the difference between char and char*? char[] is a character array whereas char* is a pointer reference. char[] is a specific section of memory in which we can do things like indexing, whereas char* is the pointer that points to the memory location.

What is a char * array?

A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store a short piece of text as a row of characters in a character vector.

What's difference between char's [] and char * s in C?

Difference between char s[] and char *s in CThe s[] is an array, but *s is a pointer. For an example, if two declarations are like char s[20], and char *s respectively, then by using sizeof() we will get 20, and 4.


2 Answers

The declaration and initialization

char *array = "One good thing about music"; 

declares a pointer array and make it point to a constant array of 31 characters.

The declaration and initialization

char array[] = "One, good, thing, about, music"; 

declares an array of characters, containing 31 characters.

And yes, the size of the arrays is 31, as it includes the terminating '\0' character.


Laid out in memory, it will be something like this for the first:

 +-------+     +------------------------------+ | array | --> | "One good thing about music" | +-------+     +------------------------------+ 

And like this for the second:

 +------------------------------+ | "One good thing about music" | +------------------------------+ 

Arrays decays to pointers to the first element of an array. If you have an array like

char array[] = "One, good, thing, about, music"; 

then using plain array when a pointer is expected, it's the same as &array[0].

That mean that when you, for example, pass an array as an argument to a function it will be passed as a pointer.

Pointers and arrays are almost interchangeable. You can not, for example, use sizeof(pointer) because that returns the size of the actual pointer and not what it points to. Also when you do e.g. &pointer you get the address of the pointer, but &array returns a pointer to the array. It should be noted that &array is very different from array (or its equivalent &array[0]). While both &array and &array[0] point to the same location, the types are different. Using the array above, &array is of type char (*)[31], while &array[0] is of type char *.


For more fun: As many knows, it's possible to use array indexing when accessing a pointer. But because arrays decays to pointers it's possible to use some pointer arithmetic with arrays.

For example:

char array[] = "Foobar";  /* Declare an array of 7 characters */ 

With the above, you can access the fourth element (the 'b' character) using either

array[3] 

or

*(array + 3) 

And because addition is commutative, the last can also be expressed as

*(3 + array) 

which leads to the fun syntax

3[array] 
like image 200
Some programmer dude Avatar answered Sep 29 '22 12:09

Some programmer dude


No, you're creating an array, but there's a big difference:

char *string = "Some CONSTANT string"; printf("%c\n", string[1]);//prints o string[1] = 'v';//INVALID!! 

The array is created in a read only part of memory, so you can't edit the value through the pointer, whereas:

char string[] = "Some string"; 

creates the same, read only, constant string, and copies it to the stack array. That's why:

string[1] = 'v'; 

Is valid in the latter case.
If you write:

char string[] = {"some", " string"}; 

the compiler should complain, because you're constructing an array of char arrays (or char pointers), and assigning it to an array of chars. Those types don't match up. Either write:

char string[] = {'s','o','m', 'e', ' ', 's', 't','r','i','n','g', '\o'}; //this is a bit silly, because it's the same as char string[] = "some string"; //or char *string[] = {"some", " string"};//array of pointers to CONSTANT strings //or char string[][10] = {"some", " string"}; 

Where the last version gives you an array of strings (arrays of chars) that you actually can edit...

like image 34
Elias Van Ootegem Avatar answered Sep 29 '22 12:09

Elias Van Ootegem