![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
Although this isn't technically true "char *name" and "char name[xx]" are basically the same.
When you are passing an array in C, you can pass it as a pointer and dereference it with brackets. int foo( char *bar )
{
bar[1] = 100;
}
int foo2()
{
char bar2[20];
foo( bar2 );
//bar2[1] now equals 100
}I have heard that they are not the same at all, so hopefully someone will be enraged by my saying they are and come and explain it. As for passing arrays though, I'm pretty sure that should work. int foo( char **bar )
{
bar[1][2] = 100;
}
int foo2()
{
char bar2[20][10];
foo( bar2 );
//bar2[1][2] now equals 100
}EDIT: fixed error in code |
|
|
|
|
|
#22 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#23 |
|
Newbie
Join Date: May 2006
Posts: 13
Rep Power: 0
![]() |
I've decided to use a template, because I have way too little time to actually do a totally random generator. But I'm having another problem declaring 2d arrays.
if(randTemplate == 1)
{ //If template 2 is chosen
int buffer[9][9] = {
{n[6],n[7],n[8], n[1],n[0],n[4], n[2],n[5],n[3] },
{n[1],n[4],n[5], n[2],n[8],n[3], n[6],n[0],n[7] },
{n[2],n[0],n[3], n[5],n[7],n[6], n[4],n[1],n[8] },
{n[8],n[2],n[1], n[7],n[6],n[0], n[5],n[3],n[4] },
{n[5],n[3],n[0], n[4],n[2],n[8], n[1],n[7],n[6] },
{n[7],n[6],n[4], n[3],n[1],n[5], n[8],n[2],n[0] },
{n[0],n[5],n[7], n[8],n[4],n[2], n[3],n[6],n[1] },
{n[4],n[1],n[2], n[6],n[3],n[7], n[0],n[8],n[5] },
{n[3],n[8],n[6], n[0],n[5],n[1], n[7],n[4],n[2] }
};
}int buffer[9][9];
if(randTemplate == 1)
{ //If template 2 is chosen
buffer[][] = {
{n[6],n[7],n[8], n[1],n[0],n[4], n[2],n[5],n[3] },
{n[1],n[4],n[5], n[2],n[8],n[3], n[6],n[0],n[7] },
{n[2],n[0],n[3], n[5],n[7],n[6], n[4],n[1],n[8] },
{n[8],n[2],n[1], n[7],n[6],n[0], n[5],n[3],n[4] },
{n[5],n[3],n[0], n[4],n[2],n[8], n[1],n[7],n[6] },
{n[7],n[6],n[4], n[3],n[1],n[5], n[8],n[2],n[0] },
{n[0],n[5],n[7], n[8],n[4],n[2], n[3],n[6],n[1] },
{n[4],n[1],n[2], n[6],n[3],n[7], n[0],n[8],n[5] },
{n[3],n[8],n[6], n[0],n[5],n[1], n[7],n[4],n[2] }
};
} |
|
|
|
|
|
#24 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
After you declare the array you cannot use an initializer list to declare the values. You will have to either loop through or have individual assignments.
|
|
|
|
|
|
#25 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
Or you could try something like:
int buffer[9][9];
if(randTemplate == 1)
{ //If template 2 is chosen
int buffer2[9][9] = {
{n[6],n[7],n[8], n[1],n[0],n[4], n[2],n[5],n[3] },
{n[1],n[4],n[5], n[2],n[8],n[3], n[6],n[0],n[7] },
{n[2],n[0],n[3], n[5],n[7],n[6], n[4],n[1],n[8] },
{n[8],n[2],n[1], n[7],n[6],n[0], n[5],n[3],n[4] },
{n[5],n[3],n[0], n[4],n[2],n[8], n[1],n[7],n[6] },
{n[7],n[6],n[4], n[3],n[1],n[5], n[8],n[2],n[0] },
{n[0],n[5],n[7], n[8],n[4],n[2], n[3],n[6],n[1] },
{n[4],n[1],n[2], n[6],n[3],n[7], n[0],n[8],n[5] },
{n[3],n[8],n[6], n[0],n[5],n[1], n[7],n[4],n[2] }
};
memcpy (buffer, buffer2, sizeof buffer);
} |
|
|
|
|
|
#26 |
|
Newbie
Join Date: May 2006
Posts: 13
Rep Power: 0
![]() |
Thanks, but what does the function memcpy() do? Does it assign buffer2 to buffer?
|
|
|
|
|
|
#27 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
|
|
|
|
|
|
#28 |
|
Newbie
Join Date: May 2006
Posts: 13
Rep Power: 0
![]() |
Cool, thanks.
|
|
|
|
|
|
#29 |
|
Newbie
Join Date: May 2006
Posts: 13
Rep Power: 0
![]() |
Wow, memcpy is actually way better than simply assigning matrices through 2 nested for loops. Thanks, it really shortened my code by about 30 lines.
|
|
|
|
|
|
#30 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
Note that you only want to do it for simple types, once you start having classes in your matrices, you have to be careful that you don't muck up any allocated pointers.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|