473,772 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9119
"Jason" <ja******@NO.SP AM.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.ne t
"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((s ize_t)(rooms*si zeof(int)));
for (int j = 1; j < rooms; ++j) {
roomnum[j] = 1 + j;
}
return p;
}

int maze_rooms(cons t 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(co nst maze* p) { // maze [pseudo]destructor
free((void)(((m aze*)p)->roomnum));
}

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

<snip>
void maze_destroy(co nst maze* p) { // maze [pseudo]destructor
free((void)(((m aze*)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.pow ernet.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
3981
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 bound inside struct or class to become global functions. It makes easier for me to use "struct.memberfunction()" instead of "globalfunction()" when I have to use dot between struct and member function rather than global function. I do not have...
7
2168
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
647
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 level pagetable. Thank you. struct _PageTable { struct _PageTable **PageArray; unsigned int *FrameArray;
22
2057
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; struct ln *next; } listNode, *listNodePtr; just to test in my main method I tried to initialize the
11
2125
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
575
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 system + OS e.g. Windows, Linux, WinCE. I am not quite sure about the alignment of the memory.... soln. 1: should be faster, I am not sure. idx size (bytes) 1 4
12
3885
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
5
2562
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 use to run a UM program, I kept on getting error messages. I have used someone else's implementation and it runs fine. I have compared my code with other's and I still can't figure it out what's wrong with mine. So please help me out, after 3...
3
2977
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 pass its first block pointer inside the structure Array to Array ->first and the last block pointer inside the structure Array to Array ->last.So i can manipulate the double linked list as a dynamic array. The cells of this dynamic array are...
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8937
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.