1 / 24

Quiz 3

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&lt; stdio.h &gt; void main() { int num = 9; int * numptr = &amp;num; int ** ptr2 = &amp; numptr ; printf(&quot;num is %d<br>&quot;, num);

Télécharger la présentation

Quiz 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Quiz 3

  2. 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\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); • }

  3. 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 . . .

  4. 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]);

  5. #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 . . .

  6. 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; • }

  7. References for pointers • http://home.netcom.com/~tjensen/ptr/pointers.htm • http://boredzo.org/pointers/

  8. String functions • strcat() • strcpy() • strtok()

  9. 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.

  10. 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.

  11. If your input is Kitty, your output is going to be as follow. • Kitty is a cat.

  12. 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; • }

  13. 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; • }

  14. Output • copy - Be the beast • orig - beast

  15. 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); • }

  16. 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"); • }

  17. 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," ."); • } • }

  18. 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," ."); • } • }

  19. 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.

  20. 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.

  21. 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; • }

  22. 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 . . .

More Related