how to write in terms of reference ?

M

m sergei

main(int argc, char *argv[])

the second parameter, argv, to function main takes a pointer to an array.

can we also write it in terms of a reference rather than a pointer ? (like &..)
 
J

Jack Klein

main(int argc, char *argv[])

the second parameter, argv, to function main takes a pointer to an array.

can we also write it in terms of a reference rather than a pointer ? (like &..)

No, a pointer is not a reference, and a reference is not a pointer.
 
N

Niels Dybdahl

main(int argc, char *argv[])
the second parameter, argv, to function main takes a pointer to an array.

can we also write it in terms of a reference rather than a pointer ? (like
&..)

The main function has been defined to have those parameters, and you can not
change that definition.
In addition argv is an array and arrays can not be referenced as a reference
can not be indexed.

Niels Dybdahl
 
R

Rolf Magnus

m said:
main(int argc, char *argv[])

the second parameter, argv, to function main takes a pointer to an
array.

No, it doesn't. It takes a pointer to a pointer.
can we also write it in terms of a reference rather than a pointer ?
(like &..)

If you mean whether you can write main in such a way that it takes a
reference to an array, the answer is no (typically).
If you mean whether you can use a reference to an array as function
parameter in general, the answer is yes:

#include <iostream>

void foo(int (&x)[3])
{
x[0] = 1;
x[1] = 2;
x[2] = 3;
}

int main()
{
int array[3];
foo(array);
std::cout << array[0] << ' ' << array[1] << ' ' << array[2] << '\n';
}
 
J

JKop

m sergei posted:
main(int argc, char *argv[])

the second parameter, argv, to function main takes a pointer to an
array.

can we also write it in terms of a reference rather than a pointer ?
(like &..)


I know that in your own head you may just think of a reference as a const
pointer, and this is may even be the way a certain compiler works with them
in certain places, but the thing is that it doesn't have to, the Standard
does specify how the behaviour of references is to be achieved, just *that*
it is achieved. For example:

int main()
{
double k;

double& p = k;

p = 53.2342;
}


There won't be a pointer there.


-JKop
 

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
474,174
Messages
2,570,941
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top