C - Pointer to function to function

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

C - Pointer to function to function

Post 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
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: C - Pointer to function to function

Post 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)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: C - Pointer to function to function

Post 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
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: C - Pointer to function to function

Post 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);
}
 
 
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: C - Pointer to function to function

Post 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.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: C - Pointer to function to function

Post by CuteAlien »

I did compile this time, you probably didn't use the exact same code (just code above + #include <cstring> for the memcpy).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply