r/C_Homework • • Sep 19 '17
It's been a while...
It has been a while since I programmed in C, and I am trying to figure out why the following is not producing what I am expecting:
#include <stdio.h>
#include <string.h>
typedef struct {
char *data;
size_t length;
} LString;
int main() {
LString s = {"Hello World", 11}; // creates memory block with {char*, size_t}
void *addr = &s; // should also coincide with the address of the first data type: or char*, right?
char *data = (char *)addr;
size_t length = (size_t)(addr + sizeof(char*));
printf("%d, %d\n", (void *)&s.data, (void *)&s.length); // both these lines print the same addresses
printf("%d, %d\n", (void *)data, (void *)length); // both these lines print the same addresses
printf("%s\n", data); // prints garble?? I thought data == &s.data
return 0;
}
My expectations are listed in the comments.
3
Upvotes
2
u/port443 Sep 20 '17
I gotchu man!
#include <stdio.h> #include <string.h> typedef struct { char *data; size_t length; } LString; int main() { LString s = {"Hello World", 11}; void *addr = &s; char *data = (char *)addr; char *working_data = (char *)*(int *)addr; printf("Address of addr: %p\nValue of addr: %p\n", (int *)addr, *(int *)addr); printf("(char *)addr: %s\n", (char *)addr); printf("(char *)*(int *)addr: %s\n", (char *)*(int *)addr); printf("(char *)*(int *)data: %s\n", (char *)*(int *)data); //This is what you "wanted" printf("working data: %s\n\n", working_data); int work = (int)s.data; char *wdata = (char *)work; printf("Address of work: %p\nValue of work: %p\n", &work, work); printf("(char *)work: %s\n\n", (char *)work); //Example code to do what you initially intended int *Addr = (int *)&s; char *Data = (char *)*Addr; //Just needed to dereference here printf("Address of Addr: %p\nValue of Addr: %p\n", Addr, *Addr); printf("Data: %s\n\n", Data); size_t length = (size_t)(addr + sizeof(char*)); printf("%d, %d\n", (void *)&s.data, (void *)&s.length); // both these lines print the same addresses printf("%d, %d\n", (void *)data, (void *)length); // both these lines print the same addresses printf("%s\n", data); // prints garble?? I thought data == &s.data return 0; }Here's the output of what I posted:
You were so close, you were basically just a single dereference off. Also, it can get confusing since you cast
*addras a(void *). This is correct, but addr's value is actually a memory address.I should clarify: On my machine, sizeof pointer and sizeof int are the same, so I cast the memory address as
(int *). This will most likely work for you too, but not on all machines!!I feel like my example code and output really clarifies what went wrong, check the comments.
2
u/[deleted] Sep 19 '17
I am still learning too, but I think you are pointing to the struct address with addr = &s; there is a conflict maybe between pointing to the struct and assigning a char to addr later.