Accessing Strings vs ints in a structure
Posted: Mon Nov 15, 2004 1:27 am
In the manual we have this example:
I redefined the struct so that the char* came before the int and noticed that we still need to use 'get-integer' before 'get-string'. But getting the integer is still straightforward: (get-integer (+ astruc 4))
Assuming that astruc is just a memory address, why do we need to call 'get-integer' before we get at the string?typedef struct mystruc {
int number;
char * ptr;
} MYSTRUC;
MYSTRUC * foo3(char * ptr, int num )
{
MYSTRUC * astruc;
astruc = malloc(sizeof(MYSTRUC));
astruc->ptr = malloc(strlen(ptr) + 1);
strcpy(astruc->ptr, ptr);
astruc->number = num;
return(astruc);
}
> (set 'astruc (foo3 "hello world" 123))
4054280
> (get-string (get-integer (+ astruc 4))) <--- ??
"hello world"
> (get-integer astruc)
123
I redefined the struct so that the char* came before the int and noticed that we still need to use 'get-integer' before 'get-string'. But getting the integer is still straightforward: (get-integer (+ astruc 4))