Reputation: 11
I am using an atmega16
and am writing a simple program to test. I found out that the line
PORTC = *(z+2);
always prints n
and not 0
/ ( NULL )
what it should be .... anyone can help?
int main(void)
{
char *z;
DDRC=0xFF;
while(1)
{
*z='o';
PORTC=*z;
_delay_ms(24000);
*(z+1)='n';
PORTC = *(z+1);
_delay_ms(24000);
*(z+2)=0;
PORTC = *(z+2);
_delay_ms(24000);
if(strcmp(z,"on")==0)
{
PORTC =0xff;
_delay_ms(6000);
}
else
{
PORTC=0x03;
_delay_ms(6000);
}
}
}
Upvotes: 1
Views: 60
Reputation: 8058
You haven't actually allocated any space for z to point at. As a result, it's a wild pointer and attempts to assign anything to what it points at -- never mind a character or two after that -- will have undefined results, probably bashing or being bashed by other data.
When putting things into memory, reserve memory to put them into. Either malloc, or reserve an array or other memory block and point z at the start of that.
Upvotes: 3
Reputation: 69934
You never initialized the value of the z
pointer. It might be pointing to invalid memory and your program might be running into undefined behaviour.
Try again while allocating an array for z to point to
char buffer[10];
char *z = buffer;
or
char z[10]
Upvotes: 2