Passing a 2-D Array To A Function

Hello, I recently wrote a program that uses recursion to fill an object.


EX.
*****
*   *
*   *
*****


To:

*****
*****
*****
*****



The problem is that when I try to pass the 2D-Array to the function that fills it, I get this error:
233 35 [Error] cannot convert 'char (*)[(((long long unsigned int)(((long long int)width) + -0x00000000000000001)) + 1)] {aka char (*)[(((long long unsigned int)(((long long int)width) + -0x00000000000000001)) + 1)]}' to 'char**' for argument '1' to 'char** fill(char**, int, int, int)'

So here is the code and the bolded part is where the problem is.

Some library code for those of you who want to try to compile it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <cctype>
#ifndef _simpio_h
#define _simpio_h
#ifndef _genlib_h
#define _genlib_h
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#define FALSE false
#define TRUE true
#define ErrorExitStatus 1
typedef char *string;
typedef FILE *stream;
#define UNDEFINED ((void *) undefined_object)
extern char undefined_object[];
char undefined_object[] = "UNDEFINED";
void *GetBlock(size_t nbytes);
void *GetBlock(size_t nbytes)
{
     void *result;
     result = malloc(nbytes);
     //if (result == NULL) Error("No memory available");
     return result;
}
void FreeBlock(void *ptr);
void FreeBlock(void *ptr)
{
     free(ptr);
}
#define New(type) ((type) GetBlock(sizeof *((type) NULL)))
#define NewArray(n, type) ((type *) GetBlock((n) * sizeof (type)))
void Error(string msg, ...);
void Error(string msg, ...)
{
     va_list args;

     va_start(args,msg);
     fprintf(stderr, "Error: ");
     vfprintf(stderr, msg, args);
     fprintf(stderr, "\n");
     va_end(args);
     exit(ErrorExitStatus);
}
#define repeat for (;;)
#if defined( __BORLANDC__ )
#  if !defined ( __LARGE__ )
#    error The genlib.h library requires the LARGE memory model.
#  endif
#  if defined( _Windows )
#    define main void main
#  else
#    define main() void Main(void); \
            main() \
            { \
                Main(); \
                (void) getchar(); \
                return (0); \
            } \
            void Main(void)
#  endif
#endif
#endif
#define InitialBufferSize 120
string ReadLine(FILE *infile)
{
     string line, nline;
     int n, ch, size;

     n = 0;
     size = InitialBufferSize;
     line = (char *) GetBlock(size + 1);
     while ((ch = getc(infile)) != '\n' && ch != EOF)
     {
	  if (n == size)
          {
               size *= 2;
               nline = (string) GetBlock(size + 1);
               strncpy(nline,line,n);
               FreeBlock(line);
               line = nline;
          }
	  line[n++] = ch;
     }
     if (n == 0 && ch == EOF)
     {
          FreeBlock(line);
          return (NULL);
     }
     line[n] = '\0';
     nline = (string) GetBlock(n + 1);
     strcpy(nline , line);
     FreeBlock(line);
     return (nline);
}
string GetLine(void)
{
     return (ReadLine(stdin));	
}
int GetInteger(void)
{
     string line;
     int value;
     char termch;

     while (1)
     {
	  line = GetLine();
	  switch (sscanf(line, " %d %c", &value, &termch))
          {
	       case 1:
                  FreeBlock(line);
                  return value;
           case 2:
                  printf("Unexpected character: '%c'\n", termch);
                  break;
           default:
                  printf("Please enter an integer\n");
                  break;
          }
          FreeBlock(line);
          printf("Retry: ");
     }
}
long GetLong(void)
{
     string line;
     long value;
     char termch;

     while (1)
     {
	  line = (char *) GetLine();
	  switch (sscanf(line, " %ld %c", &value, &termch))
          {
	       case 1:
                  FreeBlock(line);
                  return value;
               case 2:
                  printf("Unexpected character: '%c'\n", termch);
                  break;
               default:
                  printf("Please enter an integer\n");
                  break;
          }
          FreeBlock(line);
          printf("Retry: ");
     }
}
double GetReal(void)
{
     string line;
     double value;
     char termch;

     while (1)
     {
	  line = GetLine();
	  switch (sscanf(line, " %lf %c", &value, &termch))
          {
	       case 1:
                  FreeBlock(line);
                  return value;
               case 2:
                  printf("Unexpected character: '%c'\n", termch);
                  break;
               default:
                  printf("Please enter an integer\n");
                  break;
          }
          FreeBlock(line);
          printf("Retry: ");
     }
}
#endif 


My Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
char **fill(char **figure,int length,int width,int wplace)
{
	int counter;
	for(counter=0;counter<length;counter++)
	{
		if(!isspace(figure[counter][wplace]))
		{
			counter++;
			do
			{
				figure[counter][wplace]='*';
				counter++;
			}while(isspace(figure[counter][wplace]));
		}
	}
	if(wplace<width-1)
	{
		fill(figure,length,width,wplace+1);
	}
	else
	{
		return figure;
	}
}

int main(void)
{
	int counter,count,length,width;
	
	printf("This program fills objects.\n");
	printf("Enter the length of the object:  ");
	length=GetInteger();
	printf("Enter the width of the object:  ");
	width=GetInteger();
	
	char figure[length][width];
	char *row=new char[length];
	
	for(counter=0;counter<width;counter++)
	{
		if(counter>8)
		{
			printf("Row #%d: ",counter+1);
		}
		else
		{
			printf("Row #%d:  ",counter+1);
		}
		row=GetLine();
		for(count=0;count<length;count++)
		{
			figure[count][counter]=row[count];
		}
	}
	figure=fill(figure,length,width,0);
	
	printf("\n\n\nFilled object:\n\n");
	for(count=0;count<width;count++)
	{
		for(counter=0;counter<length;counter++)
		{
			printf("%c",figure[counter][count]);
		}
		printf("\n");
	}
	delete [] row;

getchar();
return(0);
}


(For those who cannot see it it is on line 55 in 'My Code'.)

I think the problem has to do with the fact that I have two variables defining the boundaries. Not sure... Any help would be nice.
Last edited on
Does it still give you an error if you try changing line 36 to char** figure?
Array figure is declared as

char figure[length][width];

but in function fill the first parameter is declared as

char **figure

char [*][width] and char ** are two different types.

You can call function the following way

fill( (char **)figure,length,width,0);

And you shall not do the assignment

figure=fill(figure,length,width,0);

because as I said the array figure and the return type of the function have different types. You should write simply

fill( (char **)figure,length,width,0);


New problem , now the whole 2-D array is Nullified.

Problem with the types???
It means that function fill is invalid. Investigate its code.
Topic archived. No new replies allowed.