Hoe een char-array in C afdrukken via printf?

Dit resulteert in een segmentatiefout.
Wat moet worden gecorrigeerd?

int main(void)
{
    char a_static = {'q', 'w', 'e', 'r'};
    char b_static = {'a', 's', 'd', 'f'};
    printf("\n value of a_static: %s", a_static);
    printf("\n value of b_static: %s\n", b_static);
}

Antwoord 1, autoriteit 100%

De geposte code is onjuist: a_staticen b_staticmoeten worden gedefinieerd als arrays.

Er zijn twee manieren om de code te corrigeren:

  • je kunt null-terminators toevoegen om van deze arrays de juiste C-strings te maken:

    #include <stdio.h>
    int main(void) {
        char a_static[] = { 'q', 'w', 'e', 'r', '\0' };
        char b_static[] = { 'a', 's', 'd', 'f', '\0' };
        printf("value of a_static: %s\n", a_static);
        printf("value of b_static: %s\n", b_static);
        return 0;
    }
    
  • Als alternatief kan printfde inhoud afdrukken van een array die niet op nul is beëindigd met behulp van het precisieveld:

    #include <stdio.h>
    int main(void) {
        char a_static[] = { 'q', 'w', 'e', 'r' };
        char b_static[] = { 'a', 's', 'd', 'f' };
        printf("value of a_static: %.4s\n", a_static);
        printf("value of b_static: %.*s\n", (int)sizeof(b_static), b_static);
        return 0;
    }
    

    De precisie die wordt gegeven na de .specificeert het maximum aantal tekens dat uit de tekenreeks moet worden uitgevoerd. Het kan worden opgegeven als een decimaal getal of als *en worden opgegeven als een int-argument vóór de char-aanwijzer.


Antwoord 2, autoriteit 2%

Dit resulteert in een segmentatiefout. ?vanwege de onderstaande verklaring

char a_static = {'q', 'w', 'e', 'r'};

a_staticmoet char arrayzijn om meerdere karakters te bevatten. maak het zoals

char a_static[] = {'q', 'w', 'e', 'r','\0'}; /* add null terminator at end of array */

Ook voor b_static

char b_static[] = {'a', 's', 'd', 'f','\0'};

Antwoord 3, autoriteit 2%

Je moet array gebruiken in plaats van te declareren

a_static
b_static

als variabelen

Het ziet er dus zo uit:

int main()
{
  char a_static[] = {'q', 'w', 'e', 'r','\0'};
  char b_static[] = {'a', 's', 'd', 'f','\0'};
  printf("a_static=%s,b_static=%s",a_static,b_static);
  return 0;
}

Antwoord 4

Het punt is dat je C Style Strings gebruikt, en een C Style String wordt afgesloten met een nul.
Als u bijvoorbeeld “alien” wilt afdrukken met een char-array:

char mystring[6] = { 'a' , 'l', 'i', 'e' , 'n', 0}; //see the last zero? That is what you are missing (that's why C Style String are also named null terminated strings, because they need that zero)
printf("mystring is \"%s\"",mystring);

De uitvoer moet zijn:

mijn string is “buitenaards”

Terug naar uw code, deze zou er als volgt uit moeten zien:

int main(void) 
{
  char a_static[5] = {'q', 'w', 'e', 'r', 0};
  char b_static[5] = {'a', 's', 'd', 'f', 0}; 
  printf("\n value of a_static: %s", a_static); 
  printf("\n value of b_static: %s\n", b_static); 
  return 0;//return 0 means the program executed correctly
}

Trouwens, in plaats van arrays kun je pointers gebruiken (als je de string niet hoeft aan te passen):

char *string = "my string"; //note: "my string" is a string literal

U kunt uw char-arrays ook initialiseren met letterlijke tekenreeksen:

char mystring[6] = "alien"; //the zero is put by the compiler at the end 

Ook: Functies die werken op C Style Strings (bijv. printf, sscanf, strcmp, strcpy, etc) hebben nul nodig om te weten waar de string eindigt

Ik hoop dat je iets hebt geleerd van dit antwoord.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5 − 5 =

Other episodes