473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

HELP: Accessing int pointer inside struct

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;
}

--
Jason
[ jake1138 AT yahoo DOT com ]
Nov 13 '05 #1
5 9104
"Jason" <ja******@NO.SPAM.yahoo.com> wrote in message
news:bg**********@terabinaries.xmission.com...
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
rh*******@pobox.com

Nov 13 '05 #2
Jason wrote:

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
*).
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #3
Joe Wright wrote:
Jason wrote:
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


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

--
Artie Gold -- Austin, Texas

Nov 13 '05 #4
Jason wrote:
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));
}

Nov 13 '05 #5
E. Robert Tisdale wrote:

<snip>
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.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
7
by: dam_fool_2003 | last post by:
friends, I wanted to learn the various ways of inserting a single list. so: Method 1: #include<stdlib.h> #include<stdio.h> struct node { unsigned int data; struct node *next;
3
by: Robert Rota | last post by:
Need help figuring out why my program core dumps and if I have the structures right. If you are interested in helping me I can send you a copy of the code. The program is supposed to mimic a 3...
22
by: Dave Cooke | last post by:
Hi I am very new to C. I am trying to figure out how to initialize a struct in my main program. The struct is declared in anouther header file like this... typedef struct ln { int key; int data;...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
3
by: dreiko466 | last post by:
(sorry about my english...) I am a newbie in C (3 month expierience) I have wrote a simple test programm in VS2005, what i do wrong?Please... In this programm i create a double linked list.Then ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.