c - to input data to structure member is a pointer -


#include<stdio.h> #include<stdlib.h> struct test {     int x;     int *y; };  main() {     struct test *a;     = malloc(sizeof(struct test));      a->x =10;     a->y = 12;     printf("%d %d", a->x,a->y); } 

i o/p there warning

 warning: assignment makes pointer integer without cast 

and

 warning: format ‘%d’ expects type ‘int’, argument 3 has type ‘int *’ 

how input value *y in struct test

to access, need dereference pointer returned expression a->y maniputlate pointed @ value. this, use unary * operator:

you need allocate memory y make sure points @ something:

a->y = malloc(sizeof(int)); ... *(a->y) = 12; ... printf("%d %d", a->x,*(a->y)); 

and sure free malloc'd data in reverse order malloc'd

free(a->y); free(a); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -