Is this expression illegal?

B

Bo Yang

Hi,
I want to assign a two-demon array to a int ** pointer,
but gcc give an error!

int env[11][11] ;
int ** e = env ;

How to do this?
 
J

Jim Langston

Bo Yang said:
Hi,
I want to assign a two-demon array to a int ** pointer,
but gcc give an error!

int env[11][11] ;
int ** e = env ;

How to do this?

int env[11][11] is a two dimentional array of ints. The data is int values.

int** is a pointer to an int pointer. The data would contain pointers.

int* e = env;
is what you want. It is a pointer to an int and can be accessed as an
array.

e[10] is legal.

If your next question is, how do you figure out how to access env[5][5] from
e, the general formula is:
row * rowsize + column.
so it would be
e[5 * 11 + 5];
 
B

Bo Yang

Jim Langston :
Bo Yang said:
Hi,
I want to assign a two-demon array to a int ** pointer,
but gcc give an error!

int env[11][11] ;
int ** e = env ;

How to do this?

int env[11][11] is a two dimentional array of ints. The data is int values.

int** is a pointer to an int pointer. The data would contain pointers.

int* e = env;
is what you want. It is a pointer to an int and can be accessed as an
array.

e[10] is legal.

If your next question is, how do you figure out how to access env[5][5] from
e, the general formula is:
row * rowsize + column.
so it would be
e[5 * 11 + 5];
But I will get another error if I do
e[1][1]

how to avoid this? I don't want to write
e[1*11+1] every time!

Thank you!
 
D

dasjotre

Bo said:
But I will get another error if I do
e[1][1]

how to avoid this? I don't want to write
e[1*11+1] every time!

you could write a function
size_t get_index(size_t i, size_t j)
{
return i*11+j;
}

and use like this

e[get_index(1, 1)] = 0;

or get rid of the pointer and use a reference to a 2d array

typedef int row[11];
typedef row array2d[11];

array2d e;
array2d & eref = e;
eref[1][1] = 0;

since you're using C++, why not use vector instead

#include <vector>

typedef std::vector<int> row;
typedef std::vector<row> array2d;

array2d my_array(11, row(11, 0));
my_array[1][1] = 0;
array2d & my_array_ref = my_array;
my_array_ref[1][1] = 0;
 
B

Bo Yang

dasjotre :
Bo said:
But I will get another error if I do
e[1][1]

how to avoid this? I don't want to write
e[1*11+1] every time!

you could write a function
size_t get_index(size_t i, size_t j)
{
return i*11+j;
}

and use like this

e[get_index(1, 1)] = 0;

or get rid of the pointer and use a reference to a 2d array

typedef int row[11];
typedef row array2d[11];

array2d e;
array2d & eref = e;
eref[1][1] = 0;

since you're using C++, why not use vector instead

#include <vector>

typedef std::vector<int> row;
typedef std::vector<row> array2d;

array2d my_array(11, row(11, 0));
my_array[1][1] = 0;
array2d & my_array_ref = my_array;
my_array_ref[1][1] = 0;
Thank you, I know it.
The reason I don't use vectors is I want to these code can be
used in c too!
 
M

Mike Wahler

Pete Becker said:
Bo said:
Hi,
I want to assign a two-demon array to a int ** pointer,
but gcc give an error!

int env[11][11] ;
int ** e = env ;

How to do this?

int *e[11] = env;

Shouldn't that be:

int (*e)[11] = env;

?

-Mike
 
N

Noah Roberts

Mike said:
Pete Becker said:
Bo said:
Hi,
I want to assign a two-demon array to a int ** pointer,
but gcc give an error!

int env[11][11] ;
int ** e = env ;

How to do this?

int *e[11] = env;

Shouldn't that be:

int (*e)[11] = env;

Yes, that is the correct one I believe. Pete's version creates an 11
count array of int pointers...at least in C. I'd be surprised if that
assignment actually works but I've been surprised before.
 
L

Lionel B

dasjotre :
Bo Yang wrote:

[snip]

since you're using C++, why not use vector instead

#include <vector>

typedef std::vector<int> row;
typedef std::vector<row> array2d;

[snip]

Thank you, I know it.
The reason I don't use vectors is I want to these code can be
used in c too!

In that case I'd suggest you write (and compile) it in C - and seek
assistance in a C language newsgroup. It should then be reasonably easy to
adapt a version that will compile and run correctly in C++, since C++ is
(more or less) a superset of C [*]. I suspect that'll be far easier than
trying to write "C-friendly" C++ code.

[*] I know, I know...
 
F

Fred Zwarts

Jim Langston said:
Bo Yang said:
Hi,
I want to assign a two-demon array to a int ** pointer,
but gcc give an error!

int env[11][11] ;
int ** e = env ;

How to do this?

int env[11][11] is a two dimentional array of ints. The data is int values.

int** is a pointer to an int pointer. The data would contain pointers.

int* e = env;
is what you want. It is a pointer to an int and can be accessed as an
array.

My compiler says:
%CXX-E-BADINITTYP, a value of "int (*)[11] cannot be used to initialize an entity of type "int *"

So, this is not going to work either.

int (*e)[11] = env;

is what you want.
 

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,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top