Expected ',' or ';' Before '{' Token

Being a beginning (and when I say beginning, I mean that this is my first program apart from the little "Hello World" ones) C++ programmer, theirs probably a very obvious flaw in what I've written so far, but here is what I've written:


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
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int groundArr[10][10];

int mapGen(groundArr[10][10]);

int main(groundArr[10][10])
{
    mapGen(groundArr[10][10]);

    int i;
    int j;
    i = 0;
    j = 0;

    for (j<10,j++)
    {
        for (i<10,i++)
        {
            if (groundArr[i][j] = 1)
            {
                if (i == 1)
                {
                    cout << ".\n";
                }
            }
        }
    }
}

int mapGen(groundArr[10][10])
{
    int i;
    int j;
    i = 0;
    j = 0;
    for (i<10,i++)
    {
        for (j<10,j++)
        {
            groundArr[i][j] = 1;
        }
    }
}



The error that pops up says "Expected ',' or ';' before '{' token".
It's talking about the 10th line of code, but I can't find the error anywhere. Any help is greatly appreciated.
Check your for loops. Their syntax is off.
1
2
3
4
for (initialization; condition; iteration)
{
    //Stuff.
}


Also, your main function cannot take anything as an argument except argc or argv, which are int and char**, respectively.

Also, your function definitions are off. You're supposed to list the type of the argument being passed to the function as well as the name.

int mapGen(int** groundArr);
EDIT: The above is wrong. I use way to many pointers in my programming, really. :) It should've been (as Disch pointed out and as I realized while my browser was refreshing):
int mapGen(int groundArr[][10]);



Have fun!

-Albatross


Last edited on
Thanks Albatross!! I made those changes, and it finally compiled without errors, but when I ran the compiled program, it froze. The little window that says "<program name> has stopped working" popped up and closed the program. Anybody know of a reason this may have happened?
int mapGen(int** groundArr)

This is wrong. Pointers to Pointers are not the same thing as a 2D array.

the right way to do it would be:

int mapGen(int groundArr[][10])

But note:
- this would not work with pointer to pointer arrays (ie: nested new arrays)
- this would not work with arrays that have a different width (ie: int foo[10][9]; would not be able to be passed)


You're also passing the array incorrectly:

1
2
3
4
int main(groundArr[10][10])  // main does not take groundArr as a parameter
{
    mapGen(groundArr[10][10]);  // this does not pass the array, it passes a single element of the array
  // (and an invalid out-of-bounds element, at that) 


The correct way would be:

1
2
3
int main()
{
  mapGen(groundArr);



EDIT: I guess Albatross pointed out main() already XD whoops
Last edited on
Yeah, whoops. Just realized I suggested int** instead of int name[][size]. I use pointers too much.

I also realized I forgot to mention line 13, but if it compiled correctly then you must have changed that.

Although, also, check line 24. Are you using the right equals?

-Albatross
Last edited on
Even name[][] isn't good enough. you need name[][size]
Disch, c'mon. I know that detail, just... meh, I should put it in.

-Albatross
Okay, now it compiles and runs, ending up with a blank screen.

How exactly do you use pointers? I'm not sure what symbols go where.
XD Sorry Albatross.
@Disch: Eh. It's fine.

@Urist: To use pointers, you decide that you're insane and read the document that I will provide a link to below. :)
http://cplusplus.com/doc/tutorial/pointers/

-Albatross
Topic archived. No new replies allowed.