2016-12-26 4 views
1

왜 내 코드가 원하는 결과를 제공하지 않는지 이해하고 싶습니다. 임의의 문자를 생성 한 다음 각 문자가 생성 된 횟수를 계산해야합니다. 내 예상 출력은 다음과 비슷할 것입니다.올바른 strcat 사용법 및 'asterisk'var 손상됨

a: * 
b: ****** 
c: ** 
d: ** 
e: 
f: * 

무엇이 잘못 되었습니까?

#include <stdio.h> 
#include <stdlib.h> 

#define N 3 
#define lettera 97 
#define M 5 

int main() 
{ 
    int c, i,random[M]; 
    char charray[N], asterisk[M]; 

    // array of random numbers which in turn will genrate the randomnes in our sequence of letters 

    for (i = 0; i < M; i++) 
    { 
     random[i] = rand() % 3; 
     random[i] = random[i] + lettera +i; 
     printf("random letters : %c\n", random[i]); 
    } 

    //the char array is build of the abc given the constanet N 
    for (i = 0; i < N; i++) 
    { 
     charray[i] = lettera + i; 
    } 

    for (i = 0; i < N; i++) 
    { 
     if ((random[i] >= lettera) || (random[i] <= lettera + N - 1)) 
     { 
      for (c = 0; c < M; c++) 
       if (charray[i] == random[c]) 
       { 
        strcat(asterisk, "*"); 
       } 
     } 
    } 

    for (i = 0; i < N; i++) 
    { 
     printf("%c: %s\n", charray[i], asterisk); 
    } 

    return 0; 
} 

--------- 갱신 -------------------

#include <stdio.h> 
#include <stdlib.h> 


#define N 3 
#define lettera 97 
#define M 5 
#define buff 40 



int main() 
{ 
    int c, i,random[M]; 
    char charray[N], asterisk[buff] = ""; 


    // array of random numbers which in turn will genrate the randomnes in our sequence of letters 

    for (i = 0; i < M; i++) 
    { 
     random[i] = rand() % 3; 
     random[i] = random[i] + lettera +i; 
     printf("random letters : %c\n", random[i]); 
    } 


    //the char array is build of the abc given the constanet N 
    for (i = 0; i < N; i++) 
    { 
     charray[i] = lettera + i; 
    } 

    for (i = 0; i < N; i++) 
    { 
     if ((random[i] >= lettera) || (random[i] <= lettera + N - 1)) 
     { 
      for (c = 0; c < M; c++) 
       if (charray[i] == random[c]) 
       { 
        strcat(asterisk[i], "*"); 
       } 
     } 
    } 



    for (i = 0; i < N; i++) 
    { 
     printf("%c: %s\n", charray[i], asterisk[i]); 
    } 

    return 0; 
} 

------- ---------------------- another update -------------------------- --------

그래서 strcat (별표 [i], "")에서 변경되었습니다. -> strcat (별표, "") 그리고 나는 더 이상 오류가 없지만 어떻게 조절할 수 있는지 이해하지 못합니다. 올바른 숫자로 "*"를 입력해야합니다. 편지) 나타났다 내가지고있어 -

무작위로 글자 :

무작위로 글자 C :

무작위로 글자 C :

무작위로 글자 b를 :

,536,913 b를 63,210

무작위로 글자 : *****

B : *****

C :

는 C ***** 내가

를 GET-싶은

A :

B : **

C : ***

나는 이것을 달성하기 위해 어떤 방법으로 접근 하는가?

#include <stdio.h> 
#include <stdlib.h> 

#define N 3 
#define lettera 97 
#define M 5 
#define buff 40 



int main() 
{ 
    int c, i,random[M]; 
    char charray[N],abc[M], asterisk[buff] = ""; 


    // array of random numbers which in turn will genrate the randomnes in our sequence of letters 

    for (i = 0; i < M; i++) 
    { 
     random[i] = rand() % 3; 
     random[i] = random[i] + 'a'; 
     printf("random letters : %c\n", random[i]); 
    } 


    //the char array is build of the abc given the constanet N 
    for (i = 0; i < N; i++) 
    { 
     charray[i] = lettera + i; 
    } 

    for (i = 0; i < N; i++) 
    { 
     if ((random[i] >= lettera) || (random[i] <= lettera + N - 1)) 
     { 
      for (c = 0; c < M; c++) 
       if (charray[i] == random[c]) 
       { 
        strcat(asterisk, "*"); 
       } 
     } 
    } 



    for (i = 0; i < N; i++) 
    { 
     printf("%c: %s\n", charray[i], asterisk); 
    } 

    return 0; 
} 

