Member Avatar for I_m_rude

hi,
1. actually i was reading some text from my book and i come across this expression. I am not getting what it means. here it goes,

typedef int (*HIG) (int,int); And

"sizeof" can't be used in #if because the preprocessor does not parse type names. What this statement means ?

Can u please explain what it means?

thanks.

  1. typedef int (*HIG) (int,int);

    • a pointer to a function returning an integer.
      In this HIG is pointer to function which taking two arguments first as int and second as int type. And the function HIG return int type.
  2. Is there anyway to use a "sizeof" in a pre-processor macro?
    No. The conditional directives take a restricted set of conditional expressions; sizeof is one of the things not allowed.
    Preprocessing directives are evaluated before the source is parsed (at least conceptually), so there aren't any types or variables yet to get their size.

Member Avatar for I_m_rude

@perry31
You have explained what HIG is! but, what typedef is doing here in this expression ?
typedef defines new names for the data types. so what is the old data name and what is the new data name in this ?
For ex: typedef int Length
In this, Length is new name for int. so what is new and old name in the above asked expression ?

Secondly,
ok,i understand the second point. can you tell me the other conditions like sizeof, which can't come in preprocessor ? Also, but sizeof is valid in #define statements althoguh it is also preprocessor. how is it possible then ?

For the typedef, the older name would be

int Length;

after giving the user defined name like

typedef int Length newLength;

instead of using int Length, now you are able to use newLength.

If the typedef is like the one you state

typedef int Length;

then the int type variables can be represented with the user defined name which is Length.
For example:

Length value;
Length len[];

this is referring to the int type which you have defined.

    typedef int (*HIG) (int,int);

In this statement HIG is the name of the new type, the type it is defined to is int (*)(int,int). This statement is defining a type that is a point to a function that returns int and takes 2 integers as parameters, as always the syntax for creating a typedef is exactly the same as the syntax for declaring a variable of that type, that is

    int (*fnPtr) (int,int);

and

    typedef int (*HIG) (int,int);
    HIG fnPtr;

are functionally equivilent, they both create a variable, fnPtr, with a type of int (*) (int,int).

You can't use sizeof in #if because #if is evaluated completely by the preprocessor (which runs first) but sizeof is evaluated by the compiler (which runs second).

However you can use sizeof in #define because the processors evaluation of #define is to set-up a text substitution so it you have a definition like this

    #define SIZE(x) (sizeof(x))

the proprocess replaces every occurance of SIZE it finds in the code with what is in the rest of the macro definition (sizeof in this case) with out actually evaluating the new text it has inserted. The compiler comes along later and evaulates the code so the compiler never sees the SIZE but does see and evaluate the sizeof.

Member Avatar for I_m_rude

oh! really awesome explaination. I must say that this type of explaination can't be get by anyone, even after searching whole google.com. HATS off! thanks alot to you sir/mam. really thanks.

Hey nitin resolve this post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.