Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 5th, 2006, 5:52 PM   #21
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
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
Harakim is offline   Reply With Quote
Old Jun 5th, 2006, 5:57 PM   #22
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
"char *name" and "char name[xx]" are basically the same.
This isn't true. They are essentially different. I use the term, 'essentially', advisedly. One only has to use them as if they are the same (even in a valid way) and examine the emitted code. The compiler tends to hide this difference in some instances, as a perceived favor to the user. Sometimes, if one accepts it literally, as many seem to do these days, it isn't a favor. You might want to refer to the section in my tutorial on pointers (see my sig) entitled, "What is NOT a pointer".
__________________
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
DaWei is offline   Reply With Quote
Old Jun 13th, 2006, 10:14 PM   #23
Manan
Newbie
 
Join Date: May 2006
Posts: 13
Rep Power: 0 Manan is on a distinguished road
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] }
          };
}
The above segment of code works perfectly. However, I want to declare int buffer[][] outside the if statement. This is because, it is currently a local variable and I want to use it outside the if statement. Here is what I'm trying to do:
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] }
          };
}
I get about 50-60 errors when I do this. I've even tried getting rid of the [][], and I also tried making it [9][9], but it won't work. Help is greatly appreciated
Manan is offline   Reply With Quote
Old Jun 13th, 2006, 11:20 PM   #24
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
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.
nindoja is offline   Reply With Quote
Old Jun 13th, 2006, 11:32 PM   #25
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
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);
      }
The Dark is offline   Reply With Quote
Old Jun 14th, 2006, 1:57 AM   #26
Manan
Newbie
 
Join Date: May 2006
Posts: 13
Rep Power: 0 Manan is on a distinguished road
Thanks, but what does the function memcpy() do? Does it assign buffer2 to buffer?
Manan is offline   Reply With Quote
Old Jun 14th, 2006, 2:14 AM   #27
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
Google Is Your Friend
The Dark is offline   Reply With Quote
Old Jun 14th, 2006, 2:16 AM   #28
Manan
Newbie
 
Join Date: May 2006
Posts: 13
Rep Power: 0 Manan is on a distinguished road
Cool, thanks.
Manan is offline   Reply With Quote
Old Jun 14th, 2006, 6:55 PM   #29
Manan
Newbie
 
Join Date: May 2006
Posts: 13
Rep Power: 0 Manan is on a distinguished road
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.
Manan is offline   Reply With Quote
Old Jun 14th, 2006, 7:48 PM   #30
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC