0

I have an array arr say

int arr[3] = {1, 2, 3};

so as per my knowledge this arr is just like any variable that has a name arr just like

int a = 4;

a is an integer variable that asks 4 bytes of memory. And arr asks 3 * 4 = 12 bytes of memory and arr has properties like 12 bytes of memory and when used as an Rvalue it decays to a

(pointer value not a pointer variable)

so as it decays to a pointer value like &arr[0] we can use a pointer variable to point (grab the address)

int* ptr = arr; //here arr implicitly converts to &arr[0]

so here arr and ptr have the same address but ptr is like copied the memory of &arr[0] and stores it. Whereas arr is literally the address in the memory which only decays like &arr[0] when used as Rvalue.

if we do something like

arr++;

complier throws error as Expression must be a modifiable lvalue As the arr is allocated on the stack we cannot change the address because we are asking cpu to change the address of the array, if we change it we loose the array address and cannot further access it

Code

#include <stdio.h>

int main()
{
    int arr[3] = { 1, 2, 3 };
    arr++; // not allowed 

    int* p = arr;
    p++;   // allowed because its a copy of (arr) address.

    return 0;
}

That's how what i know and correct me with more resources.

  • 1
    Objects have a fixed address once created – M.M Mar 31 '21 at 10:50
  • Does this answer your question? [Arrays are Pointers?](https://stackoverflow.com/questions/3959705/arrays-are-pointers) – SOFuser Mar 31 '21 at 11:13
  • your question is already answered in thread [3959705](https://stackoverflow.com/questions/3959705/arrays-are-pointers) – SOFuser Mar 31 '21 at 11:14

2 Answers2

1

arr is not a pointer, it's an array. When you use the word arr the compiler may convert it to a pointer for you - it does &arr[0] automatically, and gets the address of the first element, which is a pointer value, not a pointer variable. And you can't change values. You can't do (&some_variable)++;, you can't do 5++;, and you can't do (&arr[0])++; which is what arr++; would mean.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • An array is a pointer though. It's a const pointer to the first element. – Irelia Mar 31 '21 at 10:52
  • @Nina No it's not. An array is an array. A group of one or more values in contiguous memory locations. – user253751 Mar 31 '21 at 11:02
  • How is `int arr[1] = {10}` different than a const pointers which points to the address of an integer holding the value of 10? `int x = 10; const int* px = &x` – Irelia Mar 31 '21 at 11:16
  • And is `int* x = malloc(10*sizeof(int))` an array or a pointer? By your definition of an array "A group of one or more values in contiguous memory locations" this should be an array. Yet I'm able to modify where it points to. – Irelia Mar 31 '21 at 11:27
  • @Nina how's `int x = 10;` different from `int x[1] = {10};`? Both allocate 4 bytes of memory and put the number 10 in that space, don't they? – user253751 Mar 31 '21 at 11:56
  • Yes...so an array is a const pointer to the first element.. – Irelia Mar 31 '21 at 11:57
  • @Nina where's the pointer? Pointers are 4 bytes (on 32-bit), ints are 4 bytes, `int x[1] = {10};` only allocates 4 bytes, which is for the int - where's the pointer? – user253751 Mar 31 '21 at 14:06
  • @Nina an array is a array and a pointer is a pointer. In most cases array names decay to pointer like forms and are treated similarly to pointer but aren't actually pointers. This can be proved by the fact that you can't assign another memory address to the array name. If you could you could call it a pointer, but you can't hence meaning it's not a pointer, it's rather treated like a pointer like object. – programmer Apr 04 '21 at 14:00
0

Arrays are not pointers.

Consider the following demonstrative program.

#include <stdio.h>

int main(void) 
{
    int arr[3] = { 1, 2, 3 };
    int *p = arr;
    
    printf( "sizeof( arr ) = %zu\n", sizeof( arr ) );
    printf( "sizeof( p ) = %zu\n", sizeof( p ) );
    
    return 0;
}

Its output might look like

sizeof( arr ) = 12
sizeof( p ) = 8

Arrays are non-modifiable lvalues. That means that you for example can not assign one array to another or apply an increment operator.

On the other hand, arrays used in expressions are implicitly converted (with rare exceptions) to pointers to their first elements.

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

So for example in this declaration

int *p = arr;

the array arr used as an initializer expression is implicitly converted to pointer to its first element that has the type int *.

To set the pointer p to point to the second element of the array arr you could use the pointer arithmetic the following way

int *p = arr + 1;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • @Nina x is a pointer that points to the starting address of a dynamically allocated extent of memory that can contain an array of 10 elements of the type int. Using the pointer arithmetic with the pointer x you can access each of the 10 elements – Vlad from Moscow Mar 31 '21 at 11:27