asign to return value
-
struct TestStruct
{
int x;
int z;};
TestStruct S;
TestStruct Funct()
{
return S;
}//
Funct().x = 100;Will this work?
-
struct TestStruct
{
int x;
int z;};
TestStruct S;
TestStruct Funct()
{
return S;
}//
Funct().x = 100;Will this work?
Yes, but
Func()
is not really necessary, since you have already declaredS
. You can just sayS.x = 100;
Creating a function to return a structure (or anything else) is usually necessary when you cannot declare the relevant object at compile time, but need to create it dynamically. Something like:TestStruct* Funct()
{
TestStruct* pS = new TestStruct;
pS->x = 0;
ps->z = 0;
// other initialisation codereturn pS;
}
//
TestStruct* pNewStruct = Funct();
pNewStruct->x = 100; -
struct TestStruct
{
int x;
int z;};
TestStruct S;
TestStruct Funct()
{
return S;
}//
Funct().x = 100;Will this work?
Hello, the code itself will work, but will not do what I suppose you expect. Here, Funct returns a copy of the TestStruct S. So Funct().x = 100 will affect 100 in a temporary TestStruct that you can not use after that. I don't know why you want to use a function to access your S structure, but the behavior you look for is probably :
// Funct returns a reference on S structure.
TestStruct& Funct()
{
return S;
} -
Hello, the code itself will work, but will not do what I suppose you expect. Here, Funct returns a copy of the TestStruct S. So Funct().x = 100 will affect 100 in a temporary TestStruct that you can not use after that. I don't know why you want to use a function to access your S structure, but the behavior you look for is probably :
// Funct returns a reference on S structure.
TestStruct& Funct()
{
return S;
}my actual code:
ChartNode Chart[100];
ChartNode NodeCoord(int x, int z)
{
return Chart[z * 10 + x];
}if(NodeCoord(4,4).access)
//do something
NodeCoord(4,4).access = false;The result I`m looking for is the same as the result achieved with this function:
void NodeCoord(int x, int z, bool writetobool)
{
Chart[z * 10 + x].access = writetobool;
} -
my actual code:
ChartNode Chart[100];
ChartNode NodeCoord(int x, int z)
{
return Chart[z * 10 + x];
}if(NodeCoord(4,4).access)
//do something
NodeCoord(4,4).access = false;The result I`m looking for is the same as the result achieved with this function:
void NodeCoord(int x, int z, bool writetobool)
{
Chart[z * 10 + x].access = writetobool;
}So you probably need to return a reference :
ChartNode& NodeCoord(int x, int z)
{
return Chart[z * 10 + x];
}else the
NodeCoord(4,4).access = false
will assign a temporary copy of your ChartNode struct and not the one in your array. -
So you probably need to return a reference :
ChartNode& NodeCoord(int x, int z)
{
return Chart[z * 10 + x];
}else the
NodeCoord(4,4).access = false
will assign a temporary copy of your ChartNode struct and not the one in your array.ChartNode& NodeCoord() will work for both read and write?
-
my actual code:
ChartNode Chart[100];
ChartNode NodeCoord(int x, int z)
{
return Chart[z * 10 + x];
}if(NodeCoord(4,4).access)
//do something
NodeCoord(4,4).access = false;The result I`m looking for is the same as the result achieved with this function:
void NodeCoord(int x, int z, bool writetobool)
{
Chart[z * 10 + x].access = writetobool;
}If you are referring to array cells with two dimension values then you should use a two-dimensional array. As it is you can pass any values in to
NodeCoord
but there is no way of telling if they are valid. So you could end up with lots of random memory corruption. -
If you are referring to array cells with two dimension values then you should use a two-dimensional array. As it is you can pass any values in to
NodeCoord
but there is no way of telling if they are valid. So you could end up with lots of random memory corruption.Thanks Richard How do I declare a two (or more) dimensional array? The problem is I might need arrays with a great number of dimensions. I need a `one fits all` type of solution.
-
Thanks Richard How do I declare a two (or more) dimensional array? The problem is I might need arrays with a great number of dimensions. I need a `one fits all` type of solution.
Just like a one-dimensional, but you declare the two dimensions. Think of it as a block of items having some number of rows and columns. so you could have something like:
#define MAXROW 10
#define MAXCOL 5ChartNode Chart[MAXROW][MAXCOL];
ChartNode NodeCoord(int row, int column)
{
if (row > 0 && row < MAXROW && // make sure row index
column > 0 && column < MAXCOL) // and column index are within range
{
return Chart[row, column]; // return the cell
}
else
{
return NULL; // error
}
}All of these features are well documented in the C/C++ documentation and the various tutorials on the language.
-
ChartNode& NodeCoord() will work for both read and write?
Yes.
-
Just like a one-dimensional, but you declare the two dimensions. Think of it as a block of items having some number of rows and columns. so you could have something like:
#define MAXROW 10
#define MAXCOL 5ChartNode Chart[MAXROW][MAXCOL];
ChartNode NodeCoord(int row, int column)
{
if (row > 0 && row < MAXROW && // make sure row index
column > 0 && column < MAXCOL) // and column index are within range
{
return Chart[row, column]; // return the cell
}
else
{
return NULL; // error
}
}All of these features are well documented in the C/C++ documentation and the various tutorials on the language.
the two dimensional array seems like something too good to be true :) The compiler is definitely doing something suspicious behind the courtain. Thanks.
-
the two dimensional array seems like something too good to be true :) The compiler is definitely doing something suspicious behind the courtain. Thanks.
-
struct TestStruct
{
int x;
int z;};
TestStruct S;
TestStruct Funct()
{
return S;
}//
Funct().x = 100;Will this work?
Often I edit my posts/replies shortly after making them, I can`t organise myself in a single swing.
-
the two dimensional array seems like something too good to be true :) The compiler is definitely doing something suspicious behind the courtain. Thanks.
Internally it's just continuous memory that is allocated for a 2 dimensional array (For any no. of dimensions for that matter). The compiler uses the specified dimensions to calculate the offset into the memory to fetch. For instance, offset of
Chart[2][3]
could be calculated as -(sizeof(ChartNode) * MAXROW * 2) + (sizeof(ChartNode) * 3)
«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)