passing a 2 dimensional char array through a function

Hey guys,

I tried to get the following code to work for all day long, but I still don't get it. I searched a lot in other topics, but withouth succes.
I already got it working without the functions, but I want to make my code look nicer by doing it with functions.

I really would appreciate some help, or something to get me in the right direction!

#include <iostream>
using namespace std;

////////// Function CREATE THE FIELD //////////
char create_field()
{
char field[14][14];
for(int i = 0; i < 14; ++i)
{
for(int j = 0; j < 14; ++j)
{
field[i][j] = 'O';
}
}
return field;
}
///////////////////////////////////////
//////////// Funtion Print THE FIELD array//////////
///////////////////////////////////////
void print_field(char field)
{
for(int i = 0; i < 14; ++i)
{
for(int j = 0; j < 14; ++j)
{
cout << " " << field[i][j] << " ";
}
putchar('\n');
}
}

int main()
{
field = create_field();

char field[14][14];
print_field(field);

return 0;
}
Please use code tags, they make helping you a lot easier

The type of 'field' is not char. It is an array of arrays of chars. This type is indentified by writing char[][] or char**

In main() you call create_field() before even defining 'field'

create_field()° should take a char** as parameter and fill that with chars. Also it makes little sense that it returns 'field'

print_field() is almost correct, but you have to change the type of its parameter


° The way in which you wrote this function would work if you used dynamic memory allocation. As of now it return (or you wanted to return, but actually it doesn't) a pointer to a local variable, which will get destroyed and invalidate the pointer when the function ends

I wouldn't worry about dynamic memory for now though, it's a bit more advanced
Last edited on
Thanks a lot for the quick responses!
So if I get it correctly, it it did not make sense to return field, because Im changing the variable at the memory location now? And I can acces it globally by refering at the beginning in a function?

I still find it a bit vague how you get 2 dimensions by reffering a pointer to a pointer.

Now I changed my code to the following:


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
#include <iostream>
using namespace std;

////////// Function CREATE THE FIELD //////////
void create_field()
   {
       char** field = 0;
		for(int i = 0; i < 14; ++i)
		{
			for(int j = 0; j < 14; ++j)
			{
				field[i][j] = 'O';
			}
		}
   }
///////////////////////////////////////
//////////// Funtion PRINT THE FIELD //
///////////////////////////////////////
    void print_field()
    {
    	char** field;
    	for(int i = 0; i < 14; ++i)
    	{
    		for(int j = 0; j < 14; ++j)
    		{
    			cout << " " << field[i][j] << " ";
    		}
    		putchar('\n');
    	}
    }
/////////////////////////////////////
////////// MAIN FUNCTION   //////////
/////////////////////////////////////

int main()
{
	create_field();
    print_field();

return 0;
}


But now the program stops working and crashes and doesn't give an error to work with..
Am I overseeing something?

Thanks a lot for the help so far!
So if I get it correctly, it it did not make sense to return field, because Im changing the variable at the memory location now? And I can acces it globally by refering at the beginning in a function?
Uh, sorry, I'm not sure of what you're saying here

I still find it a bit vague how you get 2 dimensions by reffering a pointer to a pointer.
Pointers can be pretty hard to deal with at the beginning.
I'll assume you're familiar with the similarity of pointers and arrays. Since an array is a sequence, it can be represented like this

1D array
[0][ ][ ][ ]

Since we're talking about a pointer to pointers, it's the same as saying an array of arrays. So we can represent it like this

2D array
[0][ ][ ][ ]
[0][ ][ ][ ]
[0][ ][ ][ ]
[0][ ][ ][ ]

The computer won't actually store the data in this way, but the mechanism with which we access it can be represented with a matrix. And here's your two dimensions. The key is to remember that pointers and arrays both do the same thing: hold the address of a chunk of data
-------------------------------------------------------------
About your code, there are several new problems:
In create_field() you're creating 'field' which is [a variable] local to the function. This means that when the functions ends 'field' gets deleted (every local variable is) and you can't access it in print_field()

In print_field() you are again creating a 'field' which is not the one you previously populated

Moreover, a difference between pointers and arrays is that when you create an array the memory to store its elements is automatically reserved.
To do that with pointers you need to use dynamic memory allocation, which you are not. So your 'field**' doesn't actually point to any valid memory location. In your loops you try to access those location and that makes your program crash ("segmentation fault" if you want to google it)

So, unless you want to study how 'new' works, you better define 'field' as char[x][y]. The type will be the same (char** == char[][]) but giving the size in the brackets will reserve valid memory.

Now, to make 'field' accessible to both functions you have two options: make it a global variable (bad, don't do this unless your life is threatened) or create it in main() and pass it as argument to both functions
Last edited on
Thank you for your response and making the pointer-array relation more clear,

I ready into the dynamic memory method, but now I get the following error for the
- field[i][j] = 'O'; - part:

Symbol 'field' could not be resolved.

I don't get it, I reserved memory for a 14 row char array, why can it not write a
char to the array elements?

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
int main()
{
	char** field = new char*[14];
	void create_field(char**);
//	void print_field(char field);

	create_field(field);

//	print_field();

return 0;
}


////////// Function CREATE THE FIELD //////////
void create_field()
   {
		for(int i = 0; i < 14; ++i)
		{
			for(int j = 0; j < 14; ++j)
			{
				field[i][j] = 'O';
			}
		}

   }
in your void create_field() put char** field as the parameter, and i think you need to allocate the memory in a different way.
1
2
3
4
5
6
7
8

     char** field = new char*[14];
     
     for(int i = 0;i < 14;i++)
     {
          field[i] = new char[1];
     }
thanks a lot guys!

I finally got it working and learned a lot.
Here's 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
int main()
{
	char** field = new char*[14];
	char** create_field(char** field);
	void print_field(char **field);

	field = create_field(field);

	print_field(field);

return 0;
}


////////// Function CREATE THE FIELD //////////
char** create_field(char** field)
   {
		for(int i = 0; i < 14; i++)
		{
			field[i] = new char [14];

			for(int j = 0; j < 14; j++)
			{
				field [i][j] = 'O';
			}
		}
		return field;

   }
///////////////////////////////////////
//////////// Funtion PRINT THE FIELD //
///////////////////////////////////////
    void print_field(char ** field)
    {
    	for(int i = 0; i < 14; ++i)
    	{
    		for(int j = 0; j < 14; ++j)
    		{
    			cout << " " << field[i][j] << " ";
    		}
    		putchar('\n');
    	}
    }
Last edited on
Topic archived. No new replies allowed.