Connecting Tech Pros Worldwide Forums | Help | Site Map

Is this expression illegal?

Bo Yang
Guest
 
Posts: n/a
#1: Dec 7 '06
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?

Jim Langston
Guest
 
Posts: n/a
#2: Dec 7 '06

re: Is this expression illegal?


"Bo Yang" <struggle@mail.nankai.edu.cnwrote in message
news:el8he2$t96$1@news.cn99.com...
Quote:
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];


Bo Yang
Guest
 
Posts: n/a
#3: Dec 7 '06

re: Is this expression illegal?


Jim Langston :
Quote:
"Bo Yang" <struggle@mail.nankai.edu.cnwrote in message
news:el8he2$t96$1@news.cn99.com...
Quote:
>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!
dasjotre
Guest
 
Posts: n/a
#4: Dec 7 '06

re: Is this expression illegal?


Bo Yang wrote:
Quote:
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<introw;
typedef std::vector<rowarray2d;

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

Pete Becker
Guest
 
Posts: n/a
#5: Dec 7 '06

re: Is this expression illegal?


Bo Yang wrote:
Quote:
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;

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Geo
Guest
 
Posts: n/a
#6: Dec 7 '06

re: Is this expression illegal?



Bo Yang wrote:
Quote:
how to avoid this? I don't want to write
e[1*11+1] every time!
>
Just as well, because if I'm not mistaken, this is undefined behaviour
!
Quote:
Thank you!
Bo Yang
Guest
 
Posts: n/a
#7: Dec 7 '06

re: Is this expression illegal?


dasjotre :
Quote:
Bo Yang wrote:
Quote:
>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<introw;
typedef std::vector<rowarray2d;
>
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!
Mike Wahler
Guest
 
Posts: n/a
#8: Dec 7 '06

re: Is this expression illegal?



"Pete Becker" <pete@versatilecoding.comwrote in message
news:o-6dnXiT8NKPiuXYnZ2dnUVZ_sednZ2d@giganews.com...
Quote:
Bo Yang wrote:
Quote:
>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


Noah Roberts
Guest
 
Posts: n/a
#9: Dec 7 '06

re: Is this expression illegal?



Mike Wahler wrote:
Quote:
"Pete Becker" <pete@versatilecoding.comwrote in message
news:o-6dnXiT8NKPiuXYnZ2dnUVZ_sednZ2d@giganews.com...
Quote:
Bo Yang wrote:
Quote:
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.

Pete Becker
Guest
 
Posts: n/a
#10: Dec 7 '06

re: Is this expression illegal?


Mike Wahler wrote:
Quote:
"Pete Becker" <pete@versatilecoding.comwrote in message
news:o-6dnXiT8NKPiuXYnZ2dnUVZ_sednZ2d@giganews.com...
Quote:
>Bo Yang wrote:
Quote:
>>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;
>
Yup.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Lionel B
Guest
 
Posts: n/a
#11: Dec 7 '06

re: Is this expression illegal?


On Thu, 07 Dec 2006 23:04:06 +0800, Bo Yang wrote:
Quote:
dasjotre :
Quote:
>Bo Yang wrote:
>>
>[snip]
>>
>since you're using C++, why not use vector instead
>>
>#include <vector>
>>
>typedef std::vector<introw;
>typedef std::vector<rowarray2d;
>>
>[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...

--
Lionel B
Fred Zwarts
Guest
 
Posts: n/a
#12: Dec 8 '06

re: Is this expression illegal?


"Jim Langston" <tazmaster@rocketmail.comwrote in message news:0XPdh.84$IK1.50@newsfe02.lga...
Quote:
"Bo Yang" <struggle@mail.nankai.edu.cnwrote in message
news:el8he2$t96$1@news.cn99.com...
Quote:
>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.

--
Fred.Zwarts.
Closed Thread


Similar C / C++ bytes