Jan 7, 2008

A C pointer FAQ ( kind of ) [ as of now for me ]

Of course, pointers are not limited to ints. It's quite common to use pointers to other types, especially char. Here is the innards of the mystrcmp function we saw in a previous chapter, rewritten to use pointers. (mystrcmp, you may recall, compares two strings, character by character.)

 // p1, p2 point to the address of the first character in str1, str2 respectively
 char *p1 = &str1[0], *p2 = &str2[0];

   // compare the values of str1[x], str[x] at the position x. Not equal
   if(*p1 != *p2)
    return *p1 - *p2;
   // we reached the end of both str1, str2 -> they are equal.
   if(*p1 == '\0' || *p2 == '\0')
    return 0;
   // p1++ and p2++ do the increment of p1 and p2.
   // i.e., the next characters in str1 ( str[x + 1] and str2 ( str2[ x + 1]

As another example, here is the strcpy (string copy) loop from a previous chapter, rewritten to use pointers:

 char *dp = &dest[0], *sp = &src[0];
 while(*sp != '\0')
  // *dp++ means *(dp++), i.e., the value of the pointer next to dp.
  // to access the address of the next pointer of dp, use (*dp)++
  *dp++ = *sp++;
 *dp = '\0';

(One question that comes up is whether the expression *dp++ increments p or what it points to. The answer is that it increments p. To increment what p points to, you can use (*dp)++.) 

ref: http://www.eskimo.com/~scs/cclass/notes/sx10b.html

Os type detection with uname

[vuhung@test_make]$make vh
[vuhung@test_make]$cat Makefile 
uname=$(shell uname)

ifeq ($(uname), Linux)

ifeq ($(uname), SunOS)

        @echo $(APP_OSTYPE)