Re: [C] functions and 2D arrays?

From: Leor Zolman (leor_at_bdsoft.com)
Date: 01/09/04

  • Next message: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"
    Date: Fri, 09 Jan 2004 01:56:42 GMT
    
    

    On Thu, 8 Jan 2004 19:16:21 -0500 (EST), "Arthur J. O'Dwyer" >to

    > Also, you've got one more layer of indirection there than you
    >really need. There's no reason to be passing around *pointers*
    >to arrays of arrays of int, when you could be passing pointers
    >to arrays of int.

    Absolutely. When reading the OP's question, my first thought was just
    that exactly; I had meant to say something about how using a "pointer
    to array" is rather out of the ordinary. Enough so that most compilers
    accept &array as an expression in lieu of just "array" (though they
    may emit warnings), and even vice-versa. For example, VC++ 7.1 accepts
    this:

    #include <stdio.h>

    void pfirst(int a[10])
    {
            printf("a[0] = %d\n", a[0]);
    }

    void pfirstp(int (*a)[10])
    {
            printf("(*a)[0] = %d\n", (*a)[0]);
    }

    int main()
    {
            int a[10] = {1,2,3};
            pfirst(a);
            pfirst(&a); /* warning */
            pfirstp(a); /* warning */
            pfirstp(&a);
            return 0;
    }

    Comeau, on the other hand, flags those both as fatal errors in one of
    its "strict" modes (which seems to be the default in my configuration)
    but just gives warnings in C90 or C99 mode. MSVC probably has similar
    options.

    Although passing a "pointer to array" is in some sense more
    straight-forward than taking advantage of the way array expressions
    "decay" into pointers, it is nevertheless the latter approach that
    feels more "natural" after you've been reading and writing C and C++
    for a while. Go figure.
            -leor

    Leor Zolman
    BD Software
    leor@bdsoft.com
    www.bdsoft.com
    C++ users: Download BD Software's free STL Error Message
    Decryptor at: www.bdsoft.com/tools/stlfilt.html


  • Next message: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"

    Relevant Pages

    • Re: [Lit.] Buffer overruns
      ... &x does not yield the same address as x when x is an array. ... "When two pointers are compared, the result depends on the relative ... tryit.c:6: warning: int format, long int arg ...
      (sci.crypt)
    • Re: performance of accessing array using indexes or pointers
      ... index or using pointers in C. ... int findex(int* array, int size) ...
      (comp.lang.asm.x86)
    • Re: Is the syntax for multi-dimensional arrays counter-intuitive?
      ... >> of whose elements is an array of int with a size of 3. ... There are no pointers here, and pointers don't have elements arrays do. ... If you output sizeof intArray you'll see it ...
      (comp.lang.c)
    • Re: array
      ... > It is an array of one TRGBQUAD. ... > (pointers to) variable size arrays. ... reading another post you've referenced similarly, on this same thread, I am ... Anyway back to this dynamic element of these two types of "hacks" ...
      (borland.public.delphi.language.objectpascal)
    • Re: Search through a (large) binary file.
      ... If you're only ever going to read the plain bytes, just read them directly from the FileStream you're working with. ... You can use the Position property to adjust from where you're reading in the file; save the current position, set the current position to 4 bytes earlier than the offset of the found string, read the 4 bytes of interest, then restore the current position to the previously saved value. ... If you have 4 bytes in an array, it would seem that you ought to just be using BitConverter to convert those directly to a 32-bit int, rather than doing all that stuff with the string. ...
      (microsoft.public.dotnet.languages.csharp)