Download presentation
Presentation is loading. Please wait.
Published byAlexandrina McBride Modified over 8 years ago
1
Pointers
2
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));
3
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);
4
Pointer Arithmetic #include 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; }
5
chararray intarray ------------------- 0012FF58, 0012FF3C 0012FF59, 0012FF40 0012FF5A, 0012FF44 0012FF5B, 0012FF48 0012FF5C, 0012FF4C Press any key to continue...
6
Array of pointers 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]);
7
#include 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...
8
Quiz 3
9
#include void main() { int num = 8; int* numptr = # int** ptr2 = &numptr; 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); (*numptr)++; printf("*numptr is %d\n", *numptr); (**ptr2)-=3; printf("**ptr2 is %d\n", **ptr2); (numptr)++; printf("The content in numptr is %p.\n", numptr); } What does this program print? (10 points) Assumptions: Address of num is 208 Address of numptr is 220 Address of ptr2 is 232
10
Solution num is 8 *numptr is 8 The content in numptr is 208. *ptr2 is 208 **ptr2 is 8 *numptr is 9 **ptr2 is 6 The content in numptr is 212. Press any key to continue...
11
Passing the address after the last elements in the array #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; }
12
2D Array int score[4][2]; score, being the name of an array, is the address of the first element of the array. The first element of score is an array of two integers, so score is the address of an array of two integers. ▫That is score has the same value as &score[0]. Since score[0] is itself an array of two integers, so score[0] has the same value as &score[0][0], the address of its first element. Physically, both score and score[0] have the same address location.
13
2D Array Adding 1 to a pointer or address yields a value larger by the size of the object referred to. In this respect, score and score[0] differ, because score refers to 4 arrays containing two integers each. score[0] refers to one array of 2 integers. Therefore, score + 1 has a different value from score[0] + 1.
14
2D Array #include int main(void) { int score[4][2] = {{0,1},{2,3},{4,5},{6,7}}; printf("The value of score is %p.\n", score); printf("The value of score[0] is %p.\n", score[0]); printf("The value of &score[0][0] is %p.\n", &score[0][0]); printf("The value of score+1 is %p.\n", score+1); printf("The value of score[0]+1 is %p.\n", score[0]+1); return 0; }
15
References for pointers http://home.netcom.com/~tjensen/ptr/pointers.htm http://boredzo.org/pointers/
16
String functions strcat() strcpy() strtok()
17
strcat() 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.
18
Strcat() cont. #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!!! strcat( ) not checking whether the first array is large enough to hold the second string.
19
If your input is Kitty, your output is going to be as follow. Kitty is a cat.
20
Strcpy() #include int main(void) { char orig[] = "beast"; char copy[80] = "Be the best that you can be."; strcpy(copy, orig); printf("copy - %s\n", copy); printf(“orig - %s\n", orig); return 0; }
21
Strcpy() #include int main(void) { char orig[] = "beast"; char copy[80] = "Be the best that you can be."; strcpy(copy+7, orig); printf("copy - %s\n", copy); printf(“orig - %s\n", orig); return 0; }
22
Output copy - Be the beast orig - beast
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.