I saw the logo Onion Omega. I made my logo three letters "TIM" and in 6 lines, trying to display one of them and get a Ошибка сегментирования (сделан дамп памяти) .

Here is the code:

 #include <stdio.h> int main(void) { char logo[] = { '/////// /// // //', '/ / /// //// ////', '/// /// // // // //', ' / / /// // // //', ' / / /// // //', ' /// /// // //' }; printf(logo[1]); return 0; } 

How to fix it? Maybe I'm doing something wrong?

    2 answers 2

    1. An array of strings is a pointer to a string pointer, or an array of string pointers on a stack.
    2. Strings are enclosed in 2 quotes, in single characters.
    3. Strings that do not change are of type const char* not char*
    4. An array that does not change has type const char* const (for optimization)
     const char* const logo[] = { "/////// /// // //", "/ / /// //// ////", "/// /// // // // //", " / / /// // // //", " / / /// // //", " /// /// // //" }; for(int i = 0; i < sizeof(logo) / sizeof(*logo); i++) { printf("%s\n", logo[i]); } 

    Test ideone

    • Question about the for loop: um, but why divide sizeof (logo) by sizeof (* logo)? What does * in the divider? - Super_Puper_User
    • Divide pointer size by pointer by pointer element size to get array size. You can use logo[0] . * dereferencing a pointer to a pointer retrieving the address of an element, in this case a pointer to a string. - LLENN
    • Do not worry, sizeof sizes are calculated at the compilation stage, and will not play any role in runtime. - LLENN
    • Well ...... The hastebin.com/abutapagol.cpp version weighs 8,720 bytes, and your version is 8,728 bytes. BY 8 BYTE MORE! 1! - Super_Puper_User
    • one
      If you need a smaller size like that - try const char * const logo = { "/////// /// // //\n" "/ / /// //// ////\n" "/// /// // // // //\n" " / / /// // // //\n" " / / /// // //\n" " /// /// // //" }; puts(logo); const char * const logo = { "/////// /// // //\n" "/ / /// //// ////\n" "/// /// // // // //\n" " / / /// // // //\n" " / / /// // //\n" " /// /// // //" }; puts(logo); :) - Harry
     #include <stdio.h> int main(void) { char *logo[6] = { "/////// /// // //", "/ / /// //// ////", "/// /// // // // //", " / / /// // // //", " / / /// // //", " /// /// // //" }; printf("%s",logo[1]); return 0; }