0

I have been blocking for a while now on a function whose purpose is to assign a value to the respective cells of the sent pointed array:

void assign(T** pointerArray, U linkedListOfValues) {
    size_t i = 0;
    while (linkedListOfValues != NULL) {
        *pointerArray[i++] = convert(linkedListOfValues->current); // Crash here, when add a second (or more) item
        linkedListOfValues = linkedListOfValues->next;
    }
}

As commented in the code, when I add more than one element to this pointed array, the program stops. There is no error message or warning. I use this function as such:

T* array = malloc(size * sizeof(T));
assign(&array, myLinkedList);

size represents the number of elements present in the linked list, and returns a correct value; the types used (U and T) are very arbitrary, and the convert function converts an element of type U into an element of type T.

The tests I performed did not find any errors in the convert function and in the dynamic allocation.

When I try without using this function, the program works correctly:

T* array = malloc(size * sizeof(T));
size_t i = 0;
while (linkedListOfValues != NULL) {
    array[i++] = convert(linkedListOfValues->current);
    linkedListOfValues = linkedListOfValues->next;
}

I don't understand what makes my function crash, since I only make a reference, using pointers, to the initial array. What's my mistake?

Ps: I started the C again a little while ago, maybe the solution is obvious, excuse me if that's the case.

4
  • 1
    T* array = malloc(size * sizeof(T)); <<-- is not an array of pointers. Commented Aug 31, 2019 at 17:41
  • 1
    If convert actually returns type T, and not a pointer to something, then all you need to do is remove a couple of * and a &. See minimal reproducible example. Commented Aug 31, 2019 at 17:46
  • 2
    You should be able to pass T* pointerArray as first argument and use pointerArray[i++] (excluding *) to assign values to. You do not need double pointer if you do not need to reassign new array or change its base pointer. Commented Aug 31, 2019 at 17:46
  • I didn't talk about an array of pointers, but a pointer to an array; so this dynamic allocation is fair, right? But I may have been misunderstood, sorry; I'm talking about assigning a value to each array cells from a function that takes a pointer of it, its size having previously allocated according to the number of elements in the linked list. In fact, there is only one pointer, and that is the array whose address was sent to the function. Edit: I forgot that the C arrays were actually... simple pointers to a memory area. So indeed, a double pointer was not necessary; it solves the problem. Commented Aug 31, 2019 at 18:35

1 Answer 1

2

If I understood it right, I think what you mean to do is something like this:

T* array = malloc(size * sizeof(T));
assign(array, myLinkedList);

void assign(T* pointerArray, U linkedListOfValues) {
  size_t i = 0;
  while (linkedListOfValues != NULL) {
      pointerArray[i++] = convert(linkedListOfValues->current);
      linkedListOfValues = linkedListOfValues->next;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly, thank you for giving the answer (comments allowed me to solve the problem, but an answer as such is always welcome).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.