I have this code that work:
int main()
{
char a[100];
int i;
int n=3;
for(i=1;i<=n;i++){
printf("insert char \n");
scanf("%c",&a[i]);
getchar();
}
for(i=1;i<=n;i++){
printf("%c ",a[i]);
}
}
I insert a b c he prints a b c, so far so good, I would like to be able to insert the size n with a scanf, but if I put a scanf of n like this:
int main()
{
char a[100];
int i;
int n;
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("insert char \n");
scanf("%c",&a[i]);
getchar();
}
for(i=1;i<=n;i++){
printf("%c ",a[i]);
}
}
when I run the program, it causes problems in the second for loop, I enter a b c, and the second for returns empty characters,actually does newline n times,in C when each program finishes "PROCESS RETURNED etc." is printed. When I run the first code the characters are printed on the same line and in the next line there is "PROCESS RETURNED", instead with the second code after the insertion n empty lines are printed and then "PROCESS RETURNED" even if I have not written \ n in the printf. I tried to see if n is stored in the same location as some element of a (with printf ("% p", & a [i]), or printf ("% p", & a) and printf ("% p", & n )), but the address is different. Some idea?