How to pass an array to a function

  • Thread starter Vicent Giner-Bosch
  • Start date
V

Vicent Giner-Bosch

Hello to all.

I have a basic question. It is a quite "philosophic" one. Maybe it is
only a matter of style, but I would like you to help me to learn, if
possible. So, there I go.

Say I have allocated an array in this way:

....
int *x ;

int asize = 10 ;

x = new int[asize + 1] ; // "+1" is not necessary, but I put it here
anyway, because I am not going to use x[0], etc.
....

And then, I want to pass that array "by reference" to a function in
order to read it and, maybe, modify it (in fact, in the moment I call
the function, the array "x" contains nosense).

So, I would call it like this:

....
funct(x , asize) ;
....

I mean, I would give the function the following information: the
"address" where the array "starts" and the size of the array.

Supose I want to do a call like above. So, MY QUESTION IS: how should
the function be declared?? I don't know what of the following is
better:

OPTION 1 :
....
funct(int *x , int array_size){
....

OPTION 2:
....
funct(int x[] , int array_size){
....

Are both right? Are they equivalent?? Which is the difference between
them, if any?

Thank you in advance for your help. :)
 
S

Saeed Amrollahi

Hello to all.

I have a basic question. It is a quite "philosophic" one. Maybe it is
only a matter of style, but I would like you to help me to learn, if
possible. So, there I go.

Say I have allocated an array in this way:

...
int *x ;

int asize = 10 ;

x = new int[asize + 1] ;   // "+1" is not necessary, but I put it here
anyway, because I am not going to use x[0], etc.
...

And then, I want to pass that array "by reference" to a function in
order to read it and, maybe, modify it (in fact, in the moment I call
the function, the array "x" contains nosense).

So, I would call it like this:

...
funct(x , asize) ;
...

I mean, I would give the function the following information: the
"address" where the array "starts" and the size of the array.

Supose I want to do a call like above. So, MY QUESTION IS: how should
the function be declared?? I don't know what of the following is
better:

OPTION 1 :
...
funct(int *x , int array_size){
...

OPTION 2:
...
funct(int x[] , int array_size){
...

Are both right? Are they equivalent??  Which is the difference between
them, if any?

Thank you in advance for your help.   :)

Hi

I believe both OPTIONS are correct and equivalent and I think there is
no
significant difference between them.
IIRC correctly, because the name of array is the pointer to the first
element, most implementations convert the declaration/definition
OPTION 2 to OPTION 1. As you wrote, because array don't know its size,
you have to supply the array_size.

Regards,
-- Saeed Amrollahi
 
B

Bo Persson

Saeed said:
Hello to all.

I have a basic question. It is a quite "philosophic" one. Maybe it
is only a matter of style, but I would like you to help me to
learn, if possible. So, there I go.

Say I have allocated an array in this way:

...
int *x ;

int asize = 10 ;

x = new int[asize + 1] ; // "+1" is not necessary, but I put it
here anyway, because I am not going to use x[0], etc.
...

And then, I want to pass that array "by reference" to a function in
order to read it and, maybe, modify it (in fact, in the moment I
call the function, the array "x" contains nosense).

So, I would call it like this:

...
funct(x , asize) ;
...

I mean, I would give the function the following information: the
"address" where the array "starts" and the size of the array.

Supose I want to do a call like above. So, MY QUESTION IS: how
should the function be declared?? I don't know what of the
following is better:

OPTION 1 :
...
funct(int *x , int array_size){
...

OPTION 2:
...
funct(int x[] , int array_size){
...

Are both right? Are they equivalent?? Which is the difference
between them, if any?

Thank you in advance for your help. :)

Hi

I believe both OPTIONS are correct and equivalent and I think there
is no
significant difference between them.
IIRC correctly, because the name of array is the pointer to the
first element, most implementations convert the
declaration/definition OPTION 2 to OPTION 1. As you wrote, because
array don't know its size, you have to supply the array_size.

This is all correct.

We are just missing the standard C++ hint of using std::vector
instead, as it takes care of everything, including knowing its own
size and not forgetting to call delete[] at the end.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top