Multidimensional array problems

  • Thread starter Joona I Palaste
  • Start date
J

Joona I Palaste

Jimmy Petersen said:
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**;

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];
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?

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 ([email protected]) ------------- 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
 
J

Jimmy Petersen

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
 
A

Al Bowers

Jimmy said:
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?

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

Jeremy Yallop

Joona said:
Jimmy Petersen said:
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**;

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

extern float **a;

(which is still semantically wrong, though syntactically correct).
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];

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.
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?

extern float a[2][2];
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.

No, he needs an array of arrays.

Jeremy.
 
E

Emmanuel Delahaye

Jimmy Petersen said:
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:

For code sanity, add this:

#include "var.h"
float a[2][2] = {{1.0f, 2.0f},{3.0f, 4.0f}};

var.h:
#ifndef var_h
#define var_h

extern float a**;

This is wrong. You want:

extern float a[2][2];
#endif

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

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top