I have this program:
#include <stdio.h>
#include <stdlib.h>
typedef struct item {
int low; int high; char label[16];
} Item;
typedef struct item_coll {
size_t length; Item *items[];
} ItemColl;
extern int find_first_in_range(ItemColl *ic, int rlow, int rhigh);
/*
char *find_first_in_range(ItemColl *ic, int rlow, int rhigh) {
for (size_t i = 0; i < ic->length; i++)
if (ic->items[i]->low >= rlow && ic->items[i]->high <= rhigh)
return &ic->items[i]->label[0];
return NULL;
}
* */
int main() {
struct item fruits[] = {
{10, 20, "Apple"},
{12, 14, "Pear"},
{ 8, 12, "Banana"},
{ 2, 4, "Grape"},
{15, 35, "Watermelon"}
};
struct item_coll *basket = malloc (sizeof *basket + 5 * sizeof *basket->items);
basket->length = 5;
for (size_t i = 0; i < 5; i++)
basket->items[i] = &fruits[i];
int label = find_first_in_range (basket, 12, 15);
printf ("%d\n", label);
free (basket);
return 0;
}
How do I access the different fields in fruits
?
Right now I was able to come up with the following information:
xor %r12, %r12
add $8, %r12
mov (%rdi, %r12), %rdi
mov (%rdi), %rax
ret
This will give me the
10
fromApple
.If I don't use
%r12
and just load%rdi
's value from memory, I get5
(thebasket
's length).If I add
16
to%r12
, I get the12
fromPear
.- If I add
24
to%r12
I get the8
fromBanana
.
How do I access for example the 20
in Apple
? Shouldn't I just need to add 12
to %r12
? It's giving me segmentation fault.