Sumit Kumar
Sumit Kumar

Reputation: 25

How to print array of element which is store in pointer to an array and this pointer to an array is store in array of pointer?

Hi How to print array of element which is sotre in pointer to an array and this pointer to an array is store in array of pointer?

/* note- ptoa is pointer to an array
aofp is array of pointer */

#include<stdio.h>

int main() {
     int i;
     int arr[3] = {1,2,3};   // integer array with 3 integers
     int (*ptoa)[3];         // ponter to an array
     ptoa = &arr;            // here i am storing 3 integer
                             // to ponter to an array
     int *aofp[1];           // this is array of pointer

     aofp[1] = ptoa;         // here i am sotring pointer to an array

     /* here i am trying to print all element of arr[3]
        using aofp i.e array of pointer*/

     for (i = 0; i < 3; i++)
         printf("%d", **aofp[i]);     // i know this is invalid 
         //please help me i want print all
         //element of arr i.e 1,2,3 using aofp?
}

Upvotes: 2

Views: 5048

Answers (4)

Nisse Engstr&#246;m
Nisse Engstr&#246;m

Reputation: 4751

The Problem

int *aofp[1]; // this is array of pointer

aofp is a one-element array of pointer to int.

aofp[1]=ptoa;   // here i am sotring pointer to an array

Here you are trying to assign a pointer to array of int to aofp. These are not compatible types. Also, element [1] doesn't exist. You have two options here, possibly more:

Option 1

You need to declare aofp with the correct type, and access element [0]:

int (*aofp[1])[3];
aofp[0]=ptoa;

for(i=0;i<3;i++)
  printf("%d",(*aofp[0])[i]);
putchar ('\n');

The parenthesis in the declaration of aofp are important, because int *aofp[1][3] would be an array-of-array-of-pointers, rather than an array-of-pointers-to-array. Accessing the values of the array uses the same syntax as the declaration. Retrieve element [0] of the array of pointers, dereference the pointer (*), and retrieve element [i] of the array of ints.

However, all this is unnecessarily convoluted, because a one-element array of pointers is pretty much the same as a single pointer. You might as well declare aofp as a pointer to array of int:

int (*aofp)[3];

And now we see that the type of aofp is exactly the same as that of ptoa. You could just as well skip aofp and print out the array pointed at by ptoa directly:

for (i=0;i<3;i++)
  printf("%d",(*ptoa)[i]);
putchar ('\n');

Of course, aofp might be declared to have more than one element at a later time.

Option 2

You need to assign a pointer-to-int to aofp[0]. Since ptoa is a pointer-to-array-of-int, dereferencing it results in a pointer to the first element of the int array. (Actually the result is an expression of type array-of-int, but you can't have array values in C, so it is immediately converted to a pointer-to-int.)

int *aofp[1];

aofp[0] = *ptoa;

for (i=0;i<3;i++)
  printf("%d",aofp[0][i]);
putchar ('\n');

And, like the previous example, a single-element array is rather useless. You might as well write:

int *aofp;

aofp = *ptoa;

for (i=0;i<3;i++)
  printf("%d",aofp[i]);
putchar ('\n');

Or just print out arr[i] directly.

Final note

You may have noticed that I have added a putchar ('\n') call at the end. The reason for this is that a C program might not produce any visible result unless it ends with a new-line.

Upvotes: 3

Utkan Gezer
Utkan Gezer

Reputation: 3069

Okay, you start off all good, except for the typing mistakes in comments maybe; but then you start doing invalid actions after some point, specifically at the line:

int *aofp[1];

This itself is all good actually. You are properly declaring a variable called aofp here, as an array of pointers to integers with a single element, with this statement. However, with the next line:

aofp[1]=ptoa;

It appears that you did not really want to declare that as an array of pointers to integers with a single element, but rather as an array of pointers to arrays of integers with three elements with a single element. This is because, the latter italicised thing is exactly what ptoa is of.

Another important thing here is that arrays in C are zero-based, which means that if you declare an array with the size denoted within square-brackets during declaration, the elements are indexed from [0] to [size - 1], so you should be making the assignment like:

aofp[0]=ptoa;

But this is only after declaring the aofp properly as I've mentioned.

The following code would be the thoroughly corrected version of your code, without changing the way you're handling the job:

#include<stdio.h>

int main( )
{
    int i;
    int arr[3] = {1, 2, 3};
    int(*ptoa)[3];
    ptoa = &arr;

    // previously was: int *aofp[1];
    int(*aofp[1])[3];   // aofp is declared as an
 /*  1  2     3   4  */ // array of 1   (3)
                        // pointer to   (2)
                        // array of 3   (4)
                        // integers     (1)

    // previously was: aofp[1] = ptoa;
    aofp[0] = ptoa;

    for (i = 0; i<3; i++)
        // previously was: printf("%d", **aofp[i]);
        printf("%d", (*aofp[0])[i]);

    return 0;   // should be there in C
}

For a code that compiles without warnings/errors, although it might be violating some rules, the following minimal changes could be done:

  1. At your own risk.
  2. aofp[1]=ptoa; to aofp[0] = (int *) ptoa;
  3. **aofp[i] inside printf to (*((int (*)[3]) aofp[0]))[i]

Should do it, but I wouldn't recommend it.

Upvotes: 3

ars
ars

Reputation: 707

this is exactly what you want:

int main()
{
     int i;
     int arr[3]={1,2,3};// integer array with 3 integers
     int *ptoa = arr;// ponter to an array
     int *aofp[1]; // this is array of pointer
     aofp[0]=ptoa;   // here i am sotring pointer to an array

     for(i=0;i<3;i++)
         printf("%d",*aofp[0]+i);
}

Upvotes: 1

yussuf
yussuf

Reputation: 635

Is it this what you want to do (http://coliru.stacked-crooked.com/a/f342849a8018de37)?

/* note- ptoa is pointer to an array
aofp is array of pointer */

#include<stdio.h>

int main()
{
     int i;
     int arr[3]={1,2,3};    // integer array with 3 integers
     int* ptoa = arr;       // pointer to an array

     int* aofp[3];          // this is array of pointer
     aofp[0] = ptoa;        // here i am storing pointer to an element of array
     aofp[1] = ptoa+1;      // here i am storing pointer to an element of array
     aofp[2] = ptoa+2;      // here i am storing pointer to an element of array

     /* here i am trying to print all element of arr[3]
        using aofp i.e array of pointer*/

     for(i=0;i<3;i++)
         printf("%d", *aofp[i]);  
}

Upvotes: 0

Related Questions