Is this expression illegal? 
December 7th, 2006, 05:55 AM
| | | Is this expression illegal?
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? | 
December 7th, 2006, 06:45 AM
| | | 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]; | 
December 7th, 2006, 11:05 AM
| | | 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! | 
December 7th, 2006, 12:05 PM
| | | 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; | 
December 7th, 2006, 12:05 PM
| | | 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) | 
December 7th, 2006, 12:35 PM
| | | 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
! | 
December 7th, 2006, 01:45 PM
| | | 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! | 
December 7th, 2006, 01:45 PM
| | | 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 | 
December 7th, 2006, 02:25 PM
| | | 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. | 
December 7th, 2006, 02:45 PM
| | | 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) | 
December 7th, 2006, 02:55 PM
| | | 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 | 
December 8th, 2006, 07:25 AM
| | | 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. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|