Presentation is loading. Please wait.

Presentation is loading. Please wait.

POINTER Dong-Chul Kim BioMeCIS UTA 3/13/2016 1.

Similar presentations


Presentation on theme: "POINTER Dong-Chul Kim BioMeCIS UTA 3/13/2016 1."— Presentation transcript:

1 POINTER Dong-Chul Kim BioMeCIS CSE @ UTA 3/13/2016 1

2 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); Addresses in Memory

3 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\n", &x); return 0; } Address in Memory

4 hexadecimal (or called base-16) 0123456789ABCDEF The address for the memory allocated to x is 0012FF60 in the previous exam. For 32 bits system the 0012FF60 hexadecimal will be converted to 32 bits binary (one hex character represented by 4 bits).

5 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*/ Pointer (1)Data type (2)Initialization (3)Updating

6 #include 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 = # /* 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; }

7 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 Indirection operator

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

9 We can use pointers in much the same way we do the variables that they point to. 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; } Pointers

10 Wrong! int * a, b; Instead, you should use int * a, *b; Define multiple pointers together

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

12 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) Comparing Pointers

13 Repeat again here: you have to initialize a variable before you use it. If there is no specific initialization, we usually initialize a pointer to NULL (in uppercase). Later we can check the value of that pointer to know whether we have pointed it to any variable. If you didn’t initialize your program may not pass the compilation. Initializing Pointers to NULL

14 #include int main(void) { int num = 3; int* numptr; numptr = NULL;/*Try another example without this statement*/ if (numptr != NULL) printf("num is %d\n", *numptr); else printf("Oops. numptr has a value of %p.\n", numptr); return 0; } Initializing Pointers to NULL

15 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. Changes made to the copy do not affect the original value. Pointers and Functions

16 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????? Pointers and Functions

17 #include 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; } Pointers and Functions

18 #include void tripleNum(int*); /* notice the function argument has a type of int * */ int main(void) { int num = 8; int* numptr = # 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 */ } Pointers and Functions

19 #include void fx(int x); void fxptr(int* x); int main(void) { int x = 7; printf("outside: x is at %p\n", &x); fx( x ); fxptr(&x); } void fx(int x) { printf("inside fx(): x is at %p\n", &x); } void fxptr(int* x) { printf("inside fxptr(): x contains %p\n", x); } Difference between copy by value and copy by reference

20 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 %3.1f\n", myData[0]); Arrays, Part 2

21 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); Pointers and Arrays

22 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)); Pointer Arithmetic

23 In the previous example, we also used pointer arithmetic in the line printf("the value at address %p is %d\n", (data + i), *(data + i)); Since data was declared to be an array of ints, the expression (data + i) adds i * sizeof(int) to the address of data to get the location of the next int. This is a reason why we can’t mix types, e.g., point an int* type pointer to a variable of type double. Pointer Arithmetic

24 We can use pointer arithmetic with pointers to non-array int some; int* someptr = &some; int data[] = {5, 6, 7}; int* dataptr = data; printf("pointer address = %p, next address = %p\n", someptr, someptr + 1); printf("pointer address = %p, next address = %p\n", dataptr, dataptr + 1); type variables as well. Pointer Arithmetic

25 Pointer arithmetic handles the task of determining the address of the next element in the array. 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)); Pointer Arithmetic

26 When you use the increment and decrement operators with pointer variables, you need to be careful that we order the dereference and increment/decrement operators properly. ∗, ++ and -- are at the same level of precedence and are evaluated from right to left. The primary issue is whether we wish to increment the address stored in the pointer, or increment the value at the address in the pointer. Incrementing Pointers

27

28 Earlier we learned how to pass the address of a non-array type variable, for example int myValue = 5; someFunct(&myValue); We’ve already been passing the address of the first member of an array when we did something like this: float data[] = {1, 2, 3}; anotherFunct(data); Arrays and Functions, Part 2

29 The subscript style of having 1D arrays as function parameters used square brackets, e.g., void somefunction(int data[]) { } The pointer style of having 1D arrays as function parameters is this: void somefunction(int* data) { }

30 #include int main(void) { int intArray[] = {8, 4, 13, 7}; double doubleArray[] = {93.2, 67.12, 2, 19.03}; int i; int* ptrI = intArray; double* ptrD = doubleArray; for(i = 0; i < 4; i++) printf("%p = %2d, %p = %5.2f\n", &intArray[i], intArray[i], &doubleArray[i], doubleArray[i]); for(i = 0; i < 4; i++) { printf("%p = %2d, %p = %5.2f\n", ptrI, *ptrI, ptrD, *ptrD); ptrI++; ptrD++; }

31 We 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]); Arrays of Pointers

32 Functions, arrays and pointers 2-dimentional arrays and pointers More string functions Outline

33 #include int main(void) { int n = 5; int m = 8; float a1[5];/*Yes*/ float a2[5*2+1];/*Yes*/ float a3[sizeof(int)+1];/*Yes*/ float a4[-4]; float a5[0]; float a6[2.5]; float a7[(int) 2.5];/*Yes*/ float a8[n];/*No for C89, but Yes for after C89*/ return 0; } Define an array of a particular size

34 (1) a fixed array size in your function (2) passing the array size as a second argument (3) passing the address after the last element in the array Function and array size

