J
j_hop_97
I'm hoping someone can explain the behavior of the following program.
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 4
int main(void)
{
int idx;
unsigned char bit[SIZE];
char tmpstr[1];
/* initialize the array*/
for (idx = 0;idx < SIZE; idx++) {
bit[idx] = (unsigned char)idx + 1;
}
printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}
printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}
strcpy(tmpstr, "0"); // why does this have any affect?
printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}
return 0;
}
Makefile:
CC = gcc
CFLAGS = -Wall
all: test
test: test.c
$(CC) $(CFLAGS) -o $@ $<
clean:
rm -f *.o test
..PHONY: clean
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 4
int main(void)
{
int idx;
unsigned char bit[SIZE];
char tmpstr[1];
/* initialize the array*/
for (idx = 0;idx < SIZE; idx++) {
bit[idx] = (unsigned char)idx + 1;
}
printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}
printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}
strcpy(tmpstr, "0"); // why does this have any affect?
printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}
return 0;
}
Makefile:
CC = gcc
CFLAGS = -Wall
all: test
test: test.c
$(CC) $(CFLAGS) -o $@ $<
clean:
rm -f *.o test
..PHONY: clean