0

I am creating a program that modifies a dynamic array. It must initialize the array and be able to insert into it. I have been unable print the array after in order to test it, how would I go about this?

Piece of relevant code:

typedef struct {
    char first; 
    char second;    
} name;

typedef struct {
    int number;                 
    name name;                              
} data;
/*points to array, number allocated, number used*/
typedef struct {
    data *info;         
    size_t numof;           
    size_t numused;         
} list;

void init(list *l) {
   l->data = malloc(sizeof(l) * l->numof);
   l->numused = 0;
   l->numof = 2;
}

int insert(list *l, const data *dat) {
    if (l->numused == l->numof) {
        l->numof *= 2;
        l->data = (int *)realloc(l->data, l->numof * sizeof(int));
    }
    l->data[l->numused++] = *dat;
    return 0;
}

int main(void) {
    int i;
    list l;
    data list1;
/*example info for testing*/
    list.number = 1234;
    strcpy(list1.name.first, "abc");
    strcpy(list1.name.second, "xyz");
    init(&l);
    insert(&l, list1);
/*runs through array elements to print*/
    for (i=0; i < ((int)sizeof(&l)) /(int)sizeof(&l); i++) {
        printf("%s\n", list1);
    }
    return 0;
}

Edit: I just need to know how to print the array to see if I'm doing it correctly, the code above will have errors as I had been messing around trying to figure it out.

B. Fett
  • 13
  • 1
  • 6

2 Answers2

1
strcpy(list1.name.first, "abc");
strcpy(list1.name.second, "xyz");

These both will invoke undefined behaviour as first and second are declared as char variables , and you copy string literals to them .

You need to declare both of them as character arrays .

And this -

for (i=0; i < ((int)sizeof(&l)) /(int)sizeof(&l); i++) {
    printf("%s\n", list1);
}

You try to print struct variable list1 with %s specifier, maybe you tend to print the strings that you wanted to copy. So directly print list1.name.first and list1.name.second in printf with %s specifier.

And the condition -

i < ((int)sizeof(&l)) /(int)sizeof(&l)

The cast is not necessary , and it will yield 1 so, loop will run for 1 time . Change the condition .

ameyCU
  • 16,489
  • 2
  • 26
  • 41
0

In your code, the member of structure name is defined as char. But you are trying to copy a string into it. May be this was a typo. If not you should define them as character array or character pointer. Also in your print statement you are trying to print structure data as string. It should be like -

printf("%s %s\n", list1.name.first, list1.name.second);

Also you assigned value 1234 to list.number. You may have meant list1.number. The parameters in function call of insert is wrong as well. And lastly, you have put l->data in functions init and insert which should be l->info.

kuro
  • 3,214
  • 3
  • 15
  • 31
  • What would i put as the second parameter of `insert`? – B. Fett Nov 13 '15 at 05:25
  • You should send pointer of structure `data` in the `insert` function as 2nd parameter. In this case it should be `&list1` instead of `list1` – kuro Nov 13 '15 at 11:03