Re: [C] functions and 2D arrays?
From: Leor Zolman (leor_at_bdsoft.com)
Date: 01/08/04
- Next message: Elliot Marks: "Re: [C] functions and 2D arrays?"
- Previous message: jeffc: "Re: [C++] Functions Opinion"
- In reply to: Elliot Marks: "Re: [C] functions and 2D arrays?"
- Next in thread: Elliot Marks: "Re: [C] functions and 2D arrays?"
- Reply: Elliot Marks: "Re: [C] functions and 2D arrays?"
- Reply: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Elliot Marks: "Re: [C] functions and 2D arrays?"
- Previous message: jeffc: "Re: [C++] Functions Opinion"
- In reply to: Elliot Marks: "Re: [C] functions and 2D arrays?"
- Next in thread: Elliot Marks: "Re: [C] functions and 2D arrays?"
- Reply: Elliot Marks: "Re: [C] functions and 2D arrays?"
- Reply: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|