简体   繁体   中英

Assign char array to another char array

I know we can not assign a char array to another char array like:

 char array1[] = "Hello";
 char array2[] = "Hi!";

 array1 = array2;//does not compile

But:

 char array1[] = "Hello";
 char *array2 = NULL;

 array2 = array1; //compile

 printf("%s", array2); //display Hello

This works.

Can anyone please explain why?

Thanks!

Plain arrays are not assignable. That is why the first code sample doesn't work.

In the second sample, array2 is just a pointer, and array1 , despite being an array, can decay to a pointer to its first element in certain situations. This is what happens here:

array2 = array1;

After this assignment, the pointer array2 points to the first element of the array array1 . There has been no array assignment, but, rather, pointer assignment.

char array1[] it's basically a pointer to de first letter of the array of char. you can assign to another pointer variable this address.

here is an example:


#include <stdio.h>

int main(){

    char a[] = "hello";

    char *b;

    a = b;

    printf("%s\n", a);

    return 0;
}

this code will output:

>>> hello 

That it would be more clear consider at first the following code

char *p = ( char * ) malloc( 6 * sizeof( char ) );

In fact you dynamically allocated an unnamed character array of 6 elements and assigned the address of the first element of the array to pointer p. Now you can use this pointer to access any alement of the array. For example

p[0] = 'H';
p[1] - 'e';
p[3] = 'l';
p[4] = 'l';
p[5] = 'o';
p[6] = '\0';

printf( "%s\n", p );

The same is valid for your code snippet only instead of the unnamed array and the address returned by function malloc you use name of existent array that is adjusted the same way to the pointer to the first element of the array

char array1[] = "Hello";
char *array2;

array2 = array1; 

printf( "%s\n", array2 ); 

So the array itself is not copied. You simply introduced a pointer to its first element. Using this pointer you can access any element of the array. because it points to the memory area occupied by the array.

If you want to create a copy of the array you should write

#include <string.h>

//...

char array1[] = "Hello";
char array2[sizeof( array1 )];

strcpy( array2, array1 ); 

It's like this:

For the compiler both are treated as a pointer, but the difference is that:

  • char array1[] = {...} is a FIXED pointer that points toward a fixed area of memory (in this case, it's in the stack)

  • char *array2 = ... is a variable pointer that can point to any point of memory (with permission of course),

In the first case the compiler knows even the size of array, while in the second case, the compiler just knows it's a pointer, so it cannot know the size of the array it's pointing to.

So, for printf() , both are pointers...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM