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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filling a 2-d array of pointers

Status
Not open for further replies.

Mungovan

Programmer
Oct 24, 2002
94
0
0
IE

Hi,
This is my problem. I have a 2-D array called grid that contains either pointers to structures called node, or NULL. The grid size is grid[SIZE][SIZE]. Each one of the struct nodes has various attributes such as location, id, positionX, and positionY. But when I try to print out the values I get an exception error!!
Any ideas anyone??

P.s - I've left out all the usual variable declaration


Code:
typedef struct node node;
node *nodeLocation;

//Initialise all pointer values in grid[][] to NULL
for (x=0; x < (SIZE); x++)
	{
	for (y=0; y < (SIZE); y++){
		grid[x][y] = NULL;
	}
 }

// populate grid[][] with different node pointers 
// POPULATION corresponds to the number of nodes required
	for (i=0; i < POPULATION; i++){

		nodeLocation = (node*)malloc(sizeof(node));

		positionX = (rand() % SIZE);
		positionY = (rand() % SIZE);
  
		grid[positionX][positionY] = nodeLocation;
		
		nodeLocation->id = i+1;
		nodeLocation->x = positionX;
        	nodeLocation->y = positionY;
		nodeLocation->age = 0;
                
                i++;
	}

[\code]

From here it compiles and runs grand.

It's when I try to print out, say, the node at grid[0][0], that I get the application error:

[code]
printf(&quot;\n%d\t%d\t%d\t&quot;,grid[0][0]->id, grid[0][0]->x, grid[0][0]->y);
[\code]

Any ideas please!!!????


Thanks,
D
 
I think that the o/p wil contain garbage, because positionX and positionY may not be 0 at all (due to rand() %SIZE)
 
can i see the declarations please ? looks like there is a problem there..

your grid[..][..] -> is it an array of structures .. even if yes, you need to malloc the array locations in the loop {where you are doing the initialisations to NULL}

also, where do you assign values for grid[..][..]->x ??

br:
 
Hi,

I think something wrong with the declarations. I tried same code and it is working.

here is the code But it is C not C++

#include <stdio.h>

#define SIZE 2
#define POPULATION 2

struct node {
int id;
int x;
int y;
int age;
};
typedef struct node node;
node *nodeLocation;


main()
{
int y,x, i, positionY, positionX;
node *grid[SIZE][SIZE];

/* Initialise all pointer values in grid[][] to NULL */
for (x=0; x < (SIZE); x++)
{
for (y=0; y < (SIZE); y++){
grid[x][y] = NULL;
}
}

/* populate grid[][] with different node pointers
// POPULATION corresponds to the number of nodes required */
for (i=0; i < POPULATION; i++){

nodeLocation = (node*)malloc(sizeof(node));

positionX = (rand() % SIZE);
positionY = (rand() % SIZE);

grid[positionX][positionY] = nodeLocation;

nodeLocation->id = i+1;
nodeLocation->x = positionX;
nodeLocation->y = positionY;
nodeLocation->age = 0;

i++;

}
printf(&quot;\n%d\t%d\t%d\t\n&quot;,grid[0][0]->id, grid[0][0]->x, grid[0][0]->y);

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top