View previous topic :: View next topic |
Author |
Message |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Wed Nov 18, 2009 4:05 pm Post subject: Little question in C++ |
|
|
I know 2 ways to pass arrays to a function.
1st way :
Code: |
void example(int *array)
{
}
int array[2];
example(array);
|
2nd way :
Code: |
void example2(int array[])
{
}
int array[2];
example2(array);
|
Which way is better? and why? I knows int *array will take 4 bytes in 32 bit system from the address of the array, but I don't know how int array[] works. Will it allocate more memory for a copy of the table to manipulate in function?? plz help [/code]
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Wed Nov 18, 2009 4:48 pm Post subject: |
|
|
When you pass an array as an argument, you aren't passing the array itself, you are passing the location of the first element of an array. Arrays and pointers are not the same; an array is a blob of memory the compiler knows about and can describe, whereas a pointer simply points to an element of the specified type. When you pass an array, you loose this information even if you try to describe it, thus either way is fine. Pick whatever suits your style, just remember to be consistent.
Here is a small example for you: http://codepad.org/Rbf6stCS
Personally, if I ever needed to pass an array, I would have the argument list describe the fact that it is a pointer and no longer an array (int* instead of int[10]). Then again, this is C++; you should pass by reference whenever you can.
|
|
Back to top |
|
 |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Wed Nov 18, 2009 5:12 pm Post subject: |
|
|
Thx Athaem, I have no problem because I used to put pointers to manipulate the arrays inside functions, but this is the second year of my lessons in university and there's a dumb freak teacher that he says he doesn't like pointers OMG and he wants that way void example(int array[]) in my exercises. I want to know if int array[] will allocate more memory or it is the same as the 4 byte pointer??
|
|
Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Wed Nov 18, 2009 6:25 pm Post subject: |
|
|
kot1990 wrote: | Thx Athaem, I have no problem because I used to put pointers to manipulate the arrays inside functions, but this is the second year of my lessons in university and there's a dumb freak teacher that he says he doesn't like pointers OMG and he wants that way void example(int array[]) in my exercises. I want to know if int array[] will allocate more memory or it is the same as the 4 byte pointer?? | The pointer is 4 byte yes. I don't think you can pass an int[] as a parameter for a function...
|
|
Back to top |
|
 |
iPromise Grandmaster Cheater
Reputation: -1
Joined: 27 Jun 2009 Posts: 529 Location: Canada
|
Posted: Wed Nov 18, 2009 8:42 pm Post subject: |
|
|
The most efficient method is to:
Code: |
int MyArray[2];
void Example(int *PointerArray)
{
}
|
Faster, easy to understand, and quick to do with the same results.
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Nov 19, 2009 1:36 am Post subject: |
|
|
kot1990 wrote: | Thx Athaem, I have no problem because I used to put pointers to manipulate the arrays inside functions, but this is the second year of my lessons in university and there's a dumb freak teacher that he says he doesn't like pointers OMG and he wants that way void example(int array[]) in my exercises. I want to know if int array[] will allocate more memory or it is the same as the 4 byte pointer?? |
when using an array as a parameter, you must pass it by reference. so no, you're not copying the memory.
&Vage wrote: | The pointer is 4 byte yes. I don't think you can pass an int[] as a parameter for a function... |
you can.
|
|
Back to top |
|
 |
|