Dec 21, 2007

C pointer FAQ :D

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

    while(1)
        {
            // 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]
            p1++;
            p2++;
        }


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

5 comments:

Anonymous said...

Aѕ mentioneԁ, Bitсoіns are gеnerated by users thгοugh the officiаl progгаm.
Often that's what prospects do when you annoy them with your barketing. This Spanish pork sausage is a flavoursome ingredient which is becoming more and more popular, and is versatile enough to suit lots of recipes.

Also visit my web-site :: dominos pizza

Anonymous said...

After its rise in popularity during the 1940s, pizza pie restaurant chains began springing
to life all around the country. The one significant aspect
is that the total number of bitcoins will be capped and
held at a strict and abitrary expansion rate. They built a
hugely successful business on that one competitive difference.



Also visit my blog - check this out

Anonymous said...

Here you can have tremendous savings, as you can learn the whole process without
smashing up an airplane. The reason that you should take a look at
a Joana's Star - Craft 2 guide review is obvious. Because Lemonade Tycoon is flash-based, it can be played by virtually anyone with a web browser. If you are running your simulator for commercial purpose and there are people playing with their own golf clubs, ask them to wipe their clubs with a wet clean piece cloth. Unfortunately, the series has been dormant for quite some time and there isn't any indication that it will be returning anytime soon.

Anonymous said...

Your pouches will degrade after time, so you will have to repair them every once in
a while in the abyss. Now repeat the process by mining and dropping simultaneously.
Once the timeframe reaches 0 to X minutes, the star is about to
land.

Anonymous said...

Whether you are searching for specialty styling products or specific Loreal hair products, you are sure to find what you need.
My hair doesn't always look good when I'm going to attend openings and red carpet events.

The professional hair merchandise of Loreal are effectively currently being
sold all more than the world in about one hundred thirty different countries.