0

This is an array of strings in C language. I try to print the elements, but the program doesn't print them. What is the error in the code?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    char day[7]={"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday"};
 
    printf("%s\n", day[0]);
}
4
  • 2
    No, that is not an array of strings. It is an array of characters. day[0] evaluates to a char, but the format specifier %s expects a char *. Commented Jan 15, 2023 at 12:06
  • Voting to close as in need of debug details. Even trying to run this through a compiler would have pointed you at the place where you're going wrong. Commented Jan 15, 2023 at 12:20
  • 1
    What compiler/version are you using? Modern gcc refuses to compile that, even when turning off warnings/errors. Oldest gcc i tried it on was 4.1.2, (released 15 years ago), and even that refused. If you didn't get any errors, I strongly advice to upgrade. Commented Jan 15, 2023 at 14:12
  • Even if the compiler does not right away refuse to compile, you should get some warnings from your compiler: "Making integer value from pointer without a cast", "Type mismatch between format specifier %s which expects char* and provided argument of type char". Did you ignore all these warnings? Commented Jan 15, 2023 at 17:31

2 Answers 2

3

The statement

char day[7];

declares an array of chars, which can only accommodate 7 bytes — including the null-terminator if it's a string — not 7 strings of indeterminate length.

You can instead declare an array of pointers to char, or char *s, where each pointer points to a sequence of chars in memory.

const char *day[7] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

In memory, it will look something like this:

--------------------------------------
day[0] -> | "Saturday"
--------------------------------------
day[1] -> | "Sunday"
--------------------------------------
day[2] -> | "Monday"
--------------------------------------
day[3] -> | "Tuesday"
--------------------------------------
day[4] -> | "Wednesday"
--------------------------------------
day[5] -> | "Thursday"
--------------------------------------
day[6] -> | "Friday"
--------------------------------------

As day is an array of pointers, you can make it's elements (i.e. pointers) point to somewhere else in memory, but you can't change the string literals themselves.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank You very much It's working with me, but if I Want to use Scanf there is an error: int x; scanf("%d",&x); printf("%s",day[x]);
And what is the error?
I enter the value, but it does not give me the value from the array
int x; scanf("%d",&x); printf("%s",day[x]);
How have you declared day? What input did you provide? Did it compile without any warnings?
|
0

This declaration

char day[7]={"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday"};

is invalid.

There is not declared an array of strings. There is declared an array of 7 characters that are initialized by addresses of string literals.

The compiler should issue a message relative to this declaration because there is no implicit conversion from the type char * (the type of the initializing expressions) to the type char.

Instead you should declare a one-dimensional array of pointers like

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

or better like

const char * day[7] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday"};

because you may not change string literals (though in C opposite to C++ string literals have types of non-constant character arrays)

In these declarations the string literals having array types are implicitly converted to pointers to their first characters.

Or a two-dimensional array like

char day[7][10] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday"};

In this declaration all characters including the terminating zero character '\0' of the string literals are used to explicitly initialize elements of the declared array. All characters of the array that do not have a corresponding explicit initializer will be zero initialized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.