×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

What's wrong with this code: array assignment problem

What's wrong with this code: array assignment problem

What's wrong with this code: array assignment problem

(OP)
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

RE: What's wrong with this code: array assignment problem

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.

RE: What's wrong with this code: array assignment problem

(OP)
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.

RE: What's wrong with this code: array assignment problem

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.

RE: What's wrong with this code: array assignment problem

(OP)
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?

RE: What's wrong with this code: array assignment problem

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.

RE: What's wrong with this code: array assignment problem

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close