Pablo Vizcaino
Pablo Vizcaino

Reputation: 23

Trying to pass an array with variable size and it's not working properly

I am having problems accessing a matrix and vector with variable size using malloc() realloc() functions in a subalgorithm. I have tried the following but it doesn't seem to work.

int main()
{
    char nombre[50];
    sprintf(nombre,"data.txt");
    int **red;
    int *links;
    int i, j, colSize;

    links  = (int*)malloc(Nred*sizeof(int));
    red = (int**)malloc(Nred*sizeof(int*));
    for(i=0; i<Nred; i++)
    {
        red[i]=(int*)malloc(1*sizeof(int));
    }

    LeeRed(*red, *links, Nred, nombre, &colSize);

}

void LeeRed (int ***mat, int **links, int nNodes, char *nombre, int *colSize)
{
    int i, j, maxSize, nodo1, nodo2;
    FILE *f, *g;

    f=fopen(nombre,"rt");
    g=fopen("matrizPrueba.txt", "w");
    maxSize=0;

    /// Number of links per node starts at 0
    for(i=0; i<nNodes; i++)
    {
        *links[i]=0;
    }
    //...
 
}


Upvotes: 0

Views: 69

Answers (2)

Guy Marino
Guy Marino

Reputation: 439

First things first, * is the dereference operator, not the create reference operator. When you pass your matrix and links to your function, you should use & instead. For example:

LeeRed(&red, &links, Nred, nombre, &colSize);

This will make all of your types match. I would be shocked if your compiler didn't warn you about this (if it's not, try recompiling with -Wall). That being said, you really shouldn't be passing a pointer to your matrix unless you plan on modifying the pointer to your matrix. You might want to do this if you were planning on reallocating the matrix for some reason, but in the function above just make your life easier and pass the pointers by value. So change your function signature to:

void LeeRed (int **mat, int *links, int nNodes, const char *nombre, int *colSize);

Also, when you access an array with a pointer, you do not need to dereference it first, so you can use links[i] instead of *links[i]. And be careful with your allocations, right now you have an Nredx1 matrix allocated, but if you reallocate any of those columns without freeing them, you'll have a memory leak on your hands. Be sure to free everything you allocate (from the bottom up, calling free(red) will not free the columns, you must do those individually).

I'm also not sure what the links variable is intended to do, but it may be redundant if you're just trying to read into a matrix. I can't help you any further without more context.

Upvotes: 2

dbush
dbush

Reputation: 223739

The types of the first two parameters don't match what you're passing in.

red has type int ** and links has type int *, and you're passing *red and *links which have types int * and int respectively. This differs from the int *** and int ** types that your function is expecting for these parameters.

You're also calling the function before it's been defined or declared, so it has an implicit declaration of int LeeRead() which doesn't match the actual definition.

You should pass these two parameters directly without dereferencing:

LeeRed(red, links, Nred, nombre, &colSize);

Change the parameter types to match:

void LeeRed (int **mat, int *links, int nNodes, char *nombre, int *colSize)

Change how these two parameters are used in the function accordingly, and move the function's definition to above main.

Upvotes: 2

Related Questions