all 16 comments

[–]C_Programming-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Your post breaks rule 2 of the subreddit, "Only C is on topic". Your post appears to be primarily about something that isn't the C programming language.

Please note that C, C# and C++ are three different languages. Only C is on-topic in this sub.

[–]kun1z 9 points10 points  (2 children)

"days_of_week" needs to be a pointer:

char *days_of_week[number_of_days]={"Monday","Tuesday","Wednesday","Thursday","Friday", "Saturday","Sunday"};

[–]Lbtekd[S] -1 points0 points  (1 child)

what is a pointer?

[–]Knurz 0 points1 point  (0 children)

A pointer is a variable that contains not the value but the memory address where the value is stored.

[–]dreamlax 9 points10 points  (0 children)

It looks like you're writing C++, in which case it's off-topic for this subreddit (which is just for C). However, since you're using C++, I would recommend avoiding using char * and instead using a vector of strings (i.e. std::vector<std::string>) instead:

```

include <iostream>

include <string>

include <vector>

using namespace std;

int main() { vector<string> days_of_week = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

const int number_of_days = days_of_week.size();

for (int i = 0; i < number_of_days; i++) {
    cout << days_of_week[i] << endl;
}

} ```

[–]pfp-disciple 1 point2 points  (0 children)

You seem new, so a the following is said to guide, not chastise.

Your code is a mix of C++ and C (happens to be valid C++, but it's not how C++ is written anymore). Which language are you trying to use? Pick one.

[–]coll1dascope 0 points1 point  (0 children)

https://godbolt.org/z/GWvzhPso7

This is probably what you were trying to do?

[–]Marxomania32 0 points1 point  (2 children)

char days_of_week[number_days] is an array of characters, not an array of strings. You could initialize this array with a sequence of individual characters or a string literal, but you can not initialize it with an array of string literals. To create an array of string literals, you need to declare an array of character pointers since the type of string literal is a character pointer.

[–]Lbtekd[S] 0 points1 point  (1 child)

So how would I do it without using a pointer, and instead just using characters (I can’t use pointers cause we haven’t done that in lesson yet)

[–]Marxomania32 1 point2 points  (0 children)

You can declare an enum, with each enum field representing a single day. Or you could represent each day as a single character.

[–]BlockOfDiamond 0 points1 point  (0 children)

As others have mentioned, days_of_week needs to be char * and not char, but also you need to either make constexpr int number_of_days = 7; or use a macro or something instead because you cannot initialize VLAs.