7

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!

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You may want to mention const and strcpy for completeness.
Side note: I always feel like C++ would be a much neater language if arrays were treated like first-class objects. That is, if operators like [] and = were defined for arrays (rather than requiring array-to-pointer conversion). It would be nice and uniform.
1

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...

6 Comments

I wouldn't say both are treated as pointers. It is only when you start to access elements of an array that you start dealing with pointers.
yes, it's kind of.... but what is the difference between them? first case the compiler has more information... I don't see any other difference... I don't have the actual formal answer. It's just a point of view...
When you are tempted to contribute in a discussion on pointers on this StackExchange (especially C), First, think of the words pedantic, precise, particular, picky, painful, payoff. (there are many others). Then decide if you will enter the fray. When you make a particular statement that is not precisely based on a set of pedantic rules, someone will come along and pick it apart. This is sometimes painful, but should payoff in the long run if you are willing to learn.
ok... you're right... but why can't arrays be considered pointers? I just can't remember any situation where it differs from pointers, with exception of assignment and sizeof() operation...
@WagnerPatriota: I think you answered your own question in your comment.
|
1

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 

Comments

0

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 ); 

2 Comments

sizeof(char) is 1.
@Carl Norum What did you want to say?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.