0

The following code

#include <stdio.h>
char * arr[]={"my","array"};
main()
{
        printf("%p %p\n",arr, &arr);
        char *(*arr_ptr)[2];
        arr_ptr = &arr;
        arr_ptr = arr;
}

compiles with the warning

2.c: In function ‘main’: 2.c:15:10: warning: assignment from incompatible pointer type [enabled by default] arr_ptr = arr;

However, being run, the compiled program would print two identical numbers.

The conclusion is that there exists a dissimilar type, namely 'pointer to the array', that points to exactly the first element of the array.

My question is: what is the reason for such a pointer type to exist, isn't just having arr as an pointer to the array not enough?

lvd
  • 793
  • 3
  • 12
  • https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer – Retired Ninja May 12 '19 at 22:38
  • 1
    I think it works the other way round. There is a reason for implicitly converting an array of type into a pointer of type. A pointer to array is just a normal pointer. There are languages where you pass arrays with size information themselves and have pointers to arrays. – KamilCuk May 12 '19 at 22:39
  • 1
    Consider `int arr[4][7];`. Then `arr` and `arr[0]` are very different things. – melpomene May 12 '19 at 22:43
  • 1
    Pointers to arrays allow pointers into multi-dimensional arrays. If you have `T a[10]`, then `T *p` is a pointer that can point to an element of `a`, which is how one-dimensional arrays are normally accessed with pointers. But suppose `T` is an array type, e.g. `int [20]`. Then `a` has type `int a[10][20]`. To access a top-level element of `a` with a pointer, one needs `T *p`, i.e. `int (*p)[20]`. – Tom Karzes May 12 '19 at 22:50
  • `arr_ptr = arr;` is an error in Standard C. When you say "However, being run, the compiled program would print two identical numbers." actually what you are seeing is the compiler assuming you meant `arr_ptr = &arr;` and proceeding. You can (and I would advise you to) configure the compiler to reject invalid code. – M.M May 12 '19 at 23:05
  • @M.M as you could see I print the addresses of both `arr` and `&arr` using `"%p"` formatting declaration in `printf` so it can't be wrong. The pointer assignment of incompatible types of course yields a warning, and this is intended as the part of the question. – lvd May 13 '19 at 10:41
  • @lvd you have configured the compiler to print a warning and change the meaning, for this case of invalid code. Your printing with `%p` causes undefined behaviour and is irrelevant. – M.M May 13 '19 at 11:25

2 Answers2

1

The reasons to have pointers to arrays are the same as the reasons for types:

  • It helps reduce errors. If a variable is declared to be a pointer to an array, and you try to assign a different type of pointer to it, the compiler can warn you when your code is compiled instead of letting your program do the wrong thing when it executes. This is the same reason we have pointers to int separate from pointers to char. It is even the reason we have int separate from char—giving types to all variables helps ensure they are used for what they are intended for.
  • It provides convenience. When you have an array of arrays and want to work with specific rows, you might have a pointer A to one of the rows (which is an array). Then A+1 is the next row, not the next element in the row, and A-2 is two rows back. So pointers to arrays can be used to work with arrays as objects, and the compiler will track the arithmetic for you so you do not have to use A-2*NumberOfColumns to calculate how many elements back is two rows back.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

Because why not.Pointer's job is pointing thinks.You can use that type of pointer's (pointer to array) make your job's easier and when you know the 1. element of the array you will know the rest so pointer will be wery usefull here's a simple example

 int i = 0;

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

  for(;i<4;i++){
    printf("%d \n",arr[i]);
  }

the output will be :

1 2 3 4

but using simple pointers

  *arr = 5;
  *(arr+1)=6;
  *(arr+2)=7;
  *(arr+3)=8;

the output will be :

5 6 7 8

reached array with pointers easly.

  • 1
    Where is there a pointer to an array in this answer, rather than a pointer to an `int`? – Eric Postpischil May 12 '19 at 23:32
  • I thought this person does not understand how pointer works.So i explained about pointer's.Because int pointer point's also array pointer point's but in different data type. – Sefa Kalkan May 14 '19 at 13:55