35 int sum(int *ar) { int i; int total = 0; for (i = 0 ; i < 10; i++) { total += ar[i]; } return total; } (1) a fixed array size in your function

36 int sum(int *ar, int n) { int i; int total = 0; for (i = 0 ; i < n; i++) { total += ar[i]; } return total; } (2) passing the array size as a second argument

37 #include int sump(int * start, int * end); int main(void) { int marbles[] = {20, 14, 23, 54}; int answer; answer = sump(marbles, marbles + 4); printf("The sum of integers in marbles is %d.\n", answer); return 0; } int sump(int * start, int * end) { int total = 0; while(start < end) { total += *start; start++; } return total; } (3) passing the address after the last element in the array

38 #include int sump(int ar[], int n); int main(void) { int marbles[] = {20, 14, 23, 54}; int answer; printf("The size of marbles is %d bytes.\n", sizeof(marbles)); answer = sump(marbles, sizeof(marbles)/sizeof(int)); return 0; } int sump(int ar[], int n) { int i; int total = 0; for (i = 0 ; i < n; i++) { total += ar[i]; } printf("The size of ar is %d bytes.\n", sizeof(ar)); return total; } sizeof( ) for array name in different places

39 Assignment Adding an integer to a pointer Incrementing a pointer Subtracting an integer from a pointer Decrementing a pointer Differencing: different from subtracting an integer from a pointer Pointer operations

40 int main(void) { int marbles[] = {20, 14, 23, 54}; int * ptr1, * ptr2; ptr1 = &marbles[0]; ptr2 = &marbles[3]; printf("There are %d elements between marbles[0] (inclusive) and marbles[3].\n", ptr2-ptr1); return 0; } Differencing of pointers

41 int zippo[4][2]; zippo, being the name of an array, is the address of the first element of the array. The first element of zippo is an array of two ints, so zippo is the address of an array of two int s. That is zippo has the same value as &zippo[0]. Since zippo[0] is itself an array of two integers, so zippo[0] has the same value as &zippo[0][0], the address of its first element, an int. In short, zippo[0] is the address of an int-sized object, and zippo is the address of a two-int-sized object. Because both the integer and the array of two integers begin at the same location, both zippo and zioop[0] have the same numeric value. 2-Dimentional array

42 Adding 1 to a pointer or address yields a value larger by the size of the referred-to object. In this respect, zippo and zippo[0] differ, because zippo refers to an object two ints in size, and zippo[0] refers to an object one int in size. Therefore, zippo + 1 has a different value from zippo[0] + 1. 2-Dimentional array cont.

43 The general form for getting the address of any element of any row is *(array_name + row) + column For example, when we write *(data + 1) + 2, we are saying “add the size of one row to the address of data, get the address of this, then add the size of two elements of a row to this”. The general form for getting the value of any element of any row is *(*(array_name + row) + column) 2-Dimentional array cont.

44 #include int main(void) { int i, j; char* text[4] = {"this", /* text[0] points to this word */ "is", /* text[1] points to this word */ "a", /* text[2] points to this word */ "string"}; /* text[3] points to this word */ char * tmp; for(i = 0; i < 4; i++) { j = 0; while (*(text[i] + j) != '\0') /* print each character */ { tmp = (text[i] + j++); printf("%c: %p ", *tmp,tmp ); /* note the %c */ } printf(" \n"); } return 0; } String array

45 strtok() strcat() strcpy() String functions

46 char *strtok( pointer-to-str1, pointer-to- str2); The strtok() function returns a pointer to the next "token" found in str1 str2 contains the delimiters that determine the token strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL. strtok( )

47 #include int main(void) { char str[] = "12333$q4tqrgtq$4524;lkj;$;lkj"; char delims[] = "$"; char *result = NULL; result = strtok( str, delims); while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims); } return 0; } strtok( ) cont.

48 strcat() for string concatenation Take two strings for arguments A copy of the second string is tacked onto the end of the first, and this combined version becomes the new first string. The second string is not altered. The return type of strcat() is char *, the value of its first argument – the address of the first character of the string to which the second string is appended. strcat( )

49 #include int main(void) { char flower[80]; char addon[] = " is a cat."; scanf("%s", flower); strcat(flower, addon); printf("%s\n",flower); return 0; } Note!!! strcar( ) not checking whether the first array is large enough to hold the second string. strncat( ) (different from strcat( ) ) strncat(bugs, addon, 5) will add the contents of the addon string to bugs, stopping when it reaches 5 additional characters or the null character, whichever comes first. strcat( ) cont.

50 char * strcyp(char * s1, const char * s2) This function copies the string (including the null character) pointed to by s2 to the location pointed to by s1. The return value is s1. The strcyp( ) function has type char *, and it returns the value of its first argument – the address of a character. The first argument need not point to the beginning of an array. strcpy( )

51 #include int main(void) { char orig[] = "beast"; char copy[80] = "Be the best that you can be."; char *ps; ps = strcpy(copy+7, orig); printf("copy - %s\n", copy); printf("ps - %s\n", ps); return 0; } Still, strcpy() doesn’t check whether the first array is large enough to fit the second string. A better way is to use strncpy() function. strcpy( ) cont.


Download ppt "POINTER Dong-Chul Kim BioMeCIS UTA 3/13/2016 1."

Similar presentations


Ads by Google