# 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 
Pointer contains  [-1073745188]
Buffer  contains  
Changing values
Pointer points at 
Pointer contains  [-1073745188]
Buffer  contains  
Changing values
Pointer points at 
Pointer contains  [-1073745188]
Buffer  contains  

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: B: C:

Random test values
A: B: C:

After multi values
A: B: C:```

Dette indlæg blev udgivet i Knowledge Base, Old Base, Programmering. Bogmærk permalinket.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6420742,"math_prob":0.9840144,"size":2524,"snap":"2023-40-2023-50","text_gpt3_token_len":688,"char_repetition_ratio":0.22103174,"word_repetition_ratio":0.30704224,"special_character_ratio":0.3280507,"punctuation_ratio":0.17857143,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9819989,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-02T09:15:07Z\",\"WARC-Record-ID\":\"<urn:uuid:e977b453-e5ea-46ed-82e0-bff68db03a2e>\",\"Content-Length\":\"47761\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:376aaced-ccac-457c-9bbc-567fc6c2abfe>\",\"WARC-Concurrent-To\":\"<urn:uuid:aafcc4fb-1408-49a2-97da-11eedd140a83>\",\"WARC-IP-Address\":\"62.69.153.244\",\"WARC-Target-URI\":\"https://www.unifix.org/2005/06/17/c-pointer-crash-course/\",\"WARC-Payload-Digest\":\"sha1:VAGALMEUCJLBBWJFG6FO6TOHYLPS2E32\",\"WARC-Block-Digest\":\"sha1:YHJXMJHLENWLDP55CODSSWJZZNSSY7TM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100381.14_warc_CC-MAIN-20231202073445-20231202103445-00553.warc.gz\"}"}