array to pointer decay question !!

M

m sergei

main(int argc, char *argv[])

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

can we also write it in terms of a reference rather than a pointer ?
an example of using (usage) as a reference would be helpful
 
R

Richard Bos

main(int argc, char *argv[])

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

can we also write it in terms of a reference rather than a pointer ?

Of course not. References are C++, not C.

Perhaps you want comp.lang.c++?

Richard
 
T

Thomas Matthews

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

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

can we also write it in terms of a reference rather than a pointer ?
an example of using (usage) as a reference would be helpful
No, references are C++.

You can declare it as: char * * arg_list.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
D

Default User

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

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

No it doesn't. It takes a pointer to a pointer to char. That's why an
equivalent version is:

int main (int argc, char **argv)

can we also write it in terms of a reference rather than a pointer ?

Ask in comp.lang.c++ where they will tell you that it is not the same.




Brian Rodenborn
 
D

Dan Pop

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

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

No it doesn't. It takes a pointer to a pointer to char. That's why an
equivalent version is:

int main (int argc, char **argv)

However, the equivalent version is losing a semantic clue: that argv is
supposed to point to the first pointer of an array of pointers. It makes
no difference to the compiler, but it does make a difference to the
human reader.

Furthermore it is quite idiomatic to refer to a pointer to the first
element of an array as a pointer to an array, as long as it is clear that
its type is not pointer to array. Example from the C standard itself:

2 The strcpy function copies the string pointed to by s2 (including
the terminating null character) into the array pointed to by
^^^^^^^^^^^^^^^^^^^^^^^
s1. If copying takes place between objects that overlap, the
^^
behavior is undefined.

The case of main being so well known, most advanced programmers prefer
**argv because it is easier to type than *argv[]. However, in any other
function interface, I would recommend *argv[] instead of **argv, due to
the semantic clue I was talking above. It's much less of an issue when
dealing with plain arrays, so I wouldn't bother with a[] instead of *p.

Dan
 
D

Default User

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

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

No it doesn't. It takes a pointer to a pointer to char. That's why an
equivalent version is:

int main (int argc, char **argv)

However, the equivalent version is losing a semantic clue: that argv is
supposed to point to the first pointer of an array of pointers. It makes
no difference to the compiler, but it does make a difference to the
human reader.

I agree with you there, I use the char *argv[] style.
Furthermore it is quite idiomatic to refer to a pointer to the first
element of an array as a pointer to an array, as long as it is clear that
its type is not pointer to array.

But when we're talking about equivalent types, the syntax is more
important than the semantics. If we were describing the usage of argv,
then you'd be correct. I don't agree with you regarding the OP's
question.




Brian Rodenborn
 
D

Dan Pop

In said:
Dan said:
In said:
m sergei wrote:

main(int argc, char *argv[])

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

No it doesn't. It takes a pointer to a pointer to char. That's why an
equivalent version is:

int main (int argc, char **argv)

However, the equivalent version is losing a semantic clue: that argv is
supposed to point to the first pointer of an array of pointers. It makes
no difference to the compiler, but it does make a difference to the
human reader.

I agree with you there, I use the char *argv[] style.
Furthermore it is quite idiomatic to refer to a pointer to the first
element of an array as a pointer to an array, as long as it is clear that
its type is not pointer to array.

But when we're talking about equivalent types, the syntax is more
important than the semantics. If we were describing the usage of argv,
then you'd be correct. I don't agree with you regarding the OP's
question.

Then, you're not agreeing with the C standard itself, either, which uses
the same "sloppy" terminology, as I have shown in the part you have so
conveniently snipped from my post.

Dan
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top