jprince14
jprince14

Reputation: 301

Manipulate variable length two dimensional array through a function

I am trying to write data into a variable length two dimensional array and my program keeps seg-faulting when I call myfunc but it works fine when I try to perform the same manipulation outside of a function. I can tell that the issues is that the address pointed to at array[0] doesn't equal the address pointed to at data[0]. Can someone advise me as to the root cause of this issue and proper way to rewrite myfun.

void myfun(unsigned char **array){
    printf("array = %p, array[0] = %p\n", array, array[0]);
    //This line below causes a segfault
    strcpy(array[0], "Position0");
}

int main(void) {
    int row = rand() % 5 + 1;   // random number between 1-5
    int col = rand() % 10 + 20;   // random number between 20-29
    unsigned char data[row][col];

    printf("data = %p, data[0] = %p\n", data, data[0]);

    //This function call causes a segfault
    myfun(data);
    printf("%s\n", data[0]);

    //This works
    strcpy(data[1], "Position1");
    printf("%s\n", data[1]);

    return 0;
}

Upvotes: 1

Views: 96

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 755104

Since you are clearly using C99 or later and a compiler with VLA (variable length array) support, you can write the function correctly quite easily:

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

static void myfun(int rows, int cols, char array[rows][cols])
{
    printf("array = %p, array[0] = %p\n", array, array[0]);
    strcpy(array[0], "Position0");
    printf("a[0] = [%s]\n", array[0]);
}

int main(void)
{
    int row = rand() % 5 + 1;   // random number between 1-5
    int col = rand() % 10 + 20;   // random number between 20-29
    unsigned char data[row][col];

    printf("data = %p, data[0] = %p\n", data, data[0]);

    myfun(row, col, data);
    printf("%s\n", data[0]);

    strcpy(data[1], "Position1");
    printf("%s\n", data[1]);

    return 0;
}

Upvotes: 3

Judismar Arpini Junior
Judismar Arpini Junior

Reputation: 431

The problem is the function definition. If you want to pass a matrix the way you did, you have to allocate it dynamically, but that's another story. I'll explain the error.

The C language actually implement the static matrices as a single array of size row*col. That is for efficiency.

When you pass the static matrix to that function, you got a problem: it expects a double pointer, but what you pass is actually a single pointer requiring the number of columns. You have to write like this:

void myfun(unsigned char array[][col]){ ...

That's how you should define it, where col is the number of columns.

The problem's that you don't know the number of columns, it's variable, so I suggest you use malloc or calloc to alocate a dynamic matrix.

Editing: look at this link posted as comment by n.m. for details on the static matrix that C implements: Is 2d array a double pointer?

Upvotes: 1

Related Questions