Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 7th, 2006, 06:55 AM
Bo Yang
Guest
 
Posts: n/a
Default 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?
  #2  
Old December 7th, 2006, 07:45 AM
Jim Langston
Guest
 
Posts: n/a
Default 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];


  #3  
Old December 7th, 2006, 12:05 PM
Bo Yang
Guest
 
Posts: n/a
Default 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!
  #4  
Old December 7th, 2006, 01:05 PM
dasjotre
Guest
 
Posts: n/a
Default 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;

  #5  
Old December 7th, 2006, 01:05 PM
Pete Becker
Guest
 
Posts: n/a
Default 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)
  #6  
Old December 7th, 2006, 01:35 PM
Geo
Guest
 
Posts: n/a
Default 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!
  #7  
Old December 7th, 2006, 02:45 PM
Bo Yang
Guest
 
Posts: n/a
Default 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!
  #8  
Old December 7th, 2006, 02:45 PM
Mike Wahler
Guest
 
Posts: n/a
Default 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


  #9  
Old December 7th, 2006, 03:25 PM
Noah Roberts
Guest
 
Posts: n/a
Default 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.

  #10  
Old December 7th, 2006, 03:45 PM
Pete Becker
Guest
 
Posts: n/a
Default 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)
  #11  
Old December 7th, 2006, 03:55 PM
Lionel B
Guest
 
Posts: n/a
Default 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
  #12  
Old December 8th, 2006, 08:25 AM
Fred Zwarts
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles