What happens to pointers in this snippet?

I am curious why this is an error and what the error message means. Here is the code:

int *x[] = {"foo", "bar", "baz"}; int *y[] = {"foo", "bar", "baz"}; x = y; 

I try to compile and I get the following:

 error: incompatible types when assigning to type 'char *[3]' from type 'char **' 

Question No. 1 why is this a mistake? and question number 2, why are there different types?

+7
c pointers
source share
5 answers

As written, there are quite a few problems. First, array literals are of type char*[] , and x and y are of type int*[] . Secondly, you cannot assign arrays directly, since they are actually constant pointers.

+5
source share

There is an error of incompatible types because you assign arrays of strings (char * type in C) to arrays of pointers to ints (for example, int *x[] ). The error message given by the compiler is a bit confusing because C does a lot backstage to try to convert variables from one type to another.

Since characters are represented internally as numbers (letters correspond to their ASCII values), C can convert characters to ints, so it tries to treat the variables x and y as arrays of pointers to characters instead of ints, therefore, char *[3] . He sees {"foo", "bar", "baz"} as a char ** type, because strings are a char * type and arrays are essentially stored as pointers in C, so this is a pointer to char * or char ** .

While this is not completely related to your question, I also wonder what you are trying to do with x = y; As written, this will make x point to the same array as y, leaving the array x used to indicate inaccessibility. To check if two variables are equal in C, you should use the == operator. Testing equality is not so simple for arrays or strings, but it is completely beyond the scope of this question.

+3
source share

This is a mistake because an array is an unmodifiable value of lvalue - this means that you cannot assign it directly. You can only change array elements separately.

Types are different because if an array is used in a context where an lvalue is not required, it is evaluated by a pointer to its first element ( somearray becomes efficient &somearray[0] ).

Since the y array is used in this context, it is evaluated as the address of its first element, which is of type int ** . Since the array x is in the context of the lvalue, it is evaluated as an array ( int *[3] ). (Your error message does not actually match your code - I suspect your data is actually char * arrays).

("context lvalue" means, in large part, one of: the left side of the assignment operator (where the name comes from), the subject of the sizeof operator, or the & subject).

+2
source share

I would recommend starting with a good tutorial on pointers and arrays. It looks like you can come from an interpreted language such as JavaScript or PHP. In C, everything is completely different, and you need to get an idea of โ€‹โ€‹stack memory, heap memory, pointers, etc. After that, everything will make sense and you will love C.

0
source share

for some reason I cannot use the same account with which I posted the question.

"int" was a typo on my part, I really meant that it was a "char". Sorry for the mess.

However, I think the cafe answered my question.

โ€œThis is a mistake because the array is an unmodified lvalue value, which means you cannot assign it directly. You can only change the elements of the array individually.

I'm just trying to understand how c pointers work, so I am trying to use these random things that you probably didnโ€™t usually do. I thought this might work, since I thought arrays = pointers, so I can just make x a y point. Probably not?

Thanks to all your response guys.

0
source share

All Articles