jeudi 28 juillet 2016

Scanning a 2d character array

This code doesn't scan the whole array. It breaks in the middle

# include<stdio.h>
# include<stdlib.h>
int** create(int m)
{
  char **a;
  int i;
  a=(char**)malloc(sizeof(char*)*m);
  for(i=0; i<m; i++)
    *(a+i)=(char*)malloc(sizeof(char)*m);
  return a;
}
int main()
{
  int i,j,n;
  char **a;
  scanf("%d",&n);
  a = create(n);
  for(i=0;i<n;i++)
    for(j=0;j<n;j++)
      scanf("%c",*(a+i)+j);

  printf("Output is n");
  for(i=0;i<4;i++)
    for(j=0;j<4;j++)
      printf("%c",*(*(a+i)+j));
  return 0;
}

input given is n = 4 it scans two line and then prints back that two line... Here is a screenshot of the output Output ScreenShot

Aucun commentaire:

Enregistrer un commentaire