Home > Back-end >  passing a char 2d array into a void function in c
passing a char 2d array into a void function in c

Time:12-09

I'm new to c but I have been trying for ages to try and get this to work even though it seems so simple.

So below is what I am aiming to do which is working but I want to make 2 functions: fillseats() and printseatingplan()[for now I just want them all blank];

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
    char seatingplan[15][15];

    memset(seatingplan,'.',sizeof seatingplan);
    
    for (int k = 0; k < 15;   k)
    {
        for(int j = 0; j < 15;   j)
        {
            printf("%c",seatingplan[k][j]);
        }
        printf("\n");
    }
}

So whenever I try to run these functions without pointers it gives me these �`:�ij:� i�d:�iH@=�i �B��ik:�

how can i use pointers to fix this

can I pass this array into the functions where printseatingplan()

void printseatingplan(char array[15][15])
{
    for (int k = 0; k < 15;   k)
    {
        for(int j = 0; j < 15;   j)
        {
            printf("%c",array[k][j]);
        }
        printf("\n");
    }
}

and then fillseats() does:

void fillseats(char array[15][15])
{
    memset(array,'.',sizeof array);
}

CodePudding user response:

sizeof will give you only the size of the pointer, not the whole array.

You need to pass the sizes to the function. I would use pointer to array:

void fillseats(size_t rows, size_t cols, char (*array)[cols])
{
    memset(array,'.',rows * sizeof(*array));
}

void printseatingplan(size_t rows, size_t cols, char (*array)[cols])
{
    for (size_t row = 0; row < rows; row  )
    {
        for(size_t col = 0; col < cols; col  )
        {
            printf("%c",array[row][col]);
        }
        printf("\n");
    }
}

CodePudding user response:

char array[15][15] when used as parameter to a function "decays" into a pointer to the first element, in this case equivalent to char (*array)[15]). If you do sizeof array instead the function, you get the size of a pointer (4 or 8 etc). If you do sizeof *array you just get the size of one dimension, 15.

A simple way to fix it:

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

void fillseats (size_t x, size_t y, char array[x][y])
{
    memset(array, '.', sizeof(char[x][y]));
}

void printseatingplan (size_t x, size_t y, char array[x][y])
{
    for (size_t i = 0; i < x; i  )
    {
        for(size_t j = 0; j < y; j  )
        {
            printf("%c",array[i][j]);
        }
        printf("\n");
    }
}

int main (void)
{
    char seatingplan[15][15];

    fillseats(15, 15, seatingplan);
    printseatingplan(15, 15, seatingplan);
}

CodePudding user response:

The size of array is bound to its type. The problem is that the parameters of array type decay to pointers. To prevent it, you can pass a pointer to an array. The pointer don't decay thus the essential part of the array type prevails.

void printseatingplan(char (*array)[15][15])
{
    for (int k = 0; k < 15;   k)
    {
        for(int j = 0; j < 15;   j)
        {
            printf("%c", (*array)[k][j]);
        }
        printf("\n");
    }
}

void fillseats(char (*array)[15][15])
{
    memset(*array,'.',sizeof *array);
}

int main (void)
{
    char seatingplan[15][15];

    fillseats(&seatingplan);
    printseatingplan(&seatingplan);
}

  • Related