426,133 Members | 1,092 Online
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Need help about changing typedef in structures..C programming help

 P: 17 hi mate.. i want to change my struct text in Expand|Select|Wrap|Line Numbers typedef struct{double r; double i;} COMPLEX; assume i want to change it to normal one without the typedef thing and do it like this Expand|Select|Wrap|Line Numbers  struct COMPLEX {double r; double i;}; ] now i got errors when i want to convert my codes here... can anyone help me if i want to change the first to the second in my following codes Expand|Select|Wrap|Line Numbers #include #include typedef struct{double r; double i;} COMPLEX; COMPLEX c_add(COMPLEX za, COMPLEX zb);   main() {     COMPLEX z1, z2, z3, *pz; /******************************************************************   * Get two complex numbers * ******************************************************************/   printf("enter the values of z1 and z2:\n z1?:\n"); scanf("%lf%lf",&z1.r, &z1.i); printf("z2:\n"); scanf("%lf%lf",&z2.r,&z2.i);   pz = &z3; *pz = c_add(z1,z2); printf("%lf + %lfi\n",pz->r,pz->i); } /******************************************************************    * Returns sum of complex numbers za and zb  ******************************************************************/ COMPLEX c_add(COMPLEX za, COMPLEX zb) {    COMPLEX sum;    sum.r = za.r + zb.r; sum.i = za.i + zb.i; return (sum);   } Is it possible for this code or not?need help please.. Oct 12 '08 #1
4 Replies

 Expert 10K+ P: 11,448 If you remove the typedef the name of your struct type is just 'struct COMPLEX'. You have to set the type of your complex variables to 'struct COMPLEX' instead of just 'COMPLEX'. Why you don't want to use a simple typedef is beyond me. note that in C++ you *can* use 'COMPLEX' instead of 'struct COMPLEX'. kind regards, Jos Oct 12 '08 #2

 P: 79 Is it possible for this code or not?need help please.. Yes it is. Write struct before COMPLEX everytime a COMPLEX structure is declared(meaning in lines 9 and 31 write: struct COMPLEX ...). Oct 12 '08 #3

 P: 17 thanks for the reply..i was just experimenting if it is possible..well i am trying the hard way i guess.will inform ya if this code conversion works.. Oct 12 '08 #4

 Expert 100+ P: 2,398 I don't understand what you're trying to accomplish. Would the following be useful to you? Expand|Select|Wrap|Line Numbers typedef struct complex {    double r;    double i; } COMPLEX; The (dis)advantage here is that you can refer to either 'struct complex' or 'COMPLEX'. Using two names for the same type is usually a bad idea because it can really confuse anybody who tries to understand the code. In this case, the (dis)advantage is that the program will continue to work as you implement the conversion from COMPLEX to 'struct complex'. On the other hand, without the prompting of compiler warnings you might miss a few locations that need to change. Oct 12 '08 #5