Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers to arrays in C

Tags:

arrays

c

pointers

I just saw this code snippet Q4 here and was wondering if I understood this correctly.

#include <stdio.h>

int main(void)
{
   int a[5] = { 1, 2, 3, 4, 5 };
   int *ptr = (int*)(&a + 1);

   printf("%d %d\n", *(a + 1), *(ptr - 1));

   return 0;
}

Here's my explanation:

int a[5] = { 1, 2, 3, 4, 5 }; => a points to the first element of the array. In other words: a contains the address of the first element of the array.

int *ptr = (int*)(&a + 1); => Here &a will be a double pointer and point to the whole array. I visualize it like this: int b[1][5] = {1, 2, 3, 4, 5};, here b points to a row of a 2D array. &a + 1 should point to the next array of integers in the memory (non-existent) [kind of like, b + 1 points to the second (non-existent) row of a 2D array with 1 row]. We cast it as int *, so this should probably point to the first element of the next array (non-existent) in memory.

*(a + 1) => This one's easy. It just points to the second element of the array.

*(ptr - 1) => This one's tricky, and my explanation is probably flawed for this one. As ptr is an int *, this should point to int previous to that pointed by ptr. ptr points to the non-existent second array in memory. So, ptr - 1 should probably point to the last element of the first array (a[4]).

like image 775
Aneesh Dogra Avatar asked Jan 03 '14 07:01

Aneesh Dogra


People also ask

How to use pointers to access array elements in C?

Use a pointer to an array, and then use that pointer to access the array elements. For example, Let's see how to make a pointer point to a multidimensional array. In a [i] [j], a will give the base address of this array, even a + 0 + 0 will also give the base address, that is the address of a [0] [0] element. Pointer is used to create strings.

What is the first pointer to an array in C?

Pointer to an Array in C. balance is a pointer to &balance[0], which is the address of the first element of the array balance. Thus, the following program fragment assigns p as the address of the first element of balance −.

How do I understand pointers in C?

It is most likely that you would not understand this section until you are through with the chapter 'Pointers'. Assuming you have some understanding of pointers in C, let us start: An array name is a constant pointer to the first element of the array.

What is the relationship between array and pointer in C language?

Try quiz, solve problems & win rewards! Array and Pointers in C Language hold a very strong relationship. Generally, pointers are the variables which contain the addresses of some other variables and with arrays a pointer stores the starting address of the array.


1 Answers

Here &a will be a double pointer.

No. It is a pointer to an array. In this example, int (*)[5]. Refer C pointer to array/array of pointers disambiguation

so when you increment pointer to an array, it will crosses the array and points to non-existent place.

In this example, It is assigned to integer pointer. so when int pointer is decremented, it will point to previous sizeof(int) bytes. so 5 is printed.

like image 87
Jeyaram Avatar answered Oct 10 '22 02:10

Jeyaram