Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Assigning pointers to arrays? Based on four examples

Tags:

arrays

c

pointers

Assume following Code:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
    int arrayXYZ[10];
    int i;
    int *pi;
    int intVar;
    for (i=0; i<10; i++){
        arrayXYZ[i] = i;
    }
    pi = arrayXYZ; // Reference 1
    pi++;          // Reference 2
    arrayXYZ++;    // Reference 3
    arrayXYZ = pi;  // Reference 4
}
  • Reference 1 is correct: pi points to first element in arrayXYZ -> *pi = 0
  • Reference 2 is correct: element to which pi points is incremented -> *pi = 1
  • Reference 3 is not correct: I am not completely sure why. Every integer needs 4 bits of memory. Hence, we cannot increment the address of the head of the array by just one? Assume, we had a char array with sizeof(char)=1 -> Would the head of the array point to the next bucket?
  • Reference 4 is not correct: I am not completely sure why. Why cannot the head of the array point to the address to which pi points?

Thanks for all clarifications! I am a new member, so if my question doesn't follow the Stackoverflow guidelines, feel free to tell me how I can improve my next questions!

like image 395
AndrejCoding Avatar asked Feb 06 '18 16:02

AndrejCoding


People also ask

Can you assign a pointer to an array in C?

You cannot assign a new pointer value to an array name. The array name will always point to the first element of the array.

What is pointer to array explain with example?

Pointers to an array points the address of memory block of an array variable. The following is the syntax of array pointers. datatype *variable_name[size]; Here, datatype − The datatype of variable like int, char, float etc.

What is the use of pointer to an array in C?

An array of pointers is similar to any other array in C Language. It is an array which contains numerous pointer variables and these pointer variables can store address values of some other variables having the same data type.

How are pointers and arrays related in C?

Array in C is used to store elements of same types whereas Pointers are address varibles which stores the address of a variable. Now array variable is also having a address which can be pointed by a pointer and array can be navigated using pointer.


2 Answers

arrayXYZ++;

This is equivalent to:

arrayXYZ += 1;

which is equivalent to:

arrayXYZ = arrayXYZ + 1;

This is not allowed because the C language does not allow it. An array can not be assigned to.

arrayXYZ = pi;

This fails for the same reason. An array can not be assigned to.

The other assignments work because you are allowed to assign to a pointer.

Also keep in mind that arrays and pointers are distinct datatypes. In C, there are circumstances where arrays decay into a pointer to their first element for convenience purposes. Which is why this works:

pi = arrayXYZ;

However, this is just an automatic conversion, so that you don't have to write:

pi = &arrayXYZ[0];

This automatic conversion does not mean that arrays are the same thing as pointers.

like image 186
Nikos C. Avatar answered Sep 24 '22 04:09

Nikos C.


From C11 standard §6.3.2.1 (N1570)

An lvalue is an expression (with an object type other than void) that potentially designates an object;64) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const- qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const- qualified type.

And also From §6.5.2.4

The operand of the postfix increment or decrement operator shall have atomic, qualified, or unqualified real or pointer type, and shall be a modifiable lvalue.

As pointed out here these are the reasons why those statements are illegal. Same way for assignment operation the left one has to be modifiable. Here it is not. That's why the problem.

Now to explain why the other two works - there is a thing called array decay. Array in most situations (exceptions are when used in operand of &, sizeof etc) are converted to pointer to the first element of the array and that pointer is being assigned to the pi. This is modifiable. And that's why you can easily apply ++ over it.

like image 44
user2736738 Avatar answered Sep 25 '22 04:09

user2736738