By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

structure and pointer

P: 63
#include <stdio.h>

typedef struct exam ex;
struct exam {
int val;
};

int main() {
int aa;
ex user;
ex *ex_ptr;

printf("\n value ");
scanf("%d", &(ex_ptr->val));
ex_ptr = &user;
printf("%d",*(int *)aa= (ex_ptr->val));

}

iam trying to assign the value "val" to the integer aa.
But segmentation fault arises ...pls help
Jun 5 '07 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
On line 14 you are using ex_ptr before you put an address in it:

Expand|Select|Wrap|Line Numbers
  1. scanf("%d", &(ex_ptr->val));
  2.  
Jun 5 '07 #2

P: 63
yes changed it ...but again the same error occurs
Jun 6 '07 #3

gpraghuram
Expert 100+
P: 1,275
HI,
Allocate memory for the pointer and the call scanf.
Expand|Select|Wrap|Line Numbers
  1. ex *ex_ptr=(struct exam*)malloc(sizeof(struct exam)); ///Add this line
  2.  
  3. printf("\n value ");
  4. scanf("%d", &(ex_ptr->val));
  5.  
  6.  
Thanks
Raghuram
Jun 6 '07 #4

P: 63
yes....but still it shows error ...
Jun 12 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
yes....but still it shows error ...
Not when I ran this code:
Expand|Select|Wrap|Line Numbers
  1. ex *ex_ptr=(struct exam*)malloc(sizeof(struct exam)); ///Add this line
  2.  
  3. printf("\n value ");
  4. scanf("%d", &(ex_ptr->val));
  5. printf("You entered %d\n", ex_ptr->val);
  6.  
  7.     }
  8.  
Jun 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.