Re: [C] functions and 2D arrays?

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


Date: Thu, 08 Jan 2004 22:30:29 GMT

On Thu, 08 Jan 2004 22:07:03 GMT, Elliot Marks <emarks@email.net>
wrote:

>Here is my C version of your code:
>
>#include <stdio.h>
>typedef int(*a2dptr)[3][3]; /* ptr to 2-dim array */
>a2dptr func(a2dptr a2dp);
>
>int main(void)
>{
> int data[3][3] = {10,20,30,40,50,60,70,80,90};
>
> a2dptr ap;
>
> ap = func(&data);
>
> printf("%d\n", (*ap)[0][0]); /* should print 11 */
> return 0;
>}
>
>a2dptr func(a2dptr a2dp)
>{
> for (int i = 0; i < 3; i++)
> for (int j = 0; j < 3; j++)
> (*a2dp)[i][j]++;
>
> return a2dp;
>}
>
>This works, and I like the idea of using typedef, but to solidify
>my understanding, can you show me how to do it without typedef? I
>tried substituting <int(*a2dptr)[3][3]> in the prototype and
>declaration but it wouldn't compile.

Grrr. You _would_ ask me that ;-)
Amazingly, I actually figured it out (this is the first time; nothing
like being challenged!). Here it is, really in _C_ this time:

#include <stdio.h>

/* typedef int(*a2dptr)[3][3]; // ptr to 2-dim array */

int (*func(int (*a2dp)[3][3]))[3][3]
{
        int i,j;
        for (i = 0; i < 3; i++)
                for (j = 0; j < 3; j++)
                        (*a2dp)[i][j]++;

        return a2dp;
}

int main()
{
        int data[3][3] = {10,20,30,40,50,60,70,80,90};

        int (*ap)[3][3];

        ap = func(&data);

        printf("%d\n", (*ap)[0][0]); // should print 11
        return 0;
}

-leor

>
>Elliot
>
>

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



Relevant Pages

  • Bypassing Personal Firewalls
    ... typedef SOCKET (int, int, int); ... typedef int (__stdcall *func_connect)(SOCKET, const struct sockaddr ... typedef HANDLE (LPCTSTR, DWORD, DWORD, ...
    (Bugtraq)
  • Bypassing Personal Firewalls
    ... typedef SOCKET (int, int, int); ... typedef int (__stdcall *func_connect)(SOCKET, const struct sockaddr ... typedef HANDLE (LPCTSTR, DWORD, DWORD, ...
    (Vuln-Dev)
  • Re: formal parameter 1 different from declaration?
    ... Here is the typedef one: ... typedef LR (HWND, long, int, int); ... struct tagWnd; // forward declaration ... takes a struct tagHwnd and the other takes a long. ...
    (microsoft.public.vc.language)
  • Re: "Portability" contructs like UINT32 etc.
    ... > typedef char CHAR; ... > typedef int INT32; ... > typedef char* PCHAR; ... Both processors had hardware alignment requirements. ...
    (comp.lang.c)
  • Re: SymGetTypeInfo and TI_GET_SYMNAME
    ... typedef int MyType; ... typedef MyStruct MyStruct2; ... Now i succed to have information about struct and typedef. ...
    (microsoft.public.vsnet.debugging)