how to declare pseudo-matrix function argument that is not modified by function?

Discussion in 'C Programming' started by classicist, Oct 5, 2005.

  1. classicist

    classicist Guest

    Suppose I have a function which takes an argv-style pseudo-matrix
    and modifies neither the strings nor the pointers. How do I
    use the const qualifier to signal my intentions w/o drawing compiler
    warnings? E.g.,

    int
    sum_of_lengths_of_strings( const char **s, int ns )
    {
    int i, len = 0;
    for ( i = 0; i < ns; ++i )
    len += strlen(s);
    return len;
    }

    int
    main(int argc, char **argv )
    {
    printf( "command args len = %d\n",
    sum_of_lengths_of_strings( argv, argc );
    return 0;
    }

    This code draws a warning about incompatable types. Presumably
    the compiler does this to guard against this situation:

    void
    get_a_const_string( const char **s )
    {
    *s = "better_not_modify_me";
    }

    However, even if we change the declaration of
    sum_of_lengths_of_strings() to

    int
    sum_of_lengths_of_strings( const *const *s, int ns )

    we still get the warning (at least w/ gcc).
    Any ideas how to declare my intensions with const and not draw a
    warning from the compiler?
     
    classicist, Oct 5, 2005
    #1
    1. Advertising

  2. classicist <> schrieb:

    > int
    > main(int argc, char **argv )
    > {
    > printf( "command args len = %d\n",
    > sum_of_lengths_of_strings( argv, argc );


    There's a ')' missing before the ';'

    > This code draws a warning about incompatable types. Presumably


    I bet it does not.
    It's always better to post an actual error message and not something
    you vaguely remember ;-)

    Markus
     
    Markus Becker, Oct 5, 2005
    #2
    1. Advertising

  3. classicist

    Guest

    classicist wrote:
    ... deleted correct analysis of nontrivial problem of declaring const
    char *const * parameter when argument may need to be char ** ...

    > Any ideas how to declare my intensions with const and not draw a
    > warning from the compiler?


    No, this is a nontrivial problem in C, fixed in C++. I usually ignore
    it by using casts and a comment. In principle, you can define a safe
    cast using a macro (I cannot obfuscate like some others, but can aspire
    to greatness!)

    static int dummy(); /* old style declaration and no definition! */
    #define safecast(t,v) /* t const * must be a pointer to const t */ \
    ((void)sizeof( ((int(*)(t,const void*))dummy)(*(v),(v)) ),(t const
    *const)(v))

    And then safecast(const char*,argv) and get the equivalent of (const
    char *const *const) argv, but get an incomprehensible errot if argv is
    not either char **, const char **, const char *const * or char *const *
    (or their const versions).

    Such `const casts' are possible in C, but I have never used them in
    practice.
     
    , Oct 5, 2005
    #3
  4. classicist

    Guest

    wrote:

    an obfuscated macro that had an unfortunate line wrap ...

    > static int dummy(); /* old style declaration and no definition! */


    Compilers often seem to warn about static functions which are not
    defined. Changing it to an undefined extern function solves the
    problem, at the expense of increasing the possibility of name
    conflict. Else one can provide a dummy definition like {return 0;},
    but a definition might start prompting compilers to provide gratuitous
    warnings about an invalid call. Of course, a gratuitous warning about
    unprototyped function is always possible.

    Not using dummy, but using a null function pointer works on all
    compilers I know of, and it certainly does not violate any constraint,
    but I have not carefully checked whether that invokes undefined
    behaviour (I vaguely remember having checked it is okay in C99, but it
    has been too long...)
     
    , Oct 5, 2005
    #4
    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. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,675
    Jonathan Bromley
    Jul 5, 2006
  2. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    409
    Holgerson
    Oct 26, 2007
  3. Reckoner
    Replies:
    11
    Views:
    704
    Steven D'Aprano
    Jan 19, 2009
  4. Pushkar Prasad

    How to declare a function argument as optional?

    Pushkar Prasad, Feb 7, 2011, in forum: C Programming
    Replies:
    16
    Views:
    1,248
    Joel C. Salomon
    Feb 7, 2011
  5. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
Loading...

Share This Page