473,385 Members | 1,409 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,385 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 2474
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.