0

I'm a newbie at C++, but I tried to do my research.However I seem to be not able to figure out what is wrong in my code, probably not familiar with the syntax, that I can't find. Trying to do something bigger, just testing some theory.
Here I have filled a 2D array, put in a function Func(). Lets suppose I did something to it, then I want to retrieve it from there. I imagined I could extract an address to the first array of arrays, but I keep getting the same error.

#include <iostream>
#include <time.h>
#include <cstdlib>
#include <typeinfo>
using namespace std;
char(*)[15] Func(char A[15][15]) //Here is the problem 
{
    A[0][0]='O';
    return A;  //trying to return an address to the A[0]-1D array
}
int main()      //This part isn't really important, just filling an 2-D array
{       
    char A[16][16]; 
    int x,y;
    for (y=0; y<=15;y++)
    {
        for(x=0;x<=15;x++)
        {
            if (3<=x<=12 and 3<=y<=12)
            {
                A[y][x]='*';
            }
            if (x==0 or x==15 or y==0 or y==15)
            {
                A[y][x]='#';
            }
            if ((x==2 and 2<=y<=13) or (x==13 and 2<=y<=13) or (y==2 and 2<=x<=13) or(y==13 and 2<=x<=13))
            {
                A[y][x]='#';
            }
            if (x==14 or y==14 or (x==1 and y==1))
            {
                A[y][x]='#';
            }       
        }
        
    }
    for (int i=0,y=3, x=1; y<=12; y++, i++ )
    {
        char j='0'+i;
        A[y][x]=j;
    }
    for (int i=0,y=1, x=3; x<=12; x++, i++)
    {
        char j='0'+i;
        A[y][x]=j;
    }
    for (y=0; y<=15;y++)
    {
        for(x=0;x<=15;x++){
            cout<<A[y][x];
        }
        cout<<endl;  
    }                      
    char(*p)[15]=Func(A[15][15]);    //here Im trying to assign pointer p to point to the first 1D array of 15x15 Array of A (To do pointer arithmetics later)
    cout<<"p="<<p; //Just testing
    return 0;
}

And I get this error:

expected unqualified-id before ')' token
expected initializer before 'Func'

Both directed at Func() function declaration line
Plus this error: 'Func' was not declared in this scope
Which is not pleasant too but less engaging than the previous ones, in my opinion.
I think the problem might be in the type of "A" (the pointer) or there is something I missed. How can I fix the issues of this code? It feels like I have tried everything! Thank you.

Bzzzt
  • 1
  • Use `std::array` or `std::vector` – Thomas Sablik Nov 20 '20 at 08:35
  • On an unrelated note, the comparison `3<=x<=12` is the same as `(3<=x)<=12`. I.e. you compare the boolean `0` or `1` result of `3<=x` with `12`, and both `0` and `1` are less than `12` which means it will *always* be true. – Some programmer dude Nov 20 '20 at 08:42
  • The call `Func(A[15][15])` is wrong as well, you only pass a *single* `char` value to the function. It seems you need to take a few steps back, get [some decent books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), and maybe take some classes as well, and learn or relearn some of the basics of C++. – Some programmer dude Nov 20 '20 at 08:43

2 Answers2

0

You could use std::array instead. I also fixed the problems mentioned in the comments:

#include <array>
#include <iostream>
using namespace std;

using Field = std::array<std::array<char, 16>, 16>;
Field Func(Field A) //Here is the problem 
{
    A[0][0]='O';
    return A;  //trying to return an address to the A[0]-1D array
}
int main()      //This part isn't really important, just filling an 2-D array
{       
    Field A; 
    int x,y;
    for (y=0; y<=15;y++)
    {
        for(x=0;x<=15;x++)
        {
            if (3<=x and x<=12 and 3<=y and y<=12)
            {
                A[y][x]='*';
            }
            if (x==0 or x==15 or y==0 or y==15)
            {
                A[y][x]='#';
            }
            if ((x==2 and 2<=y and y<=13) or (x==13 and 2<=y and y<=13) or (y==2 and 2<=x and x<=13) or(y==13 and 2<=x and x<=13))
            {
                A[y][x]='#';
            }
            if (x==14 or y==14 or (x==1 and y==1))
            {
                A[y][x]='#';
            }       
        }
        
    }
    for (int i=0,y=3, x=1; y<=12; y++, i++ )
    {
        char j='0'+i;
        A[y][x]=j;
    }
    for (int i=0,y=1, x=3; x<=12; x++, i++)
    {
        char j='0'+i;
        A[y][x]=j;
    }
    for (y=0; y<=15;y++)
    {
        for(x=0;x<=15;x++){
            cout<<A[y][x];
        }
        cout<<endl;  
    }                      
    auto p = Func(A);    //here Im trying to assign pointer p to point to the first 1D array of 15x15 Array of A (To do pointer arithmetics later)
    cout<<"p=\n";
    for (const auto &row : p) {
        for (const auto &el : row)
            cout << el; //Just testing
        cout << '\n';
    }
    return 0;
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
0

You need C's "declaration follows use" principle.

You would access a single array element of the result as

(*Func(some_array))[x]

so the prototype would be

char (*Func(char A[15][15]))[15]

But it's slightly more readable to use a type alias instead. For instance,

using FifteenChars = char[15];

FifteenChars* Func(FifteenChars A[15])
{
    return A;
}

There are a few other issues:

There is a conflict between your array sizes.

A[15][15] is not the top left part of A but a single char, the one in the bottom right corner.

3<=x<=12 is not 3<=x && x<=12, but (3<=x)<=12, and 3<=x converted to int is either 0 or 1.
You need to spell it out, or split the loop into several individual parts.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82