0
votes

I have a program that take two already sorted arrays and sort them into a third array.

When I create the third array using Dynamic allocation and pass it into a function I recieve a warning (incompatible pointer types passing 'int **' to parameter of type 'int *'), what should I do to fix this?

Thanks!

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

void printVec(int v[], int t){
    printf("[ ");
    for(int i = 0; i < t; i++){
        printf("%d ", v[i]);
    }
    printf("]");  
}

void interc2w(int a[], int ta, int b[], int tb, int c[], int tc, int i, int j, int k){
    if(k < tc){
        if(i < ta && j < tb) {
            if(a[i] < b[j]) {
                c[k] = a[i];
                interc2w(a, ta, b, tb, c, tc, i+1, j, k + 1);
                }
            else {
                c[k] = b[j];
                interc2w(a, ta, b, tb, c, tc, i, j+1, k + 1);
                }
            } else{
                if(i == ta) {
                    c[k] = b[j];
                    interc2w(a, ta, b, tb, c, tc, i, j+1, k + 1);
                }
                else if(j == tb) {
                    c[k] = a[i];
                    interc2w(a, ta, b, tb, c, tc, i+1, j, k + 1);
                }
                }
        } else return;
}

int main(){
    int v1[] = {5,12,19,21,30,50}, v2[] = {2,4,6,8,10,13,15,20,25,60};
    int t1 = (sizeof(v1)/sizeof(v1[0])), t2 = (sizeof(v2) / sizeof(v2[0])), t3 = t1 + t2;
    int **v3 = malloc(t3 * sizeof(int *));
    printf("Vetor 1: ");
    printVec(v1, t1);

    interc2w(v1, t1, v2, t2, v3, t3, 0, 0, 0);

    printf("\n\nVetor 2: ");
    printVec(v2, t2);

    printf("\n\nVetor 3 intercalado: ");
    printVec(v3, t1 + t2);
    
    return 0;
} 
1
Why do you declare v3 as int** ?klutt

1 Answers

0
votes

You defined v3 as a pointer-to-pointer, allowing it to hold the address of an array of pointers, and allocated space for an array of pointers:

int **v3 = malloc(t3 * sizeof(int *));

You want an array of int, so change it to:

int *v3 = malloc(t3 * sizeof(int));