Connecting Tech Pros Worldwide Forums | Help | Site Map

Multidimensional array problems

Jimmy Petersen
Guest
 
Posts: n/a
#1: Nov 14 '05
Hello all,

After reading through chapter 6 of the faq I must admit
I'm a bit confused :-).

What I am trying to do can be explained with the following
three sample files:

var.c:
float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};

var.h:
#ifndef var_h
#define var_h

extern float a**;

#endif

test.c
#include "var.h"
#include <stdio.h>

int main() {
printf("a[0][0] = %f\n", a[0][0]);
return(0);
}

This doesn't appear to be working though since the compiler (gcc)
reports "'a' undeclared". I know from the faq that arrays and
pointers are not the same but after having tried declaring 'a' as
'extern float* a[];' and 'extern float a[][];' I've found that
the only thing which compiles is 'extern float* a[];' but this
only gives me an array of pointers to unallocated memory.

How do I do the two-dimensional thing?

Regards, Jimmy

Joona I Palaste
Guest
 
Posts: n/a
#2: Nov 14 '05

re: Multidimensional array problems


Jimmy Petersen <jip@dtv.dk> scribbled the following:[color=blue]
> Hello all,[/color]
[color=blue]
> After reading through chapter 6 of the faq I must admit
> I'm a bit confused :-).[/color]
[color=blue]
> What I am trying to do can be explained with the following
> three sample files:[/color]
[color=blue]
> var.c:
> float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};[/color]
[color=blue]
> var.h:
> #ifndef var_h
> #define var_h[/color]
[color=blue]
> extern float a**;[/color]

a is declared as the wrong type here. Even though T[] and T* are
assignment-compatible types, T[][] and T** are not. Chris Torek can
explain this in detail. You should declare this as:
extern float (*a)[2];
or:
extern float a[2][2];
[color=blue]
> #endif[/color]
[color=blue]
> test.c
> #include "var.h"
> #include <stdio.h>[/color]
[color=blue]
> int main() {
> printf("a[0][0] = %f\n", a[0][0]);
> return(0);
> }[/color]
[color=blue]
> This doesn't appear to be working though since the compiler (gcc)
> reports "'a' undeclared". I know from the faq that arrays and
> pointers are not the same but after having tried declaring 'a' as
> 'extern float* a[];' and 'extern float a[][];' I've found that
> the only thing which compiles is 'extern float* a[];' but this
> only gives me an array of pointers to unallocated memory.[/color]
[color=blue]
> How do I do the two-dimensional thing?[/color]

Try declaring a as "extern float (*a)[];". The way you compiled it, it
was an array of pointers - you need a pointer to an array.

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
Joona I Palaste
Guest
 
Posts: n/a
#3: Nov 14 '05

re: Multidimensional array problems


Jimmy Petersen <jip@dtv.dk> scribbled the following:[color=blue]
> Hello all,[/color]
[color=blue]
> After reading through chapter 6 of the faq I must admit
> I'm a bit confused :-).[/color]
[color=blue]
> What I am trying to do can be explained with the following
> three sample files:[/color]
[color=blue]
> var.c:
> float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};[/color]
[color=blue]
> var.h:
> #ifndef var_h
> #define var_h[/color]
[color=blue]
> extern float a**;[/color]

a is declared as the wrong type here. Even though T[] and T* are
assignment-compatible types, T[][] and T** are not. Chris Torek can
explain this in detail. You should declare this as:
extern float (*a)[2];
or:
extern float a[2][2];
[color=blue]
> #endif[/color]
[color=blue]
> test.c
> #include "var.h"
> #include <stdio.h>[/color]
[color=blue]
> int main() {
> printf("a[0][0] = %f\n", a[0][0]);
> return(0);
> }[/color]
[color=blue]
> This doesn't appear to be working though since the compiler (gcc)
> reports "'a' undeclared". I know from the faq that arrays and
> pointers are not the same but after having tried declaring 'a' as
> 'extern float* a[];' and 'extern float a[][];' I've found that
> the only thing which compiles is 'extern float* a[];' but this
> only gives me an array of pointers to unallocated memory.[/color]
[color=blue]
> How do I do the two-dimensional thing?[/color]

