26.09.2023 Views

The C Programming Language - Pointers

This is a free tutorial about pointers from the book "The C Programming Language" by Heimo Gaicher

This is a free tutorial about pointers from the book "The C Programming Language" by Heimo Gaicher

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Pointers</strong>, or rather pointer variables, are also stored under an address in memory.<br />

Depending on the system, pointer variables are at least large enough to store an<br />

address, in this case 4 bytes.<br />

In the following example we access two variables a and b with a char pointer ptr_a<br />

and an int pointer ptr_b and output the addresses and their contents.<br />

/* example 128 – pointers */<br />

#include <br />

int main(void)<br />

{<br />

char a='A', *ptr_a = &a;<br />

int b=1000, *ptr_b = &b;<br />

printf("a = %c = dec %d\n",a,a);<br />

printf("b = %d\n",b);<br />

printf("Address of a = %p\n",ptr_a);<br />

printf("Value of a = %c = %d\n",*ptr_a, *ptr_a);<br />

printf("Address of b = %p\n",ptr_b);<br />

printf("Value of b = %d\n",*ptr_b);<br />

printf("Address of ptr_a = %p\n",&ptr_a);<br />

printf("Address of ptr_b = %p\n",&ptr_b);<br />

printf("Size of ptr_a = %d\n",sizeof(ptr_a));<br />

printf("Size of ptr_b = %d\n",sizeof(ptr_b));<br />

}<br />

return 0;<br />

a = A = dec 65<br />

b = 1000<br />

Address of a = 0060FEFF<br />

Value of a = A = 65<br />

Address of b = 0060FEF4<br />

Value of b = 1000<br />

Address of ptr_a = 0060FEF8<br />

Address of ptr_b = 0060FEF0<br />

Size of ptr_a = 4<br />

Size of ptr_b = 4<br />

<strong>The</strong> C <strong>Programming</strong> <strong>Language</strong> by Heimo Gaicher – Chapter <strong>Pointers</strong> 10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!