INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Put pointers into an array

Put pointers into an array

(OP)
Hi,

I am playing pointer array but do not understand what is going on.

This pointer array *d[] looks correct:
 int a1[2],b1[3];
 int *d[]={a1,b1};
 d[0][1]=1; d[1][2]=2;
 cout<<d[0][1]<<", "<<d[1][2]<<endl;
 
Why this pointer array **d[] is not correct:
 int a2[1][2],b2[3][4];
 int **d[]={a2,b2};

I will be appreciated for any help!

RE: Put pointers into an array

Let's consider the declaration:

CODE

int **d[] = ...
It's an array of pointers to pointer to int. In other words, an element of a is a pointer to pointer (to int).
However

CODE

int a1[1][2]
declares an array of arrays of two ints. In other words, it's possible to convert a1 to a pointer to an array (of 2 ints), but not in a pointer to pointer.

Analogous case

CODE

int b2[3][4]
declares an array of arrays of four ints. Have you noticed that both a2 and b2 (and elements of d) have different types?

So it's impossible to initialize d by different types values a2 and b2.
 

RE: Put pointers into an array

(OP)
Thank you.
I tried the same type:
    int a[1][2];
    int **b[1];
    b[0]=a;
but still got error........

RE: Put pointers into an array

Of course, you got exactly the same type error - reread my prev post again (and again;). Reread your C text-book (arrays part)...

That's true type of array b:

CODE

int a[1][2]; // array of arrays of 2 ints
typedef int ArrayOf2ints[2];
ArrayOf2ints* b[1]; // compare with different type int** b[1]
b[0] = a; // that's OK
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close