1

i am using a pointer to an array of 4 integers as int(*ptr)[4]

using the following code in which m pointing to a 2-D array using this pointer

int arr[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int (*ptr)[4]= arr[4];
int m= (*ptr)[2];

what will be the value in "m"...

i need to find the value of element arr[1][2] how can i get it using pointer ptr?

Carl Norum
226k42 gold badges442 silver badges480 bronze badges
asked Jun 10, 2010 at 18:34
9
  • Format your code next time. Is this a homework question? Tag it that way if it is, and tell us what you've tried and what your exact problem is. Commented Jun 10, 2010 at 18:36
  • Also, what's stopping you from running it and trying (it won't work, by the way). Commented Jun 10, 2010 at 18:37
  • On the second line 'arr[4]' isn't that an index out of range? The max size of arr has been declared as 3. So the maximum index should be 2 as it is zero-based. Commented Jun 10, 2010 at 18:39
  • make ir arr[2] even then its not giving the correct output... Commented Jun 10, 2010 at 18:42
  • ptr= arr[2] gives an error.. why??? Commented Jun 10, 2010 at 18:43

3 Answers 3

1

Multi-dimensional arrays are really one dimensional arrays with a little syntaxic sugar. The initialization you had for ptr wasn't an address. It should have been

int *ptr[4] = { &arr[0][0], &arr[1][0], &arr[2][0], &arr[3][0]};

You can also leave the 4 out and just use

int *ptr[] = { &arr[0][0], &arr[1][0], &arr[2][0], &arr[3][0]};

I made a few modifications to your code below. Note the two sections with the printf. They should help to demonstrative how the values are actually laid out in memory.

#define MAJOR 3
#define MINOR 4
int arr[MAJOR][MINOR]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int (*ptr)[4];
int *p = &arr[0][0];
// init ptr to point to starting location in arr
for(i = 0; i < MAJOR; i++) {
 ptr[i] = &arr[i][0];
}
// print out all the values of arr using a single int *
for(i = 0; i < MAJOR * MINOR; i++) {
 printf(" %d", *(p + i) );
}
for(i = 0; i < MAJOR; i++) {
 for(j = 0; j < MINOR; j++) {
 printf( " %d", *(p + i * MAJOR + j) );
 }
 printf("\n");
}
answered Jun 10, 2010 at 20:27
Sign up to request clarification or add additional context in comments.

Comments

0

Ptr doesn't point to anything and probably doesn't even compile, since [4] is outside the bounds of either the [3] or the [4] of the original array.

If you need to find arr[1][2] then you need int (*ptr)[4] = arr[1]; int m = ptr[2];

answered Jun 10, 2010 at 18:37

5 Comments

int (*ptr)[4] = &arr[1]; int m = (*ptr)[2]; Note the extra & and *. Your variant will not even compile.
hey anDrey tell me one thing int (*ptr)[4]= &arr[4] will be out of bound?
@nitinpuri, yes it will be out of bounds - arr only has 3 elements, so the maximum index is 2.
arr was declared with the size [3] and [4]. With only one index it refers to the first size [3]. Since arrays in C/C++ are zero based the maximum value allowed there would be 2. And to clarify what DeadMG refers to, you need to use the '&' to get the reference of arr at the specified index.
first part is done but int m= (*ptr)[2] does not point to element 3 of 4 element array..
0

The are multiple ways of accessing the value of element arr[1][2].

The method used in the code below is called pointer to pointer. By storing the address of the first element of pa[] in pb, one can use pb to access elements of aa[][]. One can use array notation aa[1][2], or one can use pointer notation *((*pb+1)+2). Both the notation approaches yield equivalent results.

#include<stdio.h>
int main()
{
 int aa[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
 int *pa[3];
 for(int i=0;i<3;i++)
 pa[i]=aa[i];
 pb=pa;
 printf("pb[1][2]=%d\n",pb[1][2]); /*Array Notation*/
 printf("arr[1][2]=%d",*(*(pb+1)+2)); /*Pointer Notation*/
}
answered Nov 4, 2017 at 0:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.