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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

initialising multidimensional char arrays 1

Status
Not open for further replies.

Ciara

Technical User
Dec 12, 2000
6
GB
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??
 
Yes what you are tring to do is not allowed. In the variabl Initialization only you can do like that. What your are doing is an assignment. Here you have to use the string copy [ strcpy ] functions.

declare the two dimensional with in your character array like char month[13][4]; [If you want you can put the total initialization here itself].

within the main() put like the following

strcpy(month[1], "Jan");
strcpy(month[2], "Feb");
...

Regards
Maniraja S


 
The other thing u can do is :
month[1]="Jan";
month[2]="Feb";
...
..

Regards,
SwapSawe.s-)
 
Yes, month[1] = "Jan" will also solve the problem but only if the month declared as an array of ponter variables otherwise it will throw errors.

Regards
Maniraja S
 
I can't understand. Do you really need strings?
You can use:
enum months
{
ian=0,feb=1,...
};
All operations with strings are more slowly than with numbers/constants. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top