SHARE
TWEET

Untitled

a guest Jun 16th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top