passing an array as argument

M

monkeys paw

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?
 
R

Russell Hanneken

monkeys said:
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
 
B

Barry Schwarz

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

macluvitch

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,.......)
 
R

Richard Bos

macluvitch said:
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
 
D

Dan Pop

In said:
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
 
S

Stephen Sprunk

Dan Pop said:
In <e78e777a530ff915bf43f18fd1e815b0@localhost.talkaboutprogramming.com>

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
 
D

Dan Pop

In said:
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
 
M

macluvitch

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

yo folk what going on with you
I meant by return type the function return type :)


I think that pointers size in memory has sth with the current os file
system
 
K

Keith Thompson

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

I think "macluvitch" meant the phrase "return type" to stand in for
the return type of the function. It would have been clearer as:

RETURN_TYPE my_sort (char **ary, .......)

(When we see the word "return" in C code or pseudo-code, we naturally
assume that it's the C keyword.)
 

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
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top