Friday, August 30, 2013

What is the difference between the declaration and the definition of a variable?


The definition is the one that actually allocates space, and provides an initialization
value, if any.

There can be many declarations, but there must be exactly one definition. A definition
tells the compiler to set aside storage for the variable. A declaration makes the
variable known to parts of the program that may wish to use it. A variable might
be defined and declared in the same statement.

What is the difference between enumeration variables and the preprocessor #defines?


Functionality                                                 Enumerations                       #defines

Numeric values assigned automatically?             YES                            NO
Can the debugger display the symbolic values?     YES                            NO
Obey block scope?                                               YES                               NO
Control over the size of the variables?               NO                            NO

Is char a[3] = "abc"; legal?

It declares an array of size three, initialized with the three characters 'a', 'b',
and 'c', without the usual terminating '\0' character. The array is therefore not
a true C string and cannot be used with strcpy, printf %s, etc. But its legal. 

Declare an array with only one element and still access elements beyond the first element (in a valid fashion)?

There is a way to do this. Using structures.


struct mystruct {
  int  value;
  int length;
  char string[1];
};


Now, when allocating memory to the structure using malloc(), allocate more memory
than what the structure would normally require!. This way, you can access beyond
string[0] (till the extra amount of memory you have allocated, ofcourse).

But remember, compilers which check for array bounds carefully might throw warnings.
Also, you need to have a length field in the structure to keep a count of how big
your one element array really is :).

A cleaner way of doing this is to have a pointer instead of the one element array
and allocate memory for it seperately after allocating memory for the structure.



struct mystruct {
  int  value;
 char *string; // Need to allocate memory using malloc() after allocating memory
for the strucure.
};

What is the difference between char *a and char a[]?

There is a lot of difference!


char a[] = "string";
char *a = "string";


The declaration char a[] asks for space for 7 characters and see that its known by
the name "a". In contrast, the declaration char *a, asks for a place that holds a
pointer, to be known by the name "a". This pointer "a" can point anywhere. In this
case its pointing to an anonymous array of 7 characters, which does have any name
in particular. Its just present in memory with a pointer keeping track of its location.




char a[] = "string";

   +----+----+----+----+----+----+------+
a: | s  |  t |  r |  i |  n |  g | '\0' |
   +----+----+----+----+----+----+------+
   a[0] a[1] a[2] a[3] a[4] a[5] a[6]


char *a = "string";

+-----+          +---+---+---+---+---+---+------+
|  a: | *======> | s | t | r | i | n | g | '\0' |
+-----+          +---+---+---+---+---+---+------+
Pointer          Anonymous array



It is curcial to know that a[3] generates different code depending on whether a is
an array or a pointer. When the compiler sees the expression a[3] and if a is an
array, it starts at the location "a", goes three elements past it, and returns the
character there. When it sees the expression a[3] and if a is a pointer, it starts
at the location "a", gets the pointer value there, adds 3 to the pointer value, and
gets the character pointed to by that value. 

If a is an array, a[3] is three places past a. If a is a pointer, then a[3] is three
places past the memory location pointed to by a. In the example above, both a[3]
and a[3] return the same character, but the way they do it is different! 

Doing something like this would be illegal.


char *p = "hello, world!";
p[0] = 'H';