------------------------ 3 번째 updtae ----------------- -----------------

그래서 내가 원하는대로 outpout을 제어하는 ​​데 도움이되는 함수를 추가했지만, "strcat 'undefined;와 같은 오류가 계속 발생합니다. 하는 int 복귀 통근 가정 " 또는 '초기화' '숯은 문자'에서의 간접 레벨 다르다"[1] " 내가 모두 초기화, 그래서이 문제가 발생하는 이유를 이해 해달라고 이때 ..

#include <stdio.h> 
#include <stdlib.h> 

#define N 3 
#define lettera 97 
#define M 5 
#define buff 40 

char aster(int flag, int array[]); 

int main() 
{ 
    int c, i, random[M], times[M] = { 0 }; 
    char charray[N]; 


    // array of random numbers which in turn will genrate the randomnes in our sequence of letters 

    for (i = 0; i < M; i++) 
    { 
     random[i] = rand() % 3; 
     random[i] = random[i] + 'a'; 
     printf("random letters : %c\n", random[i]); 
    } 


    //the char array is build of the abc given the constanet N 
    for (i = 0; i < N; i++) 
    { 
     charray[i] = lettera + i; 
    } 

    for (i = 0; i < N; i++) 
    { 
     if ((random[i] >= lettera) || (random[i] <= lettera + N - 1)) 
     { 
      for (c = 0; c < M; c++) 
       if (charray[i] == random[c]) 
       { 
        times[i]++; 
       } 
     } 
    } 



    for (i = 0; i < N; i++) 
    { 
     printf("%c: %s\n", charray[i], aster (i,times)); 
    } 

    return 0; 
} 


char aster(int flag,int array[]) 
{ 

    int i; 
    char ch="", asterisk[buff] = ""; 
    for (i = 0; i < array[flag]; i++) 
    { 
     ch = strcat(asterisk, "*"); 
    } 
    return ch; 
} 
+0

예상되는 결과와 실제 결과의 예를 기술하십시오. – kaylum

+1

'별표 [M]; strcpy (별표, "이것들");'당신은 거기서 무엇을하려하고 있습니까? 'M'은'5'이지만''this''는 저장할 때 7자를 필요로합니다. 그리고 분명히 더 이상'strcat'에서'asterisk'까지는 계속해서 버퍼 오버 플로우를 계속합니다. – kaylum

+0

나는 그것을 고쳐 썼다, 유감스럽게도 버전이 어떻게 작동하는지 이해하려고 노력함에 근절되었다. 내가 가지고 있어야하는 것 대신에 그것을 복사했다. 나는 지금 편집했다. – mz1993

답변

1

제가 생각하기에, 작은 것, malloc을 통해 할당 된 메모리를 해제하는 데 필요한 최상의 장소는 어디입니까?

#include <stdio.h> 
#include <stdlib.h> 

#define N 8 
#define lettera 97 
#define M 20 
#define buff 40 

char *aster(int flag, int array[]); 

int main() 
{ 
    int c, i, random[M], times[M] = { 0 }; 
    char charray[N],z; 


    // array of random numbers which in turn will genrate the randomnes in our sequence of letters 

    for (i = 0; i < M; i++) 
    { 
     random[i] = rand() % 4; 
     random[i] = random[i] + 'a'; 
     printf("random letters : %c\n", random[i]); 
    } 


    //the char array is build of the abc given the constanet N 
    for (i = 0; i < N; i++) 
    { 
     charray[i] = lettera + i; 
    } 

    for (i = 0; i < N; i++) 
    { 
     if ((random[i] >= lettera) || (random[i] <= lettera + N - 1)) 
     { 
      for (c = 0; c < M; c++) 
       if (charray[i] == random[c]) 
       { 
        times[i]++; 
       } 
     } 
    } 



    for (i = 0; i < N; i++) 
    { 
     printf("%c: %s\n", charray[i], aster(i, times)); 
    } 

    return 0; 
} 


char *aster(int flag, int array[]) 
{ 

    int i,keylen; 
    char asterisk[buff] = ""; 
    char *key; 

    for (i = 1; i < array[flag]; i++) 
    { 
     strcat(asterisk, "*"); 
    } 
    /* Initial memory allocation */ 
    key = (char*)malloc(buff * sizeof(char)); 
    strcpy(key, asterisk); 


    return key; 
}