240 likes | 357 Vues
Quiz 3. What does this program print? (10 points) Assumptions: Address of num is 228 Address of numptr is 240 Address of ptr2 is 252. #include< stdio.h > void main() { int num = 9; int * numptr = &num; int ** ptr2 = & numptr ; printf("num is %d<br>", num);
E N D
What does this program print? (10 points) Assumptions: Address of num is 228 Address of numptr is 240 Address of ptr2 is 252 • #include<stdio.h> • void main() • { • int num = 9; • 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); • }
Solution • num is 9 • *numptr is 9 • The content in numptr is 228. • *ptr2 is 228 • **ptr2 is 9 • *numptr is 10 • **ptr2 is 7 • The content in numptr is 232. • Press any key to continue . . .
Array of pointers We can have an array whose members are pointers, in this example pointers-to-int. • int* data[3]; • inti; • 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]);
#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 . . .
Passing the address after the last elements in the array • #include <stdio.h> • int sump(int * start, int * end); • int main(void) • { • intmarbles[] = {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; • }
References for pointers • http://home.netcom.com/~tjensen/ptr/pointers.htm • http://boredzo.org/pointers/
String functions • strcat() • strcpy() • strtok()
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.
Strcat() cont. • #include <stdio.h> • #include <string.h> • 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.
If your input is Kitty, your output is going to be as follow. • Kitty is a cat.
Strcpy() • #include <stdio.h> • #include <string.h> • 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; • }
Strcpy() • #include <stdio.h> • #include <string.h> • 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; • }
Output • copy - Be the beast • orig - beast
Strtok() • #include<stdio.h> • #include<string.h> • void main(){ • char sentence[]="Bill is really cool."; • char *word = strtok(sentence," ."); • printf("%s\n",word); • char *secondword = strtok(NULL," ."); • printf("%s\n",secondword); • }
Strtok() cont. • #include<stdio.h> • #include<string.h> • void main(){ • char sentence[]="Bill is really cool."; • char *word = strtok(sentence,"xz"); • printf("%s\n",word); • char *secondword = strtok(NULL,"xz"); • if(secondword==NULL) • printf(":)\n"); • }
Strtok() cont. • #include<stdio.h> • #include<string.h> • void main(){ • char sentence[]="Bill is really cool."; • char *word = strtok(sentence," ."); • while(word!=NULL) • { • printf("%s\n",word); • word = strtok(NULL," ."); • } • }
Strtok() cont. Is there anything wrong in this code? • #include<stdio.h> • #include<string.h> • void main(){ • char *sentence="Bill is really cool."; • char *word = strtok(sentence," ."); • while(word!=NULL) • { • printf("%s\n",word); • word = strtok(NULL," ."); • } • }
2D Array • intscore[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.
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.
2D Array • #include<stdio.h> • 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; • }
Output • The value of score is 0012FF44. • The value of score[0] is 0012FF44. • The value of &score[0][0] is 0012FF44. • The value of score+1 is 0012FF4C. • The value of score[0]+1 is 0012FF48. • Press any key to continue . . .