Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   passing an array as argument (http://www.velocityreviews.com/forums/t318019-passing-an-array-as-argument.html)

monkeys paw 04-27-2004 10:44 PM

passing an array as argument
 
I am passing an array to a function. My first attempt was like so:

char *ary[] = {"an", "array"};
my_sort(ary);

But that doesn't work because the function my_sort cannot
determing the size of ary. So i had to do this:

nelems = sizeof(ary) / sizeof(char *);
my_sort(ary, nelems);

Why is this?


Russell Hanneken 04-28-2004 01:06 AM

Re: passing an array as argument
 
monkeys paw wrote:
> I am passing an array to a function. My first attempt was like so:
>
> char *ary[] = {"an", "array"};
> my_sort(ary);
>
> But that doesn't work because the function my_sort cannot
> determing the size of ary. So i had to do this:
>
> nelems = sizeof(ary) / sizeof(char *);
> my_sort(ary, nelems);
>
> Why is this?


This is addressed in the FAQ:

http://www.eskimo.com/~scs/C-faq/q6.21.html

--
Russell Hanneken
rghanneken@pobox.com
Remove the 'g' from my address to send me mail.

Barry Schwarz 04-28-2004 02:43 AM

Re: passing an array as argument
 
On Tue, 27 Apr 2004 22:44:05 GMT, monkeys paw
<fm_duendeBASURA@yahoo.com> wrote:

>I am passing an array to a function. My first attempt was like so:
>
>char *ary[] = {"an", "array"};
>my_sort(ary);
>
>But that doesn't work because the function my_sort cannot
>determing the size of ary. So i had to do this:
>
>nelems = sizeof(ary) / sizeof(char *);
>my_sort(ary, nelems);
>
>Why is this?


See the faq at http://www.eskimo.com/~scs/C-faq/top.html (section 6)
or google the archives. This gets discussed at least monthly.


<<Remove the del for email>>

macluvitch 04-28-2004 10:26 AM

Re: passing an array as argument
 
keep in mind that the size of a pointer is 4 bytes in a 64bit system

the prototype of myµ_sort should be sth like

return type my_sort (char **ary,.......)



Richard Bos 04-28-2004 10:58 AM

Re: passing an array as argument
 
"macluvitch" <macluvitch@hotmail.com> wrote:

> keep in mind that the size of a pointer is 4 bytes in a 64bit system


Or better, do not; the Standard says nothing about the size of pointers,
and in fact it's quite possible for pointers to different types to be
different sizes.

> the prototype of myµ_sort should be sth like
>
> return type my_sort (char **ary,.......)


That's not a prototype, it's a return statement, and a broken one at
that.

Richard

Dan Pop 04-28-2004 12:10 PM

Re: passing an array as argument
 
In <e78e777a530ff915bf43f18fd1e815b0@localhost.talkab outprogramming.com> "macluvitch" <macluvitch@hotmail.com> writes:

>keep in mind that the size of a pointer is 4 bytes in a 64bit system


Engage your brain and explain to the rest of us how can a 4-byte pointer
cover a 64-bit address space.

It is possible to use 64-bit hardware in 32-bit mode (as older OpenVMS
versions and Windows/NT did on Alpha and older IRIX versions did on MIPS
4000 and above), but the resulting system does NOT qualify as a 64-bit
system.

For a C implementation to qualify as a 64-bit implementation, it needs
64-bit pointers and at least one standard 64-bit integer type. gcc on
64-bit Linux systems has 64-bit pointers and longs (it uses the
I32LP64 model, according to a common jargon).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de

Ben Pfaff 04-28-2004 05:23 PM

Re: passing an array as argument
 
Dan.Pop@cern.ch (Dan Pop) writes:

> In <e78e777a530ff915bf43f18fd1e815b0@localhost.talkab outprogramming.com> "macluvitch" <macluvitch@hotmail.com> writes:
>
>>keep in mind that the size of a pointer is 4 bytes in a 64bit system

>
> Engage your brain and explain to the rest of us how can a 4-byte pointer
> cover a 64-bit address space.


With 16-bit bytes?
--
Just another C hacker.

Stephen Sprunk 04-28-2004 05:24 PM

Re: passing an array as argument
 
"Dan Pop" <Dan.Pop@cern.ch> wrote in message
news:c6o6vj$mrn$1@sunnews.cern.ch...
> In <e78e777a530ff915bf43f18fd1e815b0@localhost.talkab outprogramming.com>

"macluvitch" <macluvitch@hotmail.com> writes:
>
> >keep in mind that the size of a pointer is 4 bytes in a 64bit system

>
> Engage your brain and explain to the rest of us how can a 4-byte pointer
> cover a 64-bit address space.


Well, CHAR_BIT could be 16 :)

> It is possible to use 64-bit hardware in 32-bit mode (as older OpenVMS
> versions and Windows/NT did on Alpha and older IRIX versions did on MIPS
> 4000 and above), but the resulting system does NOT qualify as a 64-bit
> system.
>
> For a C implementation to qualify as a 64-bit implementation, it needs
> 64-bit pointers and at least one standard 64-bit integer type. gcc on
> 64-bit Linux systems has 64-bit pointers and longs (it uses the
> I32LP64 model, according to a common jargon).


Would you consider an IP32L64 system as 64-bit? It's certainly possible to
implement a "tiny" memory model for AMD64, giving you the better memory
efficiency of 32-bit pointers while retaining access to features not
available in 32-bit mode. And even in the small and kernel code models,
function pointers need only be 32-bit anyway, with a sign extension if
converted to a void pointer.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin


Martin Ambuhl 04-28-2004 11:14 PM

Re: passing an array as argument
 
Dan Pop wrote:

> In <e78e777a530ff915bf43f18fd1e815b0@localhost.talkab outprogramming.com> "macluvitch" <macluvitch@hotmail.com> writes:
>>keep in mind that the size of a pointer is 4 bytes in a 64bit system


> Engage your brain and explain to the rest of us how can a 4-byte pointer
> cover a 64-bit address space.


By using 16-bit bytes, obviously.

Dan Pop 04-29-2004 11:09 AM

Re: passing an array as argument
 
In <877jw09fug.fsf@blp.benpfaff.org> Ben Pfaff <blp@cs.stanford.edu> writes:

>Dan.Pop@cern.ch (Dan Pop) writes:
>
>> In <e78e777a530ff915bf43f18fd1e815b0@localhost.talkab outprogramming.com> "macluvitch" <macluvitch@hotmail.com> writes:
>>
>>>keep in mind that the size of a pointer is 4 bytes in a 64bit system

>>
>> Engage your brain and explain to the rest of us how can a 4-byte pointer
>> cover a 64-bit address space.

>
>With 16-bit bytes?


I was talking about *existing*, *real life*, 64-bit implementations, as I
assumed the previous poster did, rather than pipe dream ones.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


All times are GMT. The time now is 03:02 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.