user2308700
user2308700

Reputation: 67

Days of the week in an array in C

How do I do this? This is what I've tried so far and it keeps erroring saying naughty things at me :/

char DaysOfWeek[] = { 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' };

Upvotes: 0

Views: 8832

Answers (3)

dvai
dvai

Reputation: 2069

There are two problems:

  1. You need to use double quotes in C literal strings.
  2. This is a two dimensional array, you need to give some constant value to the second dimension.

Like this:

char DaysOfWeek[][20] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

Upvotes: 1

chouaib
chouaib

Reputation: 2817

try

char * DaysOfWeek[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

Upvotes: 4

Ricky Mutschlechner
Ricky Mutschlechner

Reputation: 4409

Your first problem is that you're defining an Array of Characters, which is just a single string. You'd want a 2D array of characters, ie. char**, char*[], or char[][] to hold multiple strings/words. Also, you need to use double quotes " " rather than single quotes ' ' when holding Strings in C.

The next step from here depends on your errors, I would say. I also don't think you can initialize a 2D array inline like that. You'd have to do something like char[][] days = { {'M', 'o', 'n', 'd', 'a', 'y'}, ... } I believe.

Upvotes: 1

Related Questions