Assigning a pointer to the exact next memory address of an int

J

Jordan

Guest
I am trying to assign a pointer to the exact next memory address of an int number and give it the doubled value of the number. However i can only write after 3(*4 which is the size of int) or more memory addresses,else it doesnt print anything.

That works

#include <stdio.h>


int main(){
int x, *ptr, memory_jump = 3;
scanf("%d",&x);
ptr = &x+memory_jump;
*ptr = x * 2;
printf("Given int = %d, Address = %d \nNext address content = %d,Next address pos = %d",x,&x,*ptr,ptr);

return 0;
}


If i lower the memory jump to 2 or 1 it doesnt print anything at all so an error occurs.

int main(){
int x, *ptr, memory_jump = 2;
scanf("%d",&x);
ptr = &x+memory_jump;
*ptr = x * 2;
printf("%d", (ptr-2));
printf("Given int = %d, Address = %d \nNext address content = %d,Next address pos = %d",x,&x,*ptr,ptr);

return 0;
}


Could anyone explain what happens with these 2 next memory addresses?

Continue reading...
 
Top