| | |
pointer to member of struct
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
Solved Threads: 0
I need to get a pointer to a particular member variable of a structure
Here's what i have done
but this isn't working. What's the correct way to do this
Here's what i have done
C Syntax (Toggle Plain Text)
typedef struct { int var1; other variables ... } structname; ---some other code here--- structname* structptr; int* intptr; intptr=(int*)&(structptr->var1)
but this isn't working. What's the correct way to do this
Well I am not able to point out your mistakes exactly but this code does work:
I think the way in which you have defined the typedef of the struct is wrong.
Apart from that the initialization of pointer for struct or creating the sruct of some type as
<struct name> variable;
doesn't make the structure usable. You need to allocate memory for the structure using malloc as mentioned above.
c Syntax (Toggle Plain Text)
#include<stdio.h> struct node{ int a; char b; }; typedef struct node * NODE; int main() { NODE n; int *in; char *ch; n=(NODE)malloc(sizeof(struct node*)); in=&(n->a); ch=&(n->b); *in=10; *ch='a'; printf("%d %c",*in,*ch); return 0; }
I think the way in which you have defined the typedef of the struct is wrong.
Apart from that the initialization of pointer for struct or creating the sruct of some type as
<struct name> variable;
doesn't make the structure usable. You need to allocate memory for the structure using malloc as mentioned above.
Last edited by csurfer; Feb 6th, 2009 at 10:26 am.
I Surf in "C"....
Related to:
Fail to check for a successful return of allocated memory.
Fail to release dynamic memory.
The code indentation has room to improve.
n=(NODE)malloc(sizeof(struct node*)); Casting malloc is not necessary if the proper header file stdlib.h is included.Fail to check for a successful return of allocated memory.
Fail to release dynamic memory.
The code indentation has room to improve.
Last edited by Aia; Feb 6th, 2009 at 11:24 am.
•
•
•
•
Related to:
n=(NODE)malloc(sizeof(struct node*));Casting malloc is not necessary if the proper header file stdlib.h is included.
Fail to check for a successful return of allocated memory.
Fail to release dynamic memory.
it should end with free(n);
and should possess error check as you said.
Can you be a bit more descriptive in your explanation of first point.?
I Surf in "C"....
![]() |
Similar Threads
- Return pointer to array of a struct (C++)
- Struct's and Pointer Question (C)
- Pointer Issues (C++)
- Function Pointers, Callbacks, and C++... (C++)
- assigning a memory to an array pointer (C)
- Passing struct to procuedre (C)
- new member here :) (C)
- pointer to member? (C++)
Other Threads in the C Forum
- Previous Thread: make fgets() wait for new lines.
- Next Thread: getString
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






