Sunday, 21 May 2017

C

 void pointer
A void pointer is a special pointer type which can point to any data type without letting the compiler know. It helps to pass a pointer to some function of any type which can be decided at run time. In the following example input parameter a and b can be both integer and string.
Void PointerVoid(int type, void *a, void *b, void *c)
{
   if(type == 1)/* int*/
   *c = ((int) *a) + ((int)*b);
   else /*string*/
   sprintf((char*)c,”%s%s”,(char*)a,(char*b));
How to restrict a header file from including more than once?
In C, to avoid double inclusion, we use a include guard also known as macro guard. It is #ifndef - #endif pair. "ifndef" is an indication of “if not defined”.
#ifndef FAMILY_H
#define FAMILY_H
struct Example
{
  int member;
};
#endif /* FAMILY _H */
pack a structure
We can pack any structure using __attribute__((__packed__)) in gcc. Example -
typdef struct A __attribute __((__packed__))
{
char c;
int i;
}B;

No comments:

Post a Comment