By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
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
  1. typedef struct{double r;
  2. 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
  1.  struct COMPLEX
  2. {double r;
  3. 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
  1. #include<stdio.h>
  2. #include<math.h>
  3. typedef struct{double r;
  4. double i;} COMPLEX;
  5. COMPLEX c_add(COMPLEX za, COMPLEX zb);
  7. main()
  8. {
  9.     COMPLEX z1, z2, z3, *pz;
  10. /******************************************************************
  12. * Get two complex numbers *
  13. ******************************************************************/
  15. printf("enter the values of z1 and z2:\n z1?:\n");
  16. scanf("%lf%lf",&z1.r, &z1.i);
  17. printf("z2:\n");
  18. scanf("%lf%lf",&z2.r,&z2.i);
  20. pz = &z3;
  21. *pz = c_add(z1,z2);
  22. printf("%lf + %lfi\n",pz->r,pz->i);
  23. }
  24. /****************************************************************** 
  26. * Returns sum of complex numbers za and zb 
  27. ******************************************************************/
  28. COMPLEX c_add(COMPLEX za, COMPLEX zb)
  30. COMPLEX sum; 
  32. sum.r = za.r + zb.r;
  33. sum.i = za.i + zb.i;
  34. return (sum);
  36. }
Is it possible for this code or not?need help please..
Oct 12 '08 #1
Share this Question
Share on Google+
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,

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
  1. typedef struct complex {
  2.    double r;
  3.    double i;
  4. } 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

Post your reply

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