Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the...

29
Pointers

description

Addresses in Memory Preceding a variable name by an ampersand, known as the address operator, will return its address: #include int main(void) { int x; /* notice the format specifier in printf() for an address is %p */ printf("The address for the memory allocated to x is %p ", &x); return 0; }

Transcript of Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the...

Page 1: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointers

Page 2: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Addresses in Memory

•Everything in memory has an address. C allows us to obtain the address that a variable is stored at.

•scanf() is an example using the address of a variable ▫scanf("%d", &year);

Page 3: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Addresses in Memory

• Preceding a variable name by an ampersand, known as the address operator, will return its address:

• #include <stdio.h> • int main(void) • { • int x; • /* notice the format specifier in printf() for an • address is %p */ • printf("The address for the memory allocated to x is %p\n", &x); • return 0; • }

Page 4: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

•hexadecimal (or called base-16) 0123456789ABCDEF

•The address for the memory allocated to x is 0012FF60 in the previous example.

•For 32 bits system the 0012FF60 hexadecimal will be converted to 32 bits binary (one hex character represented by 4 bits).

Page 5: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointer• A pointer is a variable whose value is a memory

address. • Note the type of a pointer indicates the type of

variable which it points to. ▫E.g., int * (called pointer-to-int type) should be

initialized to point to a variable of type int. ▫Similarly, we have char*, float *, ……

• Given a pointer variable, assignment can be done by using: ▫int * ptr = &pooh; /*assigns pooh’s address to ptr, we

say ptr points to pooh*/ ▫ptr = &bah; /*make ptr point to some other

variables*/

Page 6: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

• #include<stdio.h> • int main(void) • { • int num = 3, num1 = 5; • int* numptr; /* numptr is a pointer */ • printf("content of num is %d\n", num); • printf("address of num is %p\n", &num); • printf("address of num1 is %p\n", &num1); • numptr = &num; /* initialize numptr with the address of

num */ • printf("content of numptr is %p\n", numptr); • numptr = &num1; • printf("content of numptr is %p\n", numptr); • return 0; • }

Page 7: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

• #include<stdio.h> • int main(void) • { • int num = 3, num1 = 5; • int* numptr; /* numptr is a pointer */ • printf("content of num is %d\n", num); • printf("address of num is %p\n", &num); • printf("address of num1 is %p\n", &num1); • numptr = &num; /* initialize numptr with the address of num

*/ • printf("content of numptr is %p\n", numptr); • numptr = &num1; • printf("content of numptr is %p\n", numptr); • return 0; • }

Output: content of num is 3address of num is 0012FF60address of num1 is 0012FF54content of numptr is 0012FF60content of numptr is 0012FF54Press any key to continue . . .

Page 8: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Why pointers•C was developed when computers were

much less powerful•The raw ability to work with particular

memory locations was obviously a useful option to have.

•Programming microcontrollers still need this.

•Optimize a program to run faster or use less memory that it would otherwise.

Page 9: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

• #include<stdio.h>• int main(void)• {• int arr[3] = {2, 4, 6}, i;• for(i = 0; i<3; i++)• printf("The address of %d element is %p.\n",

i, &arr[i]);• return 0;• }

• The address of 0 element is 0032FE08. • The address of 1 element is 0032FE0C. • The address of 2 element is 0032FE10.

Page 10: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Indirection operator•Pointers allow us to modify content in

memory by using indirection operator (or called dereference operator).

•Putting an asterisk before your pointer variable

•See the example in the next slide

Page 11: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

What’s going here?•#include <stdio.h>•int main(void)•{• int bah = 10, val;• int* ptr = &bah;• val = *ptr;• printf("The value of val is %d.\n", val);• return 0;•}

Page 12: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

PointersWe can use pointers in much the same way we do the variables that

they point to.

• #include <stdio.h>• int main(void)• {• int a = 3, b = 3; /* a and b start with equal values */• int* bptr = &b; /* we’ll modify b using a pointer */• a *= 4;• *bptr *= 4;• printf("a is %d, b is %d\n", a, b);• a--;• (*bptr)--; /* parentheses are necessary here to override the

order of precedence */• printf("a is %d, b is %d\n", a, b);• return 0;• }

Page 13: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

•Output:•a is 12, b is 12•a is 11, b is 11

Page 14: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Define multiple pointers together• Wrong! If you use int * a, b to define two integer

pointer variables. Instead, you should use int * a, *b;

• #include<stdio.h>• int main(void)• {• int * a, *b;//it’s wrong if int *a,b• int c = 10;• b = &c;• a = &c;• printf("b: %p and a: %p\n", b, a);• return 0;• }

Page 15: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointers to Pointers

