LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2010, 03:56 AM   #1
is noname
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Rep: Reputation: 0
access value from nested struct <c language>


help me guyss ..
i am beginner.
I having a nested structures which are referenced with pointer variables :

example :
typedef struct {
int a ;
char *b ;
int *c ;
}EXP2 ;

typedef struct {
int a ;
char *s ;
EXP2 *exp2 ;
}EXP1 ;

EXP1 sv ;

int main () {
sv.exp2->a = 5 ;
printf("%d",sv.exp2->a) ;

}
While trying to access the value of sv.exp2->a it throws segmentation fault error.
How to acess this variable ??

Thankssss ..
 
Old 03-11-2010, 04:11 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
Code:
int main () {
sv.exp2 = new EXP2;
sv.exp2->a = 5 ;
printf("%d",sv.exp2->a) ;
}
 
1 members found this post helpful.
Old 03-11-2010, 09:38 AM   #3
Xyro
LQ Newbie
 
Registered: Aug 2009
Location: Canada
Distribution: Ubuntu 9.04
Posts: 22

Rep: Reputation: 19
Quote:
Originally Posted by troop View Post
Code:
int main () {
sv.exp2 = new EXP2;
sv.exp2->a = 5 ;
printf("%d",sv.exp2->a) ;
}
I think he is working in C. However, the problem is the same.

The struct EXP2 does not exist within exp1, only a pointer to a EXP2 struct. In your code you have not created one, thus when you try to dereference the exp2 pointer, you're dereferencing a NULL pointer and it make ka-boom.

Like the C++ code given above, you need to create an EXP2 struct, then link the exp2 pointer in the EXP1 struct to the actual EXP2 struct (sorry, a bit of a mouthful)

You want:

Code:
EXP1 sv;
EXP2 sv_exp2;

int main () {
sv.exp2 = &sv_exp2;
sv.exp2->a = 5 ;
printf("%d",sv.exp2->a) ;

}

Last edited by Xyro; 03-11-2010 at 09:43 AM. Reason: Horrid spelling/grammar
 
Old 03-11-2010, 09:52 AM   #4
is noname
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
thankss guys ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
how to access private pointer of struct net_device??? raklo Linux - Kernel 1 02-24-2007 12:34 AM
how to access struct tty_struct corresponding to current terminal raklo Programming 2 09-03-2006 02:17 PM
access struct member error true_atlantis Programming 4 04-25-2006 05:00 PM
Can't access methods of a struct kornerr Programming 2 01-02-2006 11:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration