I started to learn si. And the topic with strings is not very given to me. Here is the code:

#include <stdio.h> void main () { char *card="JQK"; puts(card[0]); } 

gcc on linux says this:

  expected 'const char *' but argument is of type 'char' extern int puts (const char *__s); ^ Ошибка сегментирования 

Please explain. And then I did not understand something.

    2 answers 2

    puts() prints a string , and you try to print a single character. Accordingly, this symbol is perceived as some kind of address, and since it is not so - the conversion occurs, “where it is impossible”.

     puts(card); 

    will print your string. If you need to display only the first character, then output it like this:

     putchar(card[0]); 

      The puts function has the following declaration

       int puts(const char *s); 

      As can be seen from the declaration, its only parameter has the type of a pointer to the string const char * .

      You call this function with an argument of integer type char .

       puts(card[0]); 

      In this case, the value of this expression card[0] , which is equal to the character 'J' , is considered by the function as an address and tries to output the string located at that address.

      Could you write

       puts( &card[0] ); 

      to print a string, or

       putchar( card[0] ); 

      to output just one character.