Hi something really strange is happening in the following simple program. The program takes ASCII numbers of 2 digits because i only need to read numbers, the ‘@’ symbol and the ‘.’ symbol. And finds out what is the ASCII symbol for this bytes.
int main(int args, char* argv[])
{
unsigned int ch;
char aux[256];
strcpy(aux,argv[1]);
char a[0];
char buff[50];
char result[512];
int i=0;
while(i lessThan strlen(argv[1]))
{
a[0]=aux[i];
a[1]=aux[i+1];
a[2]='\0';
ch = atoi(a);
printf("el int:%d \n",ch);
sprintf(buff,"%c",ch);
printf("el char: %s \n", buff);
i=i+2;
}
}
Ok this is working and in the variable buff is printing all the ASCII symbols the right way. But as u can see i have a char[] called result that is never used. So i delete it. and i get a different result when i run the program again :O. For some reason the program does not read the ‘.’ character any more 🙁 WHY???? please someone explain me i am scared lol!
my results with variable result declare:
./try3 494650526450
el int:49
el char: 1
el int:46
el char: .
el int:50
el char: 2
el int:52
el char: 4
el int:64
el char: @
el int:50
el char: 2
my results with out variable result declare:
./try3 494650526450
el int:49
el char: 1
el int:0
el char:
el int:50
el char: 2
el int:52
el char: 4
el int:64
el char: @
el int:50
el char: 2
You’re using a zero-length array, a GNU extension. Change
char a[0];tochar a[3];. Declaring another unused char array changes the layout of the memory, that’s why it appears to “work”.