473,395 Members | 1,689 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,395 software developers and data experts.

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?
Dec 7 '06 #1
11 1382
"Bo Yang" <st******@mail.nankai.edu.cnwrote in message
news:el**********@news.cn99.com...
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];
Dec 7 '06 #2
Jim Langston :
"Bo Yang" <st******@mail.nankai.edu.cnwrote in message
news:el**********@news.cn99.com...
>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!
Dec 7 '06 #3
Bo Yang wrote:
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)
Dec 7 '06 #4
Bo Yang wrote:
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;

Dec 7 '06 #5
Geo

Bo Yang wrote:
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
!
Thank you!
Dec 7 '06 #6
dasjotre :
Bo Yang wrote:
>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!
Dec 7 '06 #7

"Pete Becker" <pe**@versatilecoding.comwrote in message
news:o-******************************@giganews.com...
Bo Yang wrote:
>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
Dec 7 '06 #8

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

Dec 7 '06 #9
Mike Wahler wrote:
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:o-******************************@giganews.com...
>Bo Yang wrote:
>>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)
Dec 7 '06 #10
On Thu, 07 Dec 2006 23:04:06 +0800, Bo Yang wrote:
dasjotre :
>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
Dec 7 '06 #11
"Jim Langston" <ta*******@rocketmail.comwrote in message news:0X*************@newsfe02.lga...
"Bo Yang" <st******@mail.nankai.edu.cnwrote in message
news:el**********@news.cn99.com...
>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.
Dec 8 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rex_chaos | last post by:
Hi all, As some book tells, I try the following example of expression template. template < typename LeftOpd, typename Op, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
12
by: Chris Yates | last post by:
Why is this illegal: --(i++) // Error: -- needs an l-value and this is not: (--i)++ ??
2
by: JJA | last post by:
Please advise on how to get the GROUP BY coded in an acceptable way: DECLARE @LO INT DECLARE @HI INT DECLARE @StartDate varchar(10) DECLARE @EndDate varchar(10) SELECT @StartDate =...
14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
3
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't...
14
by: John Temples | last post by:
Given this code: extern volatile unsigned char v; int main(void) { v; return 0; }
5
by: JIM.H. | last post by:
Hello, I have this validation expression: ^(?:(?:0?|1)|(?:0?|11)(?!\/31)|(?:0?2)(?:(?!\/3|\/29\/(?:(?:0||)00|(?:\d{2}(?:0||))))))\/(?:0?||3)\/\d{4}$ This is supposed to match MM/DD/YYYY it is...
5
by: elie.constantine | last post by:
Hello A person developed a Microsoft Access Solution and gave me the MDE file. When I tried to run it, I got "Function is not available in expressions in query expression 'Trim([......" Error. ...
4
by: rajesh619 | last post by:
I'm new to programming. I have created a servlet which retrieves values from the database after a value is put into the HTML page to which it is attached. But during compilation, it shows two errors....
3
by: somenath | last post by:
Hi All, I got some questions regarding the type of expression . For example 1)char *const *(*ptr)(); Here the type of ptr is it is constant pointer to a function which accept unspecified...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.