Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Double arrays with pointers

Status
Not open for further replies.

Guest42

Programmer
Nov 9, 2005
28
0
0
US
I have a quick question about the nature of making a 2D array using pointers, as in

int **v;

I tried making a little piece of code like this:

int **twoDee;
int i, j;
twoDee = (int**)malloc(X*Y*sizeof(int));
twoDee[2][1] = 5;
printf("%d", twoDee[2][1]);

This was just so I could try and access twoDee after initializing it and assigning a variable. However, the program crashes before anything is printed there, so I guess I'm unaware of the proper syntax. Can someone show me what it is?
 
Code:
int i
int* pInt;
int** ppInt;

/* Allocate array of 5 ints. */
pInt = malloc( 5 * sizeof( int ) );


/* First allocate array of 5 int* pointers. */
ppInt = malloc( 5 * sizeof( int* ) );

/* Then allocate 5 arrays that are each 5 ints long. */
for ( i = 0; i < 5; ++i )
{
   ppInt[ i ] = malloc( 5 * sizeof( int ) );
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top