2016-10-28 1 views
-2

인쇄의 printf와 C의 문자를 인쇄 : 나는 그것을 실행하면 내가못해, 나는 다음과 같은 코드를 한

#include <stdio.h> 
#include <string.h> 

int main(void) 
{ 
    char buff[50]; 
    int pass = 0; 

    printf("\n Enter the password : \n"); 
    gets(buff); 
    char str[80]; 
    strcat(str, "nw"); 
    strcat(str, "ww"); 
    strcat(str, "io"); 
    strcat(str, "oi"); 

    char str2[22]; 
    strcat(str2, "jm"); 
    strcat(str2, "qw"); 
    strcat(str2, "ef"); 
    strcat(str2, "io"); 
    strcat(str2, "nw"); 
    strcat(str2, "ce"); 
    strcat(str2, "or"); 
    strcat(str2, "ww"); 
    strcat(str2, "qf"); 
    strcat(str2, "ej"); 
    strcat(str2, "oi"); 


    if(strcmp(buff, str)) 
    { 
     /* we sf as df er fd xc yyu er we nm hj ui ty as asd qwe er t yu as sd df rt ty qw sd password */ 

     printf ("\n Wrong Password \n"); 
    } 

    else 
    { 
     printf ("\n Correct Password \n"); 
     pass = 1; 
    } 

    if(pass) 
    { 
     printf ("\n\n\n\n\n\n\n%s", str2); 
    } 

    return 0; 
} 

를 얻을 :

[email protected]:~# ./root.flag 

Enter the password : 
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 

Wrong Password 







Segmentation fault (core dumped) 

그것은 개행 문자 만의 값이 아닌 저장소를 인쇄 str2. str2를 화면에 인쇄하려면 어떻게해야합니까? 나는 C가 상당히 새롭기 때문에 나는 이것이 작동하지 않는 이유를 전혀 모른다. 내가 간과하는 것은 아마도 단순 할 것 같다.

+0

코드에 '% c'변환 유형 지정자가 없습니다. – Olaf

답변

5

버프는 50 자이지만 131 "A"를 입력하면 정의되지 않은 동작이 발생합니다. 또한 str2는 22 문자 뿐이므로 23이어야하며 '\ 0'NULL로 끝내서 인쇄해야합니다. 초기화하기 전에 실제로 strcat'ing해서는 안됩니다. 시도해보십시오. str2[23] ={0};

strcat (str, new) 함수는 str 문자열의 끝을 찾아 new에 문자를 추가하기 시작합니다. 문자열의 끝은 '\ 0'또는 0으로 정의됩니다. 지역 변수를 초기화하여 원하는 값을 가져야합니다. 그 코드는 str1[80]={0};이고 str2[23]={0};

+1

'strcat's도'str'과'str2'가 초기화되지 않았기 때문에 UB입니다 – yano

+0

@yano, 그냥 답을 업데이트했습니다. – cleblanc

+0

누군가 코드에서 의미하는 바를 누군가가 나에게 보여줄 수 있습니까? – ChrisMan