Pointers

Memory 0x0

0x1

0x2

0x3

0x4

0x5

0x6

0x7

0x8

0x9

0xA

0xB

Creating Pointers Declaring pointers: * Examples: int* x; char* y; float* z;

Referencing and Dereferencing

Referencing: & Dereferencing: *

Under the hood...

int x = 5; int* ptr = &x; int copy = *ptr;

Variable

Address

Value

x

0x04

5

ptr

0x08

0x04

copy

0x0C

5

Track the values

x

ptr

int x = 5;

5

int* ptr = &x;

5

&x

*ptr = 35;

35

&x

Test Yourself a 1. 2. 3. 4. 5. 6. 7.

b

c

pa pb pc

Answers int a = 3, b = 4, c = 5; int* pa = &a, *pb = &b, *pc = &c;

a

b

c

pa pb pc

a = b * c;

20

4

5

&a

&b

&c

a *= c;

100

4

5

&a

&b

&c

b = *pa;

100

100

5

&a

&b

&c

pc = pa;

100

100

5

&a

&b

&a

*pb = b * c;

100

500

5

&a

&b

&a

c = (*pa) * (*pc);

100

500

10000

&a

&b

&a

50000

500

10000

&a

&b

&a

*pc = a * (*pb);

Pointer Arithmetic Adding/subtracting n adjusts the pointer by n * sizeof() bytes x

y

int x = 5;

5

int* y = &x;

5

0x04

y += 1;

5

0x08

What will print? int main(void) { char* str = "happy cat"; int counter = 0; for (char* ptr = str; *ptr != '\0'; ptr++) { counter++; } printf("%d\n", counter); }

Pointers and Arrays

int array[3]; *array = 1; *(array + 1) = 2; *(array + 2) = 3;

0

1

1

2

2

3

Pointers

0x0. 0x1. 0x4. 0x5. 0x6. 0x9. 0x8. 0xA. 0xB. 0x2. 0x7. 0x3. Memory. Page 3. Page 4. Creating Pointers. Declaring pointers: ... What will print? int main(void). { char* str = "happy cat"; int counter = 0; for (char* ptr = str; *ptr != '\0'; ptr++). { counter++;. } printf("%d\n", counter);. } Page 12. Pointers and Arrays int array[3];. *array = 1;.

121KB Sizes 1 Downloads 89 Views

Recommend Documents

Needlepoint-Needle-Pointers-XXVI-Number-PDF-fced93462.pdf ...
Needlepoint-Needle-Pointers-XXVI-Number-PDF-fced93462.pdf. Needlepoint-Needle-Pointers-XXVI-Number-PDF-fced93462.pdf. Open. Extract. Open with.

Visual stability based on remapping of attention pointers
Object files: A temporary representation combining an object's identity, ..... target's feature data are to be found in earlier visual cortices which are shown, highly.

Collaborative Voting with Mice and Laser Pointers in ...
image using laser pointers in response to 3D demo videos. This interaction was ... In the software industry, such discussions ..... American Medical Informatics.

pointers on c by kenneth reek free download pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. pointers on c by kenneth reek free download pdf. pointers on c by kenneth reek free download pdf. Open. Extr