array in prototype

Discussion in 'C Programming' started by cerr, Jun 12, 2014.

  1. cerr

    cerr Guest

    Hi,

    How legal is it to have a function like:

    int switch_wan_set_ipv6addr(uint32 routeID,char ipv6addr[L3ADDR_LEN]) {
    int i;

    if (routeID > MAX_ROUTE_ENTRIES) {
    printf("RouteId %d is out of range! [0-%d]", routeID, MAX_ROUTE_ENTRIES);
    return ERROR;
    }
    for (i=0; i<L3ADDR_LEN; i++)
    route_entry[routeID].ipv6addr = ipv6addr;
    return OK;
    }

    L3ADDR_LEN being 16 and route_entry.ipv6addr being of type unsigned char bcm_ip6_t[16]

    I'd expect the function the be called like

    switch_wan_set_ipv6addr(0,"0123456789012345");

    is this legal and perfectrly fine? Assume that the caller can't pass pointers as arguments.

    Thanks,
    Ron
     
    cerr, Jun 12, 2014
    #1
    1. Advertisements

  2. cerr

    James Kuyper Guest



    That being the case, I'd recommend declaring the second parameter of
    switch_wan_set_ipv6addr as "unsigned char", not "char".
    It's not possible to declare a C function as taking a parameter of array
    type. That's because, in the parameter list of a function declaration, a
    declaration of a parameter as an "array of T" gets automatically and
    unavoidably converted into a "pointer to T". If the caller (presumably
    NOT written in C?) cannot pass pointers, it can't call this function.

    The closest you can get to doing what you want to do is to declare the
    function as taking a parameter which has a struct type that contains an
    array. Whether that is any help at all depends upon what language the
    calling routine is written in.
     
    James Kuyper, Jun 12, 2014
    #2
    1. Advertisements

  3. cerr

    cerr Guest




    Hm, yeah, that is what I was worried about....
    The caller is actually going to be the VzWorks shell... so do I need to expect 16 separate variables in the prototype?
     
    cerr, Jun 12, 2014
    #3


  4. Perfectly legal, but it doesn't mean what you probably thnk it means.

    The parameter ipv6addr is of type char*. The value of L3ADDR_LEN is
    quietly ignored in the parameter declaration.
    Your final assumption is incorrect. In the call you've shown, the
    second argument *is* a pointer value.

    Recommended reading: Section 6 of the comp.lang.c FAQ,
    <http://www.c-faq.com/>.

    Incidentally, the "%d" format requires an argument of type int. Since
    routeID is of type uint32 (why not uint32_t, which is defined in the
    standard header <stdint.h>?), you need to change the format string
    and/or cast the argument to the expected type. This may also apply to
    MAX_ROUTE_ENTRIES, depending on how it's defined.
     
    Keith Thompson, Jun 12, 2014
    #4
  5. That depends on the capabilities of the VzWorks shell, which most of us
    here are unlikely to know about.
     
    Keith Thompson, Jun 12, 2014
    #5
  6. Quibble: The standard uses the word "adjusted" for the transformation of
    an array parameter to a pointer parameter, emphasizing that it takes
    place at compile time, not during execution (as most conversions do).
     
    Keith Thompson, Jun 12, 2014
    #6
  7. cerr

    Guest

     
    , Jun 12, 2014
    #7
  8. cerr

    Jorgen Grahn Guest

    BTW, I suppose he means the VxWorks shell ...

    /Jorgen
     
    Jorgen Grahn, Jun 13, 2014
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.