Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String pointer and array of chars in c

I just start learning C and found some confusion about the string pointer and string(array of char). Can anyone help me to clear my head a bit?

// source code
char name[10] = "Apple";
char *name2 = "Orange";

printf("the address of name: %p\n", name);
printf("the address of the first letter of name: %p\n", &(*name));
printf("first letter of name using pointer way: %c\n", *name);
printf("first letter of name using array way: %c\n", name[0]);
printf("---------------------------------------------\n");
printf("the address of name2: %p\n", name2);
printf("the address of the first letter of name2: %p\n", &(*name2));
printf("first letter of name2 using pointer way: %c\n", *name2);
printf("first letter of name2 using array way: %c\n", name2[0]);

// output
the address of name: 0x7fff1ee0ad50
the address of the first letter of name: 0x7fff1ee0ad50
first letter of name using pointer way: A
first letter of name using array way: A
---------------------------------------------
the address of name2: 0x4007b8
the address of the first letter of name2: 0x4007b8
first letter of name2 using pointer way: O
first letter of name2 using array way: O

so I assume that both name and name2 point to the address of their own first letter. then my confusion is(see the code below)

//code
char *name3; // initialize a char pointer
name3 = "Apple"; // point to the first letter of "Apple", no compile error

char name4[10]; // reserve 10 space in the memory
name4 = "Apple"; // compile errorrrr!!!!!!!!!!

I create a char pointer called name2 and name2 pointer to the first letter of "Apple" which is fine, then I create another array of char and allocate 10 space in the memory. and then try to use name4 which is an address points to the first letter of "Apple". As a result, I got a compile error.

I am so frustrated by this programming language. sometimes they works the same way. but sometimes they doesn't. Can anyone explain the reason and if I really want to create a string or array of chars in separated lines. how I can do that???

Many thanks...

like image 500
eded Avatar asked Jun 13 '13 00:06

eded


People also ask

What is the difference between char array and char pointer in C?

For the array, the total string is stored in the stack section, but for the pointer, the pointer variable is stored into stack section, and content is stored at code section. And the most important difference is that, we cannot edit the pointer type string.

What is array of pointers to string in C?

An array is a contiguous block of memory, and when pointers to string in C are used to point them, the pointer stores the starting address of the array. Similarly, when we point a char array to a pointer, we pass the base address of the array to the pointer.

What is string or array and pointer?

• Pointer – Variable storing a memory location. • Array – Block of memory storing associated variables. Array identifier is a pointer to location of first element. • String – Array of character variables.

What is character array and string array in C?

Character array is collection of variables, of character data type. String is class and variables of string are the object of class "string". Syntax. char array_name [size];


2 Answers

You can initialize an array when you declare it, like this:

int n[5] = { 0, 1, 2, 3, 4 };
char c[5] = { 'd', 'a', 't', 'a', '\0' };

But since we typically treat char arrays as strings, C allows a special case:

char c[5] = "data";  // Terminating null character is added.

However, once you've declared an array, you can't reassign it. Why? Consider an assignment like

char *my_str = "foo";  // Declare and initialize a char pointer.
my_str = "bar";        // Change its value.

The first line declares a char pointer and "aims" it at the first letter in foo. Since foo is a string constant, it resides somewhere in memory with all the other constants. When you reassign the pointer, you're assigning a new value to it: the address of bar. But the original string, foo remains unchanged. You've moved the pointer, but haven't altered the data.

When you declare an array, however, you aren't declaring a pointer at all. You're reserving a certain amount of memory and giving it a name. So the line

char c[5] = "data";

starts with the string constant data, then allocates 5 new bytes, calls them c, and copies the string into them. You can access the elements of the array exactly as if you'd declared a pointer to them; arrays and pointers are (for most purposes) interchangeable in that way.

But since arrays are not pointers, you cannot reassign them.

You can't make c "point" anywhere else, because it's not a pointer; it's the name of an area of memory.

You can change the value of the string, either one character at a time, or by calling a function like strcpy():

c[3] = 'e';       // Now c = "date", or 'd', 'a', 't', 'e', '\0'
strcpy(c, "hi");  // Now c = 'h', 'i', '\0', 'e', '\0'
strcpy(c, "too long!") // Error: overflows into memory we don't own.

Efficiency Tip

Note, also, that initializing an array generally makes a copy of the data: the original string is copied from the constant area to the data area, where your program can change it. Of course, this means you're using twice as much memory as you may have expected. You can avoid the copy and generally save memory by declaring a pointer instead. That leaves the string in the constant area and allocates only enough memory for a pointer, regardless of the length of the string.

like image 147
Adam Liss Avatar answered Oct 20 '22 08:10

Adam Liss


Although pointer and array seems familiar, they are different. the char *name3 is just a pointer to char, it takes no more memory than a pointer. it just store a address in it, so you can assign a string to it then the address stored in name3 change to the address of "Apple".

But your name4 is an array of char[10], it holds the memory of 10 chars, if you want to assign it, you need to use strcpy or something to write it's memory, but not assign it with an address like "Apple".

like image 24
leonhart Avatar answered Oct 20 '22 07:10

leonhart