0

I am not new to programming however I am very new to C programming and I've been looking around on here for some help on a homework question and can't seem to find an answer.

Context: We have been asked to take a month input from the user and determine which season that month is in and print it out.

The Problem: I'm trying to declare a character array variable (season) using the output of a function in C.

Java Example: String season = determineSeason(month);

What I have right now is (not the whole program):

char month[10];
printf("Enter a month: ");
scanf("%s", month);

char season[7] = detSeason(month);
printf("season: %s", season);

char* detSeason(char month[10])
{
    char season[7];
    if ((strcmp(month, "september")) == 0 || (strcmp(month, "october")) == 0 || (strcmp(month, "november")) == 0)
{
    return strcpy(season, "Spring");
}

The issue I'm having is with the line char season[7] = detSeason(month);

The error I am getting is: array initializer must be an initializer list or string literal

My guess is that it's trying to use the function call as the string literal and not actually calling the function and storing the output in the variable.

Could someone please let me know what I am doing wrong.

Note: I have cut off the rest of the else if statements to not make this post too long, but it follows the same format as the first if statement.

Thank you in advance :)

p.s. Does anyone know why it won't let me put "Hello," at the start of my post, it keeps cutting it off?

  • Try `char season[7] = detSeason(month);` --> `char season[7]; strcpy(season, detSeason(month);` – chux - Reinstate Monica Sep 19 '19 at 04:21
  • Rest assured that there is no compelling reason that `char season[7] = detSeason(month);` is not allowed, at least for an array with automatic storage duration (declared in a function). For example, the very similar `char season[7] = "123";` is, as is `char season[7] = "123456";` and even `char season[7] = "1234567";` The short strings leave part of the array uninitialized, the long string does not terminate `season` with a null byte. – Peter - Reinstate Monica Sep 19 '19 at 04:29
  • Since the name of the seasons are fixed, you don't need a separate array to hold the season name. Just use a pointer, and point it to the right string-litteral. No array or string copying is needed. – HAL9000 Sep 19 '19 at 14:44

1 Answers1

0

Your error occurs here:

char season[7] = detSeason(month);

In c you can't just define an array and assign a string to it. You need to iterate through every single element in the string and assign it individually. Something like (pseudocode):

for i in (length of string):
    array[i] = string[i]

Another thing you will need to look into is malloc() as since your strings will be of varying length, you will need to make arrays of varying length so that when you compare the strings they are equal.

For instance, if i define an array of static length 7 like your example above, and the month is may: the array will look like: [m, a, y, X, X, X, X] where X is kind of 'left over space' which will mean your string compare wont return true.

Let me know if you need anything explained in more detail :)

DoubleRainbowZ
  • 132
  • 1
  • 9
  • 2
    There are standard library functions called `strcpy` or better [`strncpy`](http://www.cplusplus.com/reference/cstring/strncpy/)doing the iterating for you. – Peter - Reinstate Monica Sep 19 '19 at 04:19
  • 1
    Detail: `char season[7] = detSeason(month);` is not a "define an array and assign a string to it". It is an attempted _initialization_, not _assignment_ - similar yet different. As `detSeason(month)` returns a `char *`, code cannot initialize an `char` array with a pointer to `char`. – chux - Reinstate Monica Sep 19 '19 at 04:19
  • Ah, I see, thanks for that :D Sorry @Nathan Winspear if I've cause any confusion with my incorrect answer – DoubleRainbowZ Sep 19 '19 at 04:21
  • Your answer was very helpful, malloc() is something I only knew very little about but I see how it factors in here, one thing I would like to ask is how would I go about implementing the pseudocode you showed. should I use a pointer instead, or should I loop through the memory addresses for each character and assign it to the array?. – Nathan Winspear Sep 19 '19 at 04:27
  • Thanks@PeterA.Schneider, do I declare the array and call the function within strcpy? `strcpy(season, detSeason())` – Nathan Winspear Sep 19 '19 at 04:31
  • @NathanWinspear Exactly. – Peter - Reinstate Monica Sep 19 '19 at 04:33
  • lets say i want to copy string of size n. I would do something like `char new_word = malloc(sizeof(char)*n);` (make sure you are always freeing your mallocs at the end) then your for loop would look something like `for (int i = 0; i < n; i++) {new_word[i] = old_word[i]}` – DoubleRainbowZ Sep 19 '19 at 04:37