I want to get a string by calling function getchar()
, but there is something wrong. Below is my code
int i, j, t;
char a[N], *p = argv[1];
for (i=0; i<5; a[i]=t, i++){
if ((t = getchar()) == EOF) break;
}
a[i] = 0;
I want to get a string by calling function getchar()
, but there is something wrong. Below is my code
int i, j, t;
char a[N], *p = argv[1];
for (i=0; i<5; a[i]=t, i++){
if ((t = getchar()) == EOF) break;
}
a[i] = 0;
Your code depends on the value of N
. So, change your code like
#define N 20 //my assumption
int i, t;
char a[N]; // *p = argv[1]; no relevant for this example
for (i=0; i < N-1; i++){ //check here
if ((t = getchar()) == EOF) break;
a[i]=t;
}
a[i] = 0;
Remeber, array index in C
is 0
based.