Try declaring a as "extern float (*a)[];". The way you compiled it, it
was an array of pointers - you need a pointer to an array.

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
Al Bowers
Guest
 
Posts: n/a
#4: Nov 14 '05

re: Multidimensional array problems




Jimmy Petersen wrote:[color=blue]
> Hello all,
>
> After reading through chapter 6 of the faq I must admit
> I'm a bit confused :-).
>
> What I am trying to do can be explained with the following
> three sample files:
>
> var.c:
> float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};
>
> var.h:
> #ifndef var_h
> #define var_h
>
> extern float a**;
>
> #endif
>
> test.c
> #include "var.h"
> #include <stdio.h>
>
> int main() {
> printf("a[0][0] = %f\n", a[0][0]);
> return(0);
> }
>
> This doesn't appear to be working though since the compiler (gcc)
> reports "'a' undeclared". I know from the faq that arrays and
> pointers are not the same but after having tried declaring 'a' as
> 'extern float* a[];' and 'extern float a[][];' I've found that
> the only thing which compiles is 'extern float* a[];' but this
> only gives me an array of pointers to unallocated memory.
>
> How do I do the two-dimensional thing?
>[/color]

Change, in var.h
extern float **a;
to
extern float a[2][2];

--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Al Bowers
Guest
 
Posts: n/a
#5: Nov 14 '05

re: Multidimensional array problems




Jimmy Petersen wrote:[color=blue]
> Hello all,
>
> After reading through chapter 6 of the faq I must admit
> I'm a bit confused :-).
>
> What I am trying to do can be explained with the following
> three sample files:
>
> var.c:
> float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};
>
> var.h:
> #ifndef var_h
> #define var_h
>
> extern float a**;
>
> #endif
>
> test.c
> #include "var.h"
> #include <stdio.h>
>
> int main() {
> printf("a[0][0] = %f\n", a[0][0]);
> return(0);
> }
>
> This doesn't appear to be working though since the compiler (gcc)
> reports "'a' undeclared". I know from the faq that arrays and
> pointers are not the same but after having tried declaring 'a' as
> 'extern float* a[];' and 'extern float a[][];' I've found that
> the only thing which compiles is 'extern float* a[];' but this
> only gives me an array of pointers to unallocated memory.
>
> How do I do the two-dimensional thing?
>[/color]

Change, in var.h
extern float **a;
to
extern float a[2][2];

--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Jeremy Yallop
Guest
 
Posts: n/a
#6: Nov 14 '05

re: Multidimensional array problems


Joona I Palaste wrote:[color=blue]
> Jimmy Petersen <jip@dtv.dk> scribbled the following:[color=green]
>> var.c:
>> float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};[/color]
>[color=green]
>> var.h:
>> #ifndef var_h
>> #define var_h[/color]
>[color=green]
>> extern float a**;[/color][/color]

This is a syntax error, by the way. You mean:

extern float **a;

(which is still semantically wrong, though syntactically correct).
[color=blue]
> a is declared as the wrong type here. Even though T[] and T* are
> assignment-compatible types, T[][] and T** are not. Chris Torek can
> explain this in detail. You should declare this as:
> extern float (*a)[2];
> or:
> extern float a[2][2];[/color]

Only the second is a valid declaration for the array in the source
file. "Assignment-compatible" is irrelevant here. See section 6 of
the FAQ.
[color=blue][color=green]
>> I know from the faq that arrays and
>> pointers are not the same but after having tried declaring 'a' as
>> 'extern float* a[];' and 'extern float a[][];' I've found that
>> the only thing which compiles is 'extern float* a[];' but this
>> only gives me an array of pointers to unallocated memory.[/color]
>[color=green]
>> How do I do the two-dimensional thing?[/color][/color]

