Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

initializing multidimentional arrays

Status
Not open for further replies.

ciara

Marine/Ocean
May 9, 2003
1
Could somebody please help me, it’s all just getting a little to frustrating!!

I was initially working with a long .c file. Within this file I had declared a global char variable of month, with the following syntax…

char month[13][4] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

It worked just fine, declared initialised etc everything knows where to go.

I then fiddled the program to make it modular. Therefore month is declared in the .h file.

When I subsequently try to initialise the array in the main method I just get a syntax error.

I’m trying to initialise it by writing….

month[1][ ] = “Jan”;
month[2][ ] = “Feb”;
etc

Is this completely wrong??
 
Replies continue below

Recommended for you

Yes, you can not do it like that. You have to use a string related function like a stringcopy or sprintf. Using sprintf like this.

sprintf(month[1],"%s","Jan");
 
Instead of declaring the character layout, declare month to be an array of pointers:

char *month[12];

Then in the routine where you want to initialize the values,

month[0] = "Jan";

or equivalent programming. You can assign the pointer; you cannot assign a character array.
 
Status
Not open for further replies.

Similar threads

?
Replies
2
Views
155
?

Part and Inventory Search

Sponsor