|
CS 456 - Systems Programming
Spring 2024
|
Displaying ./code/jan18/ptr.c
#include <stdio.h>
int main(void) {
char *ptr = "ABCDEFGH";
int *bna = (int *) ptr;
bna +=1; // Would cause iterate by one integer space (i.e 4 bytes on some systems)
ptr = (char *) bna;
printf("%s\n", ptr);
/* Notice how only 'EFGH' is printed. Why is that?
* Well as mentioned above, when performing 'bna+=1'
* we are increasing the **integer** pointer by 1,
* (translates to 4 bytes on most systems) which is equivalent
* to 4 characters (each character is only 1 byte)*/
return 0;
}
|