Welcome, %1$s. Please login or register.

December 12, 2017, 09:55:03 AM
: 1
: Whats the problem with the code?  ( 2876 )
« : July 01, 2007, 11:36:41 AM From Poonam»



Whats the problem with the code?


Struct Color
{
          float r, g, b, a;
};

#define MAKECOLOR(c, r, g, b, a) \
          (c).r = r; \
          (c).g = g; \
          (c).b = b; \
          (c).a = a;

Do you see any problem in the code?

 
Liked It? Share it!

              


« #1 : August 09, 2007, 01:04:31 PM From eisenham»

As long as u define an instance of Color and then call MAKECOLOR there is no problem.

e.g. struct Color c1;
  MAKECOLOR (c1,r,g,b,a) .
« #2 : August 09, 2007, 01:34:03 PM From Poonam»

Assume that the parameters passed to the macro are proper. There is a problem in the macro itself. What is it?
« #3 : August 09, 2007, 03:27:03 PM From eisenham»

Here the semicolon ';' is treated a end of statement.
we can write this code as
Struct Color
{
          float r, g, b, a;
};

#define MAKECOLOR(c, r, g, b, a)  (c).r = r,(c).g = g, (c).b = b,(c).a = a;
« #4 : August 09, 2007, 06:17:02 PM From Poonam»

Semicolon shouldn't be a problem as we are using the '\' at the end of each line (except the last) as line continuation.

Assume that enter is pressed next to '\' ( i.e. '\n' exists right after '\' ).
Eisenham, you are closer to the solution [:)]
« #5 : November 19, 2007, 07:35:16 PM From iammilind»

If we want to use macro, we should use like following:

struct Color s={0,0,0,0,},x;
MAKECOLOR(x,s.r,s.g,s.b,s.a);

we expect that MAKECOLOR will be converted to,
x.r = s.r;
x.g = s.g;
x.b = s.b;
x.a = s.a;

but due to hardwire rule in Macro, it will be converted to following,
x.s.r = s.r;
x.s.g = s.g;
x.s.b = s.b;
x.s.a = s.a;

So compiler will throw an error, that there is no member named 's' in structure Color. Intended object 's' of struct Color will be seen as a member of struct Color.

However, there will be no compilation error until this macro is used !!



« #6 : January 10, 2008, 01:27:49 PM From thequark»

Right, a simpler example would be:

struct Color c;
MAKECOLOR(c,0,0,0,0)

which will make the preprocessed output as:

(c). 0 = 0; (c). 0 = 0; (c). 0 = 0; (c). 0 = 0;
: 1
« previous next »

 

Best RatedList All>>



Latest
Random



SMF 2.0.10 | SMF © 2015, Simple Machines | Contact Webmaster | OnlineFunDb.com © 2009/10 | Legal Disclaimer