freezway
|
|
Posted on: March 30, 2010, 07:46:15 pm |
|
|
Joined: Dec 2009
Posts: 220
|
Ok, I'm learning C++ and it has occured there are about 3 ways to do the same thing. Here are the 3 functions i wrote up. Is there any difference in their function? are any faster? slower? more standards compliant? are magical?
void printArray(int Array, int width, int height) { int i,j; for(i=0; i < width; i++) { for(j=0; j < height; j++) { cout << Array[i][j]; } cout << endl; } }
void printArray(int& Array, int width, int height) { int i,j; for(i=0; i < width; i++) { for(j=0; j < height; j++) { cout << Array[i][j]; } cout << endl; } }
void printArray(int * Array, int width, int height) { int i,j; for(i=0; i < width; i++) { for(j=0; j < height; j++) { cout << *(Array + (i*width) + j) } cout << endl; } }
|
|
|
Logged
|
if you drop a cat with buttered toast strapped to its back, which side lands down? joshdreamland: our languages are based on the idea that it's going to end up FUBAR /kick retep998
|
|
|
Josh @ Dreamland
|
|
Reply #1 Posted on: March 30, 2010, 07:58:08 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
You meant int** for the first parameter to the first function? Anyway, yes, there is a difference between all of those.
In a function parameter, int a; specifies that "a" is one integer, local to this function. The memory is copied (not a very costly procedure; there's no improving it) for use by the function. int &a; signifies that "a" is passed as a reference, but treated like an integer all the same. This time, the address is copied, which can be of equal or lesser size than the actual integer. This allows the function to edit the contents of "a" in the scope from which it was passed. This is the only time you should use & in parameters. int *a; is pointer to int; it's an integer marking an integer in memory, and must be dereferenced explicitly with * or implicitly with [] for use. int **a; is a pointer to a pointer to an int; it's an integer that points to the address of the integer you want. You can dereference it with any combination of [] and *.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
freezway
|
|
Reply #2 Posted on: March 30, 2010, 08:14:44 pm |
|
|
Joined: Dec 2009
Posts: 220
|
You meant int** for the first parameter to the first function?
No. I didn't. I seem to remember seeing a Cpp tutorial that did that, Is it even legal?
|
|
|
Logged
|
if you drop a cat with buttered toast strapped to its back, which side lands down? joshdreamland: our languages are based on the idea that it's going to end up FUBAR /kick retep998
|
|
|
Josh @ Dreamland
|
|
Reply #3 Posted on: March 30, 2010, 08:18:36 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
Well, that is, but Array[][] isn't if array is just int.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
freezway
|
|
Reply #4 Posted on: March 30, 2010, 08:23:12 pm |
|
|
Joined: Dec 2009
Posts: 220
|
oh, whoops, forgot those []'s. ok, this is good, much less confusing thx.
|
|
|
Logged
|
if you drop a cat with buttered toast strapped to its back, which side lands down? joshdreamland: our languages are based on the idea that it's going to end up FUBAR /kick retep998
|
|
|
RetroX
|
|
Reply #5 Posted on: March 30, 2010, 09:12:08 pm |
|
|
Master of all things Linux
Location: US Joined: Apr 2008
Posts: 1055
|
Just a random thing to mention: add a space between the items in the array.
Also, although people don't seem to realize it, references/pointers use memory, too (4 or 8 bytes, depending on whether or not the system is 32-bit or 64-bit). However, considering how an integer is already 32 bytes, and you're using a 2-dimensional array, using a pointer make more sense. Any array can be assumed as a pointer, and vis versa. Additonally, this is just me, personally, but unsigned integers should be used in unsigned cases. size_t is the same size as a pointer, and an unsigned integer, and it's what you want to use for arrays and such.
This is ideal:
void print_array(int **array, int width, int height) { for (size_t i=0;i<width;i++) { for (size_t j=0;j<height;j++) { std::cout << (i>0 && j>0 ? " " : "") << array[i][j]; } std::cout << std::endl; } }
|
|
« Last Edit: March 30, 2010, 09:13:39 pm by RetroX »
|
Logged
|
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)Why do all the pro-Microsoft people have troll avatars?
|
|
|
|
|
MahFreenAmeh
|
|
Reply #8 Posted on: June 09, 2010, 06:02:51 pm |
|
|
sysadmin
"Web Team" Location: Austin, TX Joined: Apr 2008
Posts: 13
|
you should throw your computer out the window.
|
|
|
Logged
|
sys(tem)admin(istrator) [java,c++,c,javascript,html,css,php,perl,ruby,python,sql] if you've got ideas, let me hear them.
|
|
|
|
|
|
|
|
|
|