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.
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.