Pointers can contain the address of another pointer. • int main(void) • { • int num = 5; • int* numptr = &num; • int** ptr2 = &numptr; /* notice the two asterisks */ • printf("num is %d\n", num); • printf("*numptr is %d\n", *numptr); • printf("The content in numptr is %p.\n", numptr); • printf("*ptr2 is %p\n", *ptr2); • printf("**ptr2 is %d\n", **ptr2); • return 0; • }

Page 16: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Output

•num is 5 •*numptr is 5 •The content in numptr is 0012FF60 •*ptr2 is 0012FF60 •**ptr2 is 5

Page 17: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Comparing Pointers

•Note the difference between comparing the contents of pointers and the variables that pointers point to.

•To compare the addresses stored in pointers, use ▫if(numptr == valptr)

•To compare the values of the variables that pointers point to, use ▫if(*numptr == *valptr)

Page 18: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointers and Functions•Previously, we made function calls like

this: ▫int x = 3; ▫int y; ▫y = do_something(x);

•In this case, a copy of the variable’s value are passed to the function in a process called pass by value.

Page 19: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointers and Functions

•Passing pointers to a function will allow us to change the value of the original variable, called pass by reference,

•We do this by passing the variable’s address to the function.

•We already know passing array to a function is pass by reference.

•What’s the association between array and pointer?????

Page 20: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointers and Functions• #include<stdio.h> • void interchange(int * u, int * v); • int main(void) • { • int x = 5, y = 10; • printf("Originally x = %d and y = %d.\n", x,y); • interchange(&x, &y); • printf("Now x = %d and y = %d.\n", x,y); • return 0; • } • void interchange(int * u, int * v) • { • int temp; • temp = *u; • *u = *v; • *v = temp; • }

Page 21: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointers and Functions• #include <stdio.h> • void tripleNum(int* aptr); • int main(void) • {

int num = 8; int* numptr = &num; printf("before the function call, num is %d\n", num); tripleNum(numptr); /*or simply use &num*/ printf("after the function call, num is %d\n", num);

• } • void tripleNum(int* aptr) /* pass by reference */ • {

*aptr = 3 * *aptr; /* first asterisk is for multiplication, second is to dereference the pointer */

• }

Page 22: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Array review

•We have already learned how to work with arrays using subscript notation.

•Example: ▫float myData[] = {3.5, 4.0, 9.34}; ▫myData[0] += 2; ▫printf("myData[0] is %f\n", myData[0]);

Page 23: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointers and Arrays•The array name evaluates to the address

of the first element in the array. • int data[] = {5, 6, 7}; • int* dataptr = data; /* notice that we

don’t use the ampersand here */ • int* firstptr = &data[0]; /* we use & here

since data[0] evaluates to a number */ •printf("*dataptr is %d, *firstptr is %d\n",

*dataptr, *firstptr);

Page 24: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointer Arithmetic •Since arrays consist of contiguous

memory locations, we can increment (or decrement) the addresses to move through the array.

• int data[] = {5, 6, 7}; • int i; • for (i = 0; i < 3; i++) •printf("the value at address %p is %d\n",

(data + i), *(data + i));

Page 25: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointer Arithmetic•We can use pointer arithmetic with

pointers to non-array ▫int data[] = {5, 6, 7}; ▫int* dataptr = data; ▫printf("pointer address = %p, next address

= %p\n", dataptr, dataptr + 1);

Page 26: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Pointer Arithmetic• #include <stdio.h> • int main(void) • { • char chararray[] = {68, 97, 114, 105, 110}; /* 1 byte

each */ • int intarray[] = {10, 11, 12, 13, 14}; /* 4 bytes each */ • int i; • printf("chararray intarray\n"); • printf("-------------------\n"); • for(i = 0; i < 5; i++) • printf("%p, %p\n", (chararray + i), (intarray + i)); • return 0; • }

Page 27: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

•chararray intarray•-------------------•0012FF58, 0012FF3C•0012FF59, 0012FF40•0012FF5A, 0012FF44•0012FF5B, 0012FF48•0012FF5C, 0012FF4C•Press any key to continue . . .

Page 28: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

Array of pointersWe can have an array whose members are pointers,

in this example pointers-to-int.

• int* data[3]; • int i; • int x = 5; • int y = 89; • int z = 34; • data[0] = &x; • data[1] = &y; • data[2] = &z; • for(i = 0; i < 3; i++) • printf("%d\n", *data[i]);

Page 29: Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address…

•#include<stdio.h> •#include<string.h> • int main() •{ • printf("The address of the string is %p.\

n", "apple"); • printf("The length of the string is %d.\n",

strlen("Hello")); • return 0; •}

Output: The address of the string is 00415744.The length of the string is 5.Press any key to continue . . .