I don't get a error message when I compile the code but I cant a proper result.
#include <iostream>
using namespace std;
struct Coord{
int r;
int c;
};
struct CoordwValue{
Coord C;
char Value;
};
CoordwValue* getNeighbors();
int main (){
CoordwValue *k= getNeighbors();
for (int i=0;i<4;i++)
cout<<(k[i].Value);
}
CoordwValue *getNeighbors(){
CoordwValue Neighbors[4];
Neighbors->Value='X';
Neighbors->C.r= 0;
Neighbors->C.c= 1;
(Neighbors+1)->Value='0';
(Neighbors+1)->C.r= 1;
(Neighbors+1)->C.c= 2;
(Neighbors+2)->Value='1';
(Neighbors+2)->C.r= 2;
(Neighbors+2)->C.c= 1;
(Neighbors+3)->Value='X';
(Neighbors+3)->C.r= 1;
(Neighbors+3)->C.c= 0;
//for (int i=0;i<4;i++)
// cout<<Neighbors[i].Value;
return Neighbors;
}
This part of the code prints X01X
for (int i=0;i<4;i++)
cout<<Neighbors[i].Value;
But I can't get the same result from
for (int i=0;i<4;i++)
cout<<(k[i].Value);
What is the problem?
Edit:
This version of the code works fine.
#include <iostream>
using namespace std;
char* getNeighbors();
int main (){
char *k= getNeighbors();
for (int i=0;i<4;i++)
cout<<(*(k+i));
}
char *getNeighbors(Coord C, int r){
char Neighbors[4];
*Neighbors='X';
*(Neighbors+1)='0';
*(Neighbors+2)='1';
*(Neighbors+3)='X'
return Neighbors;
}