Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with this code: array assignment problem

Status
Not open for further replies.

Mingster

Programmer
Dec 6, 2000
5
US
Hi recently I try to do something like the following:
char buf[6] = { 'a','b','c','d','e','f'};
char combination[2] = { buf[3], buf[2] };
and the C compiler gives me error "illegal constant expression"

Does anyone know why the code is wrong?

What I am trying to do is create an array of elements in
display order (which one gets drawn first).
And another array of the same elements that are sorted by
access priority. If the above code is illegal, what's my alternative?

sincerely,

Ming
 
int main(){

char buf[6] = { 'a','b','c','d','e','f'};
char combination[2] = { buf[3], buf[2] };

}

gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000724 (experimental)

Gave no errrors for me, what compiler, OS and stuff are you using. Post the OS and compiler type/version and the full source code. As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
I am using Metrowerks C compiler,
under Windows NT 4.0.

I have downloaded the latest version of DJGPP, and it gave me similar error as Metrowerk's: initializaer element is not constant.

 
Hmmmm, DJGPP is supposed to be a port of gcc...

// out the second line and see which line the error is coming from, the first or the second. post that and I'll see it tonite. When I get home, I've got DJGPP on NT and I'll play with it.

MWB. As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Okay,
I tried again,
Before, I put that code
char buf ....
in the header file, and that's where errors are,
once I move them into C file, the errors disappers.

Why is that?

I am trying to generate an array of display items,
and then specify the traversal sequence in another
array... I want to put them in a header file because
I want to have other people to call these static display
items by simply include the header file. Am I doing this
wrong, and that data shouldn't have gone into the header
file?

 
Where did you put it in the header file?

If you put it in a class delcaration, you can't do an assignment, that's what constructors are for. So, if you had

class thisClass{
char buf[4]={'a'....

You'd get an error. Post the header file the way it was when you got the error and we'll take a look.

MWB As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
I am programming using C and not C++, so I didn't put it within a class declaration.

I figured out that in global space I cannot execute code,
I can only assign values. Apparently, [] constitutes execution.
So simply by not using the [], and uses the pointer
int x[] = {1, 2, 3};
int *y[] = {x+2, x, x+1};

I have evaded the problem.

Thanks for your help, this is such subtle details about a language, I never thought I have to pay such close attention to the language specification...

Ray
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top