extern float a[2][2];
[color=blue]
> Try declaring a as "extern float (*a)[];". The way you compiled it, it
> was an array of pointers - you need a pointer to an array.[/color]

No, he needs an array of arrays.

Jeremy.
Jeremy Yallop
Guest
 
Posts: n/a
#7: Nov 14 '05

re: Multidimensional array problems


Joona I Palaste wrote:[color=blue]
> Jimmy Petersen <jip@dtv.dk> scribbled the following:[color=green]
>> var.c:
>> float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};[/color]
>[color=green]
>> var.h:
>> #ifndef var_h
>> #define var_h[/color]
>[color=green]
>> extern float a**;[/color][/color]

This is a syntax error, by the way. You mean:

extern float **a;

(which is still semantically wrong, though syntactically correct).
[color=blue]
> a is declared as the wrong type here. Even though T[] and T* are
> assignment-compatible types, T[][] and T** are not. Chris Torek can
> explain this in detail. You should declare this as:
> extern float (*a)[2];
> or:
> extern float a[2][2];[/color]

Only the second is a valid declaration for the array in the source
file. "Assignment-compatible" is irrelevant here. See section 6 of
the FAQ.
[color=blue][color=green]
>> I know from the faq that arrays and
>> pointers are not the same but after having tried declaring 'a' as
>> 'extern float* a[];' and 'extern float a[][];' I've found that
>> the only thing which compiles is 'extern float* a[];' but this
>> only gives me an array of pointers to unallocated memory.[/color]
>[color=green]
>> How do I do the two-dimensional thing?[/color][/color]

extern float a[2][2];
[color=blue]
> Try declaring a as "extern float (*a)[];". The way you compiled it, it
> was an array of pointers - you need a pointer to an array.[/color]

No, he needs an array of arrays.

Jeremy.
Emmanuel Delahaye
Guest
 
Posts: n/a
#8: Nov 14 '05

re: Multidimensional array problems


In 'comp.lang.c', "Jimmy Petersen" <jip@dtv.dk> wrote:
[color=blue]
> After reading through chapter 6 of the faq I must admit
> I'm a bit confused :-).
>
> What I am trying to do can be explained with the following
> three sample files:
>
> var.c:[/color]

For code sanity, add this:

#include "var.h"
[color=blue]
> float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};
>
> var.h:
> #ifndef var_h
> #define var_h
>
> extern float a**;[/color]

This is wrong. You want:

extern float a[2][2];
[color=blue]
> #endif
>
> test.c
> #include "var.h"
> #include <stdio.h>
>
> int main() {
> printf("a[0][0] = %f\n", a[0][0]);[/color]

Don't feel happy with a '[0][0]' test. Better to try with values that are not
0 :

printf("a[1][1] = %f\n", a[1][1]); /* expected : 4.0000 */

or better, to traverse the whole thing.
[color=blue]
> return(0);
> }[/color]

--
-ed- emdelYOURBRA@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Emmanuel Delahaye
Guest
 
Posts: n/a
#9: Nov 14 '05

re: Multidimensional array problems


In 'comp.lang.c', "Jimmy Petersen" <jip@dtv.dk> wrote:
[color=blue]
> After reading through chapter 6 of the faq I must admit
> I'm a bit confused :-).
>
> What I am trying to do can be explained with the following
> three sample files:
>
> var.c:[/color]

For code sanity, add this:

#include "var.h"
[color=blue]
> float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};
>
> var.h:
> #ifndef var_h
> #define var_h
>
> extern float a**;[/color]

This is wrong. You want:

extern float a[2][2];
[color=blue]
> #endif
>
> test.c
> #include "var.h"
> #include <stdio.h>
>
> int main() {
> printf("a[0][0] = %f\n", a[0][0]);[/color]

Don't feel happy with a '[0][0]' test. Better to try with values that are not
0 :

printf("a[1][1] = %f\n", a[1][1]); /* expected : 4.0000 */

or better, to traverse the whole thing.
[color=blue]
> return(0);
> }[/color]

--
-ed- emdelYOURBRA@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Closed Thread