0

I have a problem returning dynamic array pointer with function parameter. I get segfault

#include <stdio.h>
#include <stdlib.h>

void createArray(int *ptr, int n)
{
    ptr = malloc(n * sizeof(int));
    for(int i = 1; i <= n; ++i)
    {
        *(ptr + (i - 1)) = i*i;
    }
}

int main() {
    int *array = NULL;
    int n = 5;
    createArray(array, n);
    for(int i = 0; i < n; ++i)
    {
        printf("%d", array[i]);
    }
    return 0;
}

I have to fill my array with i*i, when I is from 1 to n. I don't get any errors or warnings. Just message about segmentation fault. Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

8
  • 2
    Remember that C only passes function arguments by value. That means the value used in the call is copied into the functions local argument variable. So modifying the local argument variable itself will only modify this copy, not the original value used in the call. Please do research about emulating pass by reference in C. Commented Nov 16, 2022 at 16:35
  • 1
    On another note, why *(ptr + (i - 1)) instead of the easier to read ptr[i - 1]? Commented Nov 16, 2022 at 16:36
  • Yes, so I should do parameter (int **ptr)? Commented Nov 16, 2022 at 16:37
  • 1
    The problem is *ptr[i - 1], because it's the same as *(ptr[i - 1]) while you need (*ptr)[i - 1] . Commented Nov 16, 2022 at 16:45
  • 1
    Alternatively, you could define a int *createArray(int n) function and make it return a pointer to the allocated memory. Call it as array = createArray(n);. Commented Nov 16, 2022 at 16:54

2 Answers 2

1

Memory must be allocate in the calling function, but not in called. This variant works:

#include <stdio.h>
#include <stdlib.h>

void createArray(int *ptr, int n){
 int i;
    for(i = 1; i <= n; i++)    {
        *(ptr + (i - 1)) = i*i;
// fprintf(stdout,"%d %d\n", i, *(ptr + (i -1)));fflush(stdout);
    }
}

int main() {
    int i, n, *array = NULL;
 void *pvc;
    n = 5;
    array = (int *)malloc(n * sizeof(int));
    createArray(array, n);
    for(i = 0; i < n; i++)    {
        fprintf(stdout,"%d %d\n", i, array[i]);fflush(stdout);
    }
 pvc = (void *)array;
 free(pvc);
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

It could be allocated in other function too. Thank you.
Yes, I saw example with **ptr, but not tried it.
0

You can change pointer through function parameters like this:

void createArray(int **ptr, int n)
{
    *ptr = malloc(n * sizeof(int));
    for(int i = 1; i <= n; ++i)
    {
        (*ptr)[i - 1] = i*i;
    }
}

int main() {
    int *array = NULL;
    int n = 5;
    createArray(&array, n);

Remember to call function like this: createArray(&array, n);

Comments

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.