473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Multidimensional array problems

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
Nov 14 '05 #1
8 2475
Jimmy Petersen <ji*@dtv.dk> scribbled the following:
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];
#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?


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 (pa*****@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
Nov 14 '05 #2
Jimmy Petersen <ji*@dtv.dk> scribbled the following:
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];
#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?


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 (pa*****@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
Nov 14 '05 #3


Jimmy Petersen wrote:
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];

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

Nov 14 '05 #4


Jimmy Petersen wrote:
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];

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

Nov 14 '05 #5
Joona I Palaste wrote:
Jimmy Petersen <ji*@dtv.dk> scribbled the following:
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.
Nov 14 '05 #6
Joona I Palaste wrote:
Jimmy Petersen <ji*@dtv.dk> scribbled the following:
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.
Nov 14 '05 #7
In 'comp.lang.c', "Jimmy Petersen" <ji*@dtv.dk> wrote:
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.
return(0);
}


--
-ed- em**********@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/
Nov 14 '05 #8
In 'comp.lang.c', "Jimmy Petersen" <ji*@dtv.dk> wrote:
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.
return(0);
}


--
-ed- em**********@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/
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rob Tweed | last post by:
Probably a simple question but I can't find the answer anyway. Specifically, is it possible to copy a multidimensional array into the $_SESSION array - ie a deep clone of all keys and data? I...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
2
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
1
by: Chuy08 | last post by:
If I have a multidimensional array like the following: Array $records =Array 0 = 30 year, 6.0; 1 = 30 year, 6.0; 2 = Pay Option, 1.0; 3 = Pay Option, 1.0; How could I flatten this to...
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
4
Jezternz
by: Jezternz | last post by:
First of all I am open to any suggestions and advice. If a javscript multidimensional array is a bad way to do this please say so. I considered XML but I wondered if this would be a bad idea as it...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.