Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > passing an array as argument

Reply
Thread Tools

passing an array as argument

 
 
monkeys paw
Guest
Posts: n/a
 
      04-27-2004
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?

 
Reply With Quote
 
 
 
 
Russell Hanneken
Guest
Posts: n/a
 
      04-28-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove the 'g' from my address to send me mail.
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-28-2004
On Tue, 27 Apr 2004 22:44:05 GMT, monkeys paw
<(E-Mail Removed)> 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>>
 
Reply With Quote
 
macluvitch
Guest
Posts: n/a
 
      04-28-2004
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,.......)


 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      04-28-2004
"macluvitch" <(E-Mail Removed)> 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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      04-28-2004
In <(E-Mail Removed) outprogramming.com> "macluvitch" <(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      04-28-2004
(E-Mail Removed) (Dan Pop) writes:

> In <(E-Mail Removed) outprogramming.com> "macluvitch" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      04-28-2004
"Dan Pop" <(E-Mail Removed)> wrote in message
news:c6o6vj$mrn$(E-Mail Removed)...
> In <(E-Mail Removed) outprogramming.com>

"macluvitch" <(E-Mail Removed)> 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

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-28-2004
Dan Pop wrote:

> In <(E-Mail Removed) outprogramming.com> "macluvitch" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      04-29-2004
In <(E-Mail Removed)> Ben Pfaff <(E-Mail Removed)> writes:

>(E-Mail Removed) (Dan Pop) writes:
>
>> In <(E-Mail Removed) outprogramming.com> "macluvitch" <(E-Mail Removed)> 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: (E-Mail Removed)
 
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
How to use a passing argument(returned argument)? ؿ Ruby 7 11-27-2008 03:53 AM
passing command line argument to array virgincita schmidtmann C Programming 6 05-06-2007 10:34 PM
Passing a function as an argument and using the evaluated functionas an argument User1014 Javascript 1 11-30-2006 12:13 PM
help passing pointer to char array as argument rob.kirkpatrick@telefonica.net C++ 9 07-05-2006 02:50 PM
Argument-per-argument Passing Trans Ruby 3 01-26-2005 05:58 PM



Advertisments