HELP: Accessing int pointer inside struct

J

Jason

How do you access an int pointer inside a struct. This is how it seems like
it would work to me, but it doesn't...

struct maze {
int num;
int * roomnum;
};

void setmaze(struct maze * maze) {
maze->num = 1; //this is equivelant to: (*maze).num = 1
maze->(*roomnum) = 5;
}
 
R

Russell Hanneken

Jason said:
How do you access an int pointer inside a struct. This is how it seems
like it would work to me, but it doesn't...

struct maze {
int num;
int * roomnum;
};

void setmaze(struct maze * maze) {
maze->num = 1; //this is equivelant to: (*maze).num = 1
maze->(*roomnum) = 5;
}

That last line should be

*maze->roomnum = 5;

or, to make it clearer,

*(maze->roomnum) = 5;

I'm assuming roomnum is pointing somewhere valid.

Regards,

Russell Hanneken
(e-mail address removed)
 
J

Joe Wright

Jason said:
How do you access an int pointer inside a struct. This is how it seems like
it would work to me, but it doesn't...

struct maze {
int num;
int * roomnum;
};

void setmaze(struct maze * maze) {
maze->num = 1; //this is equivelant to: (*maze).num = 1
maze->(*roomnum) = 5;
}
As far as anyone can see, maze is the tag of a structure declaration. We
can see no structure object defined. 'struct maze *maze' is therefore
nonsense. How about this...

typedef struct maize {
int num;
int *roomnum;
} maize; /* Corny, right? */

Now maize is a user defined type for a structure. We can define a
structure of this type with something like this..

maize maze;

Now maze is a structure and maze.num is (int) and maze.roomnum is (int
*).
 
E

E. Robert Tisdale

Jason said:
How do you access an int pointer inside a struct.
This is how it seems like it would work to me, but it doesn't...

typedef struct maze {
int num;
int *roomnum;
} maze;

void setmaze(maze* maze) {
maze->num = 1; //this is equivelant to: (*maze).num = 1
*(maze->roomnum) = 5; // error; roomnum is *not* initialized!
}

maze* maze_initialize(maze* p, int rooms) { // private
p->num = rooms;
int* roomnum = (int*)malloc((size_t)(rooms*sizeof(int)));
for (int j = 1; j < rooms; ++j) {
roomnum[j] = 1 + j;
}
return p;
}

int maze_rooms(const maze* p) {
return p->num;
}

maze maze_create(int n) { // default maze [pseudo]constructor
maze m;
maze_initialize(&m, n);
return m;
}

void maze_destroy(const maze* p) { // maze [pseudo]destructor
free((void)(((maze*)p)->roomnum));
}
 
A

Artie Gold

Joe said:
As far as anyone can see, maze is the tag of a structure declaration. We

Right. And `struct maze' is the type of the structure to which the
argument `maze' points. You're solving a problem that doesn't exist.
can see no structure object defined. 'struct maze *maze' is therefore
nonsense. How about this...

typedef struct maize {
int num;
int *roomnum;
} maize; /* Corny, right? */

Now maize is a user defined type for a structure. We can define a
structure of this type with something like this..

maize maze;

Now maze is a structure and maze.num is (int) and maze.roomnum is (int
*).

HTH,
--ag
 
R

Richard Heathfield

E. Robert Tisdale wrote:

void maze_destroy(const maze* p) { // maze [pseudo]destructor
free((void)(((maze*)p)->roomnum));

free() takes as its parameter a pointer value previously returned by malloc,
calloc, or realloc.

Your compiler should have warned you about this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,075
Messages
2,570,564
Members
47,200
Latest member
Vanessa98N

Latest Threads

Top