jeudi 23 avril 2015

Using pointers to access 2D array in a function

I am currently working on a code to access elements of an array using Depth-First Search. If I come across a 'Y', I replace it with a '*' and increment my count. But while using pointers, I am encountering the following error:

In function 'dfs':
5:5: error: invalid type argument of unary '*' (have 'int')


In function 'checkMe':
23:16: error: invalid type argument of unary '*' (have 'int')

Here is my Code:

#include <stdio.h>

void dfs(char* m,int r,int c,int v1,int v2)
{
    *(*(m+r)+c) = '*';
    for(int k=0;k<2;k++)
    {
        if((r<(v1-1))&&(c<(v2-1)))
        {
            dfs(m,r,c,v1,v2);
        }
    }
} 

int checkMe(char* m,int row,int col)
{
    int counter = 0,i=0,j=0;

    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            if(*(*(m+i)+j) == 'Y')
            {
                dfs(m,i,j,row,col);
                counter++;
            }
        }
    }

    return counter;
}

int main()
{
    int i = 0,count = 0,p = 4,q = 4;
    char input[4][4]={{'Y','Y','Y','N'},{'N','N','N','Y'},{'Y','Y','N','Y'},{'Y','N','N','N'}};

    count = checkMe(input,p,q);
    printf("%d",count);

    return 0;
}

Can anyone help me with this?

Aucun commentaire:

Enregistrer un commentaire