Coupon Accepted Successfully!


Question 1

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';


Question 2

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

This is a trick question :)

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.

Question 3

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

Test Your Skills Now!
Take a Quiz now
Reviewer Name