passing an array as argument

Discussion in 'C Programming' started by monkeys paw, Apr 27, 2004.

  1. monkeys paw

    monkeys paw Guest

    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?
     
    monkeys paw, Apr 27, 2004
    #1
    1. Advertising

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

    Remove the 'g' from my address to send me mail.
     
    Russell Hanneken, Apr 28, 2004
    #2
    1. Advertising

  3. On Tue, 27 Apr 2004 22:44:05 GMT, 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?


    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>>
     
    Barry Schwarz, Apr 28, 2004
    #3
  4. monkeys paw

    macluvitch Guest

    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,.......)
     
    macluvitch, Apr 28, 2004
    #4
  5. monkeys paw

    Richard Bos Guest

    "macluvitch" <> 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
     
    Richard Bos, Apr 28, 2004
    #5
  6. monkeys paw

    Dan Pop Guest

    In <> "macluvitch" <> 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, Apr 28, 2004
    #6
  7. monkeys paw

    Ben Pfaff Guest

    (Dan Pop) writes:

    > In <> "macluvitch" <> 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.
     
    Ben Pfaff, Apr 28, 2004
    #7
  8. "Dan Pop" <> wrote in message
    news:c6o6vj$mrn$...
    > In <>

    "macluvitch" <> 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
     
    Stephen Sprunk, Apr 28, 2004
    #8
  9. Dan Pop wrote:

    > In <> "macluvitch" <> 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.
     
    Martin Ambuhl, Apr 29, 2004
    #9
  10. monkeys paw

    Dan Pop Guest

    In <> Ben Pfaff <> writes:

    > (Dan Pop) writes:
    >
    >> In <> "macluvitch" <> 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, Apr 29, 2004
    #10
  11. monkeys paw

    Dan Pop Guest

    In <> "Stephen Sprunk" <> writes:

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


    To the same extent that an IP16L32 system could be considered as 32-bit.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Apr 29, 2004
    #11
  12. monkeys paw

    Guillaume Guest

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


    Clever.
     
    Guillaume, Apr 29, 2004
    #12
  13. monkeys paw

    macluvitch Guest

    >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
     
    macluvitch, Apr 30, 2004
    #13
  14. (Richard Bos) writes:
    > "macluvitch" <> wrote:

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


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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Apr 30, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    431
  2. virgincita schmidtmann

    passing command line argument to array

    virgincita schmidtmann, May 4, 2007, in forum: C Programming
    Replies:
    6
    Views:
    461
    Old Wolf
    May 6, 2007
  3. Trans
    Replies:
    3
    Views:
    198
    Robert Klemme
    Jan 26, 2005
  4. ±è ÁØ¿µ
    Replies:
    7
    Views:
    257
    Einar Magnús Boson
    Nov 27, 2008
  5. User1014
    Replies:
    1
    Views:
    191
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page