0

I want to pass an array pointer to a function, then malloc and realloc it. The function end, but after I get a Segmentation fault (core dumped) error before returning to the main.

I'm not too familiar with C pointers, I used mainly C++ for my codes, don't know how to use pointers as references.

Main:

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

int main(){
int A[]={1,2,4,5,7};
int B[]={1,2,3,4,6,8};
int *C=NULL;
int k=0;
egyesit(A,B,C,5,6,&k);
int i;
for (i = 0; i < k; i++)
    {
        printf("%d ", C[i]);
    }
    printf("\n");
return 0;

Header:

void egyesit(int *A, int *B, int *C, int n,int m,int *k);

Function in a different c file:

#include <stdio.h>
#include <stdlib.h>
void egyesit(int *A, int *B, int *C, int n, int m, int *k)
{
    int i, j;
    C = (int *)malloc(n * sizeof(int));
    *k = n;
    for (i = 0; i < n; i++)
    {
        printf("%d ", A[i]);
        C[i] = A[i];
    }
    printf("\n");

    for (i = 0; i < *k; i++)
    {
        printf("%d ", C[i]);
    }
    printf("\n");

    int ok;
    for (i = 0; i < m; i++)
    {
        ok = 1;
        printf("%d ", B[i]);
        for (j = 0; j < *k; j++)
        {
            if (C[j] == B[i])
            {
                ok = 0;
                break;
            }
        }
        if (ok == 1)
        {
            *k=*k+1;
            C = (int *)realloc(C, *k * sizeof(int));
            C[*k - 1] = B[i];
        }
    }
    printf("\n");
    for (i = 0; i < *k; i++)
    {
        printf("%d ", C[i]);
    }
    printf("\n");
}
3
  • 1
    egyesit(A,B,C,5,6,&k); gives a copy of the pointer C to egyesit(). Changing C within egyesit() (as in C = (int *)malloc(n * sizeof(int));) will not affect the C in the calling code. Consider passing the address of C, just like code pass in the address of k. Commented May 11, 2018 at 18:04
  • Ok, how can I modify C from the calling code? I need to use **C, and &C in the main? Commented May 11, 2018 at 18:07
  • 1
    Looks about right. Try it and see. Be sure to have all compiler warnings enabled to save time. Commented May 11, 2018 at 18:08

3 Answers 3

1

To allocate malloc or realloc momory for C you have to pass its address to the function. So your function should be:

void egyesit(int *A, int *B, int **C, int n,int m,int *k)

otherwise the pointer allocated in egyesit will not be set in the variableC declared in main.

So your function becomes like this:

void egyesit(int *A, int *B, int **C, int n, int m, int *k)
{
    int i, j;
    *C = (int *)malloc(n * sizeof(int));
    *k = n;
    for (i = 0; i < n; i++) {
        printf("%d ", A[i]);
        (*C)[i] = A[i];
    }
    printf("\n");

    for (i = 0; i < *k; i++) {
        printf("%d ", (*C)[i]);
    }
    printf("\n");

    int ok;
    for (i = 0; i < m; i++) {
        ok = 1;
        printf("%d ", B[i]);
        for (j = 0; j < *k; j++) {
            if ((*C)[j] == B[i]) {
                ok = 0;
                break;
            }
        }
        if (ok == 1) {
            *k = *k + 1;
            *C = (int *)realloc(*C, *k * sizeof(int));
            (*C)[*k - 1] = B[i];
        }
    }
    printf("\n");
    for (i = 0; i < *k; i++) {
        printf("%d ", *C[i]);
    }
    printf("\n");
}

And then you call it from main in this way:

egyesit(A, B, &C, 5, 6, &k);
Sign up to request clarification or add additional context in comments.

Comments

0

Or you could just return a pointer to the memory allocated in the function eyesgit() as in

int *egyesit(int *A, int *B, int *C, int n, int m, int *k)
{
    int i, j;
    C = malloc(n * sizeof(int));

    ....
    ....
    ....


    return C;
}

and in main(),

C=egyesit(A,B,C,5,6,&k);

But don't forget to deallocate the memory once you are done using it like

free(C);

You need not explicitly cast the return value of malloc(). Read this post.

malloc() and realloc() returns NULL if there some error like insufficient memory. You may want to check that as well.

Comments

0

Segmentation fault is in your function egyesit in the first for loop

*C[i] = A[i];

This is because you allocated variable C like this

*C = (int *)malloc(n * sizeof(int));

C is pointer on pointer on int... int **C

So you should first allocate memory like this

C =(int**)malloc(sizeof(int*));

After that, you allocate memory for the array of type int on which C is pointing

//*C meaning you are dereferencig on what C is pointing, and allocate memory for that
*C=(int*)malloc(sizeof(int)*n));

This is how it should look like

 void egyesit(int *A, int *B, int *C, int n, int m, int *k)
 {
     int i, j;
     C = (int **)malloc(sizeof(int*));
     *C = (int*)malloc(sizeof(int)*n);
     *k = n;
     for (i = 0; i < n; i++)
     {
         printf("%d ", A[i]);
         C[i] = A[i];
     }
     printf("\n");
     //and the rest of the function

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.