0

I'm working on creating a simple wordsearch generator but having a problem passing a two dimensional array through a function and editing the value inside. I have declared my array in main as such:

char tableValues[xsize-1][ysize-1];

I'm filling each point in the array with a - in main, then passing it through to a function that fills the array with random letters and returns.

void fillTable(char *tableValues){
    for ( int i = 0 ; i < xsize ; i++ ){
        for ( int j = 0 ; j < ysize ; j++ ){
            if ( tableValues[i][j]=='-')
                tableValues[i][j] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand () % 26];
        }
    }
}

My problem is that an error gets flagged at the "tableValues[i][j]" parts, I'm not sure how else I'd edit individual points in an array with pointers. Any help would be much appreciated, thanks

I am calling my function as

fillTable((char *)tableValues);
Defa1t
  • 141
  • 2
  • 10

3 Answers3

1

Try this,

void fillTable(char *tableValues){
for ( int i = 0 ; i < xsize ; i++ ){
    for ( int j = 0 ; j < ysize ; j++ ){
        if ( *((tableValues +i*(xsize-1))+j)=='-')
            *((tableValues+i*(xsize-1))+j) = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand () % 26];
    }
}
}
Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
  • I do that in my other function, printTable. I've tried printing before and after the fillTable function, and my table with the "-"s prints fine, but gets stuck during the function fillTable – Defa1t Apr 29 '15 at 14:57
  • @Defa1t that is unecessarily complicated, write code that looks natural when you read it, not just code that works. And parametrize as much as you can, that way your code is much more extensible and scalable. – Iharob Al Asimi Apr 29 '15 at 14:58
  • That is not correct. The array size is `xsize-1` x `ysize-1`. – R Sahu Apr 29 '15 at 15:20
1

You are passing an 2d array and the function expects a pointer to char which is not compatible, specially accessing it with two subscript operators is wrong, you will need to change your function signature to something like

char (int xsize, int ysize, char array[xsize][ysize])
 { 
 }

don't declare xsize and ysize as globals, just pass them to the functions so that way the functions are really reusable, otherwise there is very little to gain in wrinting functions.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

The function should be defined like

void fillTable( char ( *tableValues )[ysize-1], int rows )
{
    for ( int i = 0 ; i < rows ; i++ )
    {
        for ( int j = 0 ; j < ysize - 1 ; j++ )
        {
            if ( tableValues[i][j] == '-' )
                tableValues[i][j] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand () % 26];
        }
    }
}

provided that ysize is some defined constant.

Otherwise if your compiler supports Variable Length Arrays then it can be defined like

void fillTable( int rows, int cols, char tableValues[rows][cols] )
{
    for ( int i = 0 ; i < rows ; i++ )
    {
        for ( int j = 0 ; j < cols ; j++ )
        {
            if ( tableValues[i][j] == '-' )
                tableValues[i][j] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand () % 26];
        }
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335