passing arrays

Feb 22, 2011 at 2:58am
when i pass an array with a single dimension it goes thus:
1
2
3
4
5
6
void fun(char[]);
int main{
...
fun(a)
...}
void fun(char a[]){...}

and it works but when i apply the same concept with a 2 dimensional array i get an error is there a special way to do this i can't find any literature on it in my text books ><
Last edited on Feb 22, 2011 at 2:59am
Feb 22, 2011 at 3:16am
Use char a[][], etc. for more dimensions.
Feb 22, 2011 at 3:24am
that is what i thought but i get an error that the 2nd open bracket is not allowed... >< I'm reading up on it its saying that i need to include the second size identifier so like...char a[][c]where c is the global constant number of columns... I get an error both ways... :s
Feb 22, 2011 at 3:27am
Ah, that's right, apologies. When passing a two-dimensional array like that you must specify an dimensions after the first. If you want, however, you could also just accept a char** a instead. You should pass the size of each dimension as additional parameters.
Feb 22, 2011 at 3:36am
I don't understand let me show you where my vexation is stemming from :]
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
//creates a 10 by 10 word search puzzle
//words determined by a specified filed
//word locations then specified by user
//program creates random letters to fill in the puzzle
//programmer : Tyler Sievers
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


const int k=5;    // 5 is the most words allowed in file. constant for the first array
const int r=10;		//10 rows high
const int c=10;		//10 collomns wide

void getfile(ifstream&);
void populatearray(ifstream&, string[]);
void testfcn1(string[]);  //comment when done!!!!	
void displaygrid(const char[][c]);
void colomncounter();
void horizontalborders();

void main(){
	//more or less does nothing :]
	ifstream infile;
	string words[k];
	char thegrid[r][c];    //tron reference intended... if only i were as good a programmer...
	cout<<"welcome to the Word Finder Generator\n\n";
	getfile(infile);
	populatearray(infile, words);
	//testfcn1(words);   // COMMENT WHEN DONE!!!
	system("CLS");
	return;
}

void getfile(ifstream& words){
	//gets the file name from the user, opens it, and returns to the main
	string filename;
	do{
	cout<<"Please enter the name of the file with the word list: ";
	cin >> filename;
	words.open(filename.c_str());
	if (words.fail())
		cout << "I'm sorry, that file could not be found. Please try again.\n";
	}while(words.fail());
	return;
}

void displaygrid(const char thegrid[][c]){
	//manages all the sub modules that display and change the grid
	colomncounter();
	horizontalborders();
}

displaygrid is where i come into trouble... i have thegrid[r][c] declared in the main... i get an error saying thegrid is missing a subscript... i just don't understand what i am doing wrong

+ i don't think i should have to pass the function additional sizes because these are all global constants

sorry got rid of irrelevent modules
Last edited on Feb 22, 2011 at 3:37am
Feb 22, 2011 at 3:42am
Interesting. It compiles as is for me, ignoring the linker errors to the functions you omitted.
Feb 22, 2011 at 3:42am
kk never mind what i have up there is compiling now... don't know why i was getting errors... but here is another question... my book says i should declare these arrays as const. the reasoning is vague... will i be able to change this array declared in this fashion?
Feb 22, 2011 at 3:43am
No, not if it is const. You should declare them const if you are not going to modify them, which a display function probably shouldn't need to do anyway.
Feb 22, 2011 at 3:49am
This is true, design error on my part... oh well thanks for all the help zhuge your awesome
Topic archived. No new replies allowed.