#Changing array (pointer) size and and adding value

32 messages · Page 1 of 1 (latest)

visual bane
#

Hi i'm quite new to C and i'm trying to rewrite a very simple 4 line ptyhon program about dynamic programming and memoization but i'd need to dynamically change my pointer size for that, for some reason this code doesn't work

//Second attempt
#include <stdio.h>
#include <stdlib.h>

void append(struct mem *array, struct mem newItem);

struct mem {
    int n;
    long long int i;
};

int main() {
    struct mem t = {0, 1};
    struct mem* test = (struct mem*)calloc(1, sizeof(struct mem));
    test[0] = t;
    printf("{%d, %d}\n", test[0].n, test[0].i);
    struct mem s = {1, 1};
    append(test, s);
    printf("{%d, %d}", test[0].n, test[0].i);
    printf("{%d, %d}", test[1].n, test[1].i);
    append(test, s);
    printf("{%d, %d}", test[0].n, test[0].i);
    printf("{%d, %d}", test[1].n, test[1].i);
    printf("{%d, %d}", test[2].n, test[2].i);
    //ignore messy code lol
    return 0;
}

void append(struct mem *array, struct mem newItem) {
    array = (struct mem*)realloc(array, sizeof(array)+sizeof(struct mem));
    printf("%d\n", sizeof(array)/sizeof(struct mem)+1);
    int index = sizeof(array)/sizeof(struct mem)+1;
    array[index] = newItem;
}
vernal egretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

slim locust
#

sizeof(array) is not what you want here

#

array has pointer type, and C forgot it actually is an array

empty token
visual bane
visual bane
slim locust
# visual bane what do you mean?

it should be:

void append(struct mem *array, int array_size, struct mem newItem) {
    array = (struct mem*)realloc(array, (array_size + 1) * sizeof(struct mem));
    //printf("%d\n", sizeof(array)/sizeof(struct mem)+1);
    int index = array_size + 1;
    array[index] = newItem;
}
#

C does not know, inside append, that array is an array: it only knows it's a pointer

visual bane
#

oh

slim locust
#

and the sizeof a pointer is not the "length" of the array

#

so you need to pass that array size every time

slim locust
#

(or else change your structures)

visual bane
#

so i make a separate var for size right?

slim locust
#

(also: you dont need to cast the result of realloc. this is not C++)

visual bane
slim locust
#

do you compile for C ?

#

yeah I assume, in view of the headers

visual bane
slim locust
#

normally it shouldnt raise errors. void* pointers can be casted implicitly, in C.
if you have some time later, you could remove the cast and tell us the error you're getting

visual bane
#

okay i'll try again in like 10 minutes thanks a lot

vernal egretBOT
#

@visual bane Has your question been resolved? If so, type !solved :)

broken halo
#

there's another problem that your append function does not currently modify the argument passed to it, so the test variable in main won't ever be updated if realloc returns a different address

visual bane
#

well i did get it to work after using @slim locust's method and some googling to change the value after i realized it was changed properly

#

i got ```c
//Second attempt
#include <stdio.h>
#include <stdlib.h>

struct mem {
int n;
long long int i;
};
void append(struct mem **array, struct mem newItem, int *array_size);

int main() {
int array_size = 0;
struct mem t = {0, 1};
struct mem* test = (struct mem*)calloc(1, sizeof(struct mem));
test[0] = t;
array_size++;
struct mem s = {1, 1};
append(&test, s, &array_size);
for (int i = 0; i<2; i++) {
printf("{%d, %d}", test[i].n, test[i].i);
}
printf("%d", array_size);
return 0;
}

void append(struct mem *array, struct mem newItem, int array_size) {
*array = realloc(*array, (*array_size+1)*sizeof(struct mem));
int index = *array_size;
(*array)[index] = newItem;
(*array_size)++;
}

broken halo
visual bane
#

yeah makes sense

#

thanks tho

#

!solved

vernal egretBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity