Friday, July 31, 2009

Question in C on structs?

#include %26lt;stdio.h%26gt;














typedef struct fccdev


{


int a1;


int b1;





}m8260;














typedef struct fccchan


{


int a;


int b;





m8260 *d;


constint *c;





}m8260chan;














main()


{





m8260 temp,temp1[2];


m8260chan temp23;





printf("\n test \n ");





temp23.a=9;


temp23.b=9;


temp23.c=8;


temp23.d=7;


temp23.d-%26gt;a1=7;








}





/what is the error in this program

Question in C on structs?
temp23.d=7;





here u r assigning the pointer d a value of 7, so this pointer is now pointing to the memory address 7





temp23.d-%26gt;a1=7;





u r trying to access the memory address 7 here, so the program crashes, why? cause u r not allowed to access that location, it is reserved and protected by the oprating system..





in general, assigning literal values to pointers is very dangerous and will mostly crash ur program.
Reply:You can't assign temp23.d to 7, unless 7 is a pointer to a m8260 structure, which it most certainly is not. The d-%26gt;a1=7 statement will crash.
Reply:WHAT! is this a CADD question?
Reply:you can also post you question at http://www.tekpool.com/forums to get answers from a better suited community
Reply:to begin with you don't know proper convention of variable delcaration and function.


No comments:

Post a Comment