i want to type a sequence of chars and save them with a temporary array. After that, i want to create the actual array with a certain size with the values of the temporary array. Here is the code:
#include <stdio.h>
int main()
{
char c;
char temp[100];
char array[24];
int i;
char *ptrtemp = temp;
// create a temporary array with getchar function
while(1) {
c = getchar();
if(c == '\n')
break;
*ptrtemp = c;
i++;
ptrtemp++;
}
// type wrong in case of wrong size
if(i != 24) {
printf("Data is wrong");
exit(0);
}
char *ptrarray = array;
char *ptrtemp2 = temp;
// create the actual array
for(i=0;i<24;i++) {
*ptrarray = *ptrtemp2;
if(i == 23)
break;
ptrarray++;
ptrtemp2++;
}
//printing the actual array
printf("\n%s\n", array);
}
However, I get interesting elements after the actual sequence. The array's size was stated as 24 but 25th, 26th, 27th etc. elements are being also printed.
Every time I try, I see different extra chars. Can anyone explain what is happening here?