Page 1 of 1
C - Pointer to function to function
Posted: Wed Feb 22, 2017 3:10 pm
by LunaRebirth
I tried googling this question, and maybe I couldn't word it correctly because I was having no luck with a solution.
Code: Select all
void func2(char data[60][60]) {
data[0][0] = 'x';
}
void func1(char data[60][60]) {
func2(&data);
}
int main() {
char data[60][60];
// ... Fill data with characters ...
data[0][0] = 'A';
char* newData = data;
func1(&newData);
// I want newData[0][0] to now hold 'x' while keeping data[0][0] holding 'A'
}
I feel like I've tried every combination of using pointers and I'm getting no luck.
Any help would be appreciated
Re: C - Pointer to function to function
Posted: Wed Feb 22, 2017 4:52 pm
by CuteAlien
newData is a pointer to data which means it points to the exact same place in memory. The same memory can't have 2 different values - otherwise you could double the memory in your computer per software (which would be nice...). So you need to create a copy first:
newData = new char[60][60]; memcpy(newData, data, sizeof data);
The next thing is - the array you pass as parameter is a real copy. So anything changed inside the function won't be returned.
If you want to return it then pass a reference to an array like: func(char (&data)[60][60]). Which can call then with func(data) or func(*newData). I had to look-up reference to array syntax as well btw ;-) (hope that works with 2-dimensional arrays, I did not compile...)
Personally I find it mostly easier to work with one-dimensional arrays and doing the access calculation myself (y*width+x). Especially easier once you want the size to be dynamic. (and it's not slower than 2-dimensional arrays as the compiler will have to do the y*width+x to acccess elements as well)
Re: C - Pointer to function to function
Posted: Wed Feb 22, 2017 7:54 pm
by LunaRebirth
I can't pass in char (&data)[60][60], that gives a syntax error.
Here's what I've tried:
Code: Select all
void func2(char* data[60][60]) {
data[0][0] = 'x';
}
void func1(char* data[60][60]) {
func2(&data);
}
int main() {
char data[60][60];
// ... Fill data with characters ...
data[0][0] = 'A';
char newData[60][60]; memcpy(newData, data, sizeof(data));
func1(&newData);
// I want newData[0][0] to now hold 'x' while keeping data[0][0] holding 'A'
}
Didn't work
Re: C - Pointer to function to function
Posted: Wed Feb 22, 2017 9:03 pm
by CuteAlien
What I meant was:
Code: Select all
void func2(char (&data)[60][60]) {
data[0][0] = 'x';
}
void func1(char (&data)[60][60]) {
func2(data);
}
int main() {
char data[60][60];
// ... Fill data with characters ...
data[0][0] = 'A';
char newData[60][60]; memcpy(newData, data, sizeof(data));
func1(newData);
}
Re: C - Pointer to function to function
Posted: Wed Feb 22, 2017 10:03 pm
by LunaRebirth
CuteAlien wrote:What I meant was:
Code: Select all
void func2(char (&data)[60][60]) {
data[0][0] = 'x';
}
void func1(char (&data)[60][60]) {
func2(data);
}
int main() {
char data[60][60];
// ... Fill data with characters ...
data[0][0] = 'A';
char newData[60][60]; memcpy(newData, data, sizeof(data));
func1(newData);
}
Yepp,
Code: Select all
error: expected ')' before '&' token
I solved the problem by not using pointers and just declaring my 2D arrays outside of the functions instead.
Re: C - Pointer to function to function
Posted: Wed Feb 22, 2017 10:08 pm
by CuteAlien
I did compile this time, you probably didn't use the exact same code (just code above + #include <cstring> for the memcpy).