C Pointer Crash Course

#include <stdio.h>

int main(void)
{
        int buffer;                                     // An integer are declare
        int *pointer;                                   // A pointer to an integer are declared


        buffer=512;                                     // The buffer are set to 512

        pointer=&buffer;                                // Pointer gets assigned the _adress_ that buffer points at


        printf("Pointer points at [%i]\n",*pointer);    // Print the content of whatever pointer points at
        printf("Pointer contains  [%i]\n",pointer);     // Print the adress that pointer points at
        printf("Buffer  contains  [%i]\n",buffer);      // Print the content of the buffer

        printf("Changing values\n");                    // To demonstrate i change the value of buffer
                                                        // and reprint the values
        buffer=1024;

        printf("Pointer points at [%i]\n",*pointer);    // Print the content of whatever pointer points at
        printf("Pointer contains  [%i]\n",pointer);     // Print the adress that pointer points at
        printf("Buffer  contains  [%i]\n",buffer);      // Print the content of the buffer

        printf("Changing values\n");                    // To demonstrate once again
        *pointer=2048;                                  // We now want to change buffer by using the pointer

        printf("Pointer points at [%i]\n",*pointer);    // Print the content of whatever pointer points at
        printf("Pointer contains  [%i]\n",pointer);     // Print the adress that pointer points at
        printf("Buffer  contains  [%i]\n",buffer);      // Print the content of the buffer


}


Generates following output:

bash-2.05b$ ./a.out 
Pointer points at [512]
Pointer contains  [-1073745188]
Buffer  contains  [512]
Changing values
Pointer points at [1024]
Pointer contains  [-1073745188]
Buffer  contains  [1024]
Changing values
Pointer points at [2048]
Pointer contains  [-1073745188]
Buffer  contains  [2048]


  Whats it good for? Give me some examples!

Very simple example would be a sub-routine that does work on several values and returns more than one result, as follow:
#include <stdio.h>


int multi(ap,bp,cp)   // Function that works on 3 pointers
        int *ap,*bp,*cp;
{
        int temp;

        *ap=*bp + *cp; // add the values of c and b and store the result in a

        temp=*bp;       // Swap bp and bc
        *bp=*cp;
        *cp=temp;


}

int main(void)
{
        // Practical exampel on use of pointers

        int a,b,c;

        a=b=c=0;
        printf("Initated values\n");
        printf("A:[%i] B:[%i] C:[%i]\n",a,b,c);

        a=3;b=2;c=1;                                    // Random test values


        printf("\nRandom test values\n");
        printf("A:[%i] B:[%i] C:[%i]\n",a,b,c);

        multi(&a,&b,&c);                                // Calling function, passing on addresses for a,b and c

        printf("\nAfter multi values\n");
        printf("A:[%i] B:[%i] C:[%i]\n",a,b,c);



}

Generates following output:
bash-2.05b$ ./a.out 
Initated values
A:[0] B:[0] C:[0]

Random test values
A:[3] B:[2] C:[1]

After multi values
A:[3] B:[1] C:[2]

 

Dette indlæg blev udgivet i Knowledge Base, Old Base, Programmering. Bogmærk permalinket.

Skriv et svar