Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. //code to take input of strings in an array of pointers
  2. #include <stdio.h>
  3. #include <strings.h>
  4. int main()
  5. {
  6. //suppose the array of pointers is of 10 elements
  7. char *strings[10],string[50],*p;
  8. int length;
  9. //proper method to take inputs:
  10. for(i=0;i<10;i++)
  11. {
  12. scanf(" %49[^n]",string);
  13. length = strlen(string);
  14. p = (char *)malloc(length+1);
  15. strcpy(p,string);//why use strcpy here instead of p = string
  16. strings[i] = p; //why use this long way instead of writing directly strcpy(strings[i],string) by first defining malloc for strings[i]
  17. }
  18. return 0;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement