Advertisement
Guest User

getchar and dynamic arrays

a guest
Feb 29th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(){
  4.     char **ptr;
  5.     ptr=calloc(10,sizeof(char));
  6.     printf("\nEnter the Name : ");
  7.  
  8.     int val, i, j;
  9.     unsigned char temp;
  10.     while((val = getchar())!='\n'){
  11.         printf("flag\n");
  12.         //ptr[i] = (char) val;
  13.         temp = (char) val;
  14.         ptr[i] = &temp; //need to dereference this variable
  15.         i++;            //to assign to dynamic array
  16.     }
  17.     for (j=0; j<i; j++)
  18.     printf("ptr[%d] = %s\n", i, ptr[i]);
  19.     free(ptr);
  20.     return 0;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement