Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > array to pointer decay question !!

Reply
Thread Tools

array to pointer decay question !!

 
 
m sergei
Guest
Posts: n/a
 
      06-30-2004
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
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      06-30-2004
(m sergei) wrote:

> 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
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      06-30-2004
m sergei wrote:
> 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

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-30-2004
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)


> 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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-30-2004
In <> Default User <> writes:

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

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
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-30-2004
Dan Pop wrote:
>
> In <> Default User <> writes:
>
> >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.




Brian Rodenborn
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-01-2004
In <> Default User <> writes:

>Dan Pop wrote:
>>
>> In <> Default User <> writes:
>>
>> >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
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
module for calculations involving radioactive decay? frederik aa de jonge Python 0 11-02-2006 04:07 AM
Question about function decay Snis Pilbor C Programming 5 08-05-2006 10:10 AM
array to pointer decay question !! pandapower C Programming 5 02-02-2004 01:38 PM
array decay Mantorok Redgormor C Programming 2 10-09-2003 03:32 AM
array decay Mantorok Redgormor C Programming 1 09-23-2003 12:45 PM



Advertisments