Programing

char 배열 c 지우기

lottogame 2020. 8. 17. 09:29
반응형

char 배열 c 지우기


첫 번째 요소를 null로 설정하면 char 배열의 전체 내용이 지워질 것이라고 생각했습니다.

char my_custom_data[40] = "Hello!";
my_custom_data[0] = '\0';

그러나 이것은 첫 번째 요소 만 null로 설정합니다.

또는

my_custom_data[0] = 0; 

를 사용하는 대신 memset위의 두 가지 예가 모든 데이터를 지워야한다고 생각했습니다.


어레이를 보는 방법에 따라 다릅니다. 배열을 일련의 문자로 보는 경우 데이터를 지우는 유일한 방법은 모든 항목을 터치하는 것입니다. memset이를 달성하는 가장 효과적인 방법 일 것입니다.

반면에 이것을 C / C ++ null로 끝나는 문자열로보기로 선택한 경우 첫 번째 바이트를 0으로 설정하면 문자열이 효과적으로 지워집니다.


C의 배열은 메모리 위치 일 뿐이므로 실제로 my_custom_data[0] = '\0';할당은 첫 번째 요소를 0으로 설정하고 다른 요소는 그대로 둡니다.

배열의 모든 요소를 ​​지우려면 각 요소를 방문해야합니다. 그 이유 memset는 다음과 같습니다.

memset(&arr[0], 0, sizeof(arr));

이것은 일반적으로 이것을 처리하는 가장 빠른 방법입니다. C ++를 사용할 수 있다면 대신 std :: fill을 고려하십시오.

char *begin = &arr;
char *end = begin + sizeof(arr);
std::fill(begin, end, 0);

단일 요소를 설정하면 전체 배열이 지워지는 이유는 무엇입니까? 특히 C에서는 프로그래머가 명시 적으로 프로그래밍하지 않으면 거의 발생하지 않습니다. 첫 번째 요소를 0 (또는 임의의 값)으로 설정하면 정확히 수행 한 것입니다.

초기화 할 때 배열을 0으로 설정할 수 있습니다.

char mcd[40] = {0}; /* sets the whole array */

그렇지 않으면 memset이나 비슷한 것 외에 다른 기술을 모릅니다.


사용하다:

memset(my_custom_data, 0, sizeof(my_custom_data));

또는:

memset(my_custom_data, 0, strlen(my_custom_data));

다음 코드를 시도하십시오.

void clean(char *var) {
    int i = 0;
    while(var[i] != '\0') {
        var[i] = '\0';
        i++;
    }
}

왜 사용하지 memset()않습니까? 그렇게하는 방법입니다.

첫 번째 요소를 설정하면 나머지 메모리는 그대로 유지되지만 str 함수는 데이터를 비어있는 것으로 처리합니다.


Pls는 사례 1 및 사례 2 이후 배열의 데이터로 설명 한 곳을 아래에서 찾습니다.

char sc_ArrData[ 100 ];
strcpy(sc_ArrData,"Hai" );

사례 1 :

sc_ArrData[0] = '\0';

결과:

-   "sc_ArrData"
[0] 0 ''
[1] 97 'a'
[2] 105 'i'
[3] 0 ''

사례 2 :

memset(&sc_ArrData[0], 0, sizeof(sc_ArrData));

결과:

-   "sc_ArrData"
[0] 0 ''
[1] 0 ''
[2] 0 ''
[3] 0 ''

첫 번째 인수를 NULL로 설정하는 것이 트릭을 수행하지만 memset을 사용하는 것이 좋습니다.


아니. 당신이하는 일은 첫 번째 값을 '\ 0'또는 0으로 설정하는 것뿐입니다.

null로 끝나는 문자열로 작업하는 경우 첫 번째 예제에서 예상 한 것과 유사한 동작을 얻을 수 있지만 메모리는 여전히 설정되어 있습니다.

memset을 사용하지 않고 메모리를 지우려면 for 루프를 사용하십시오.


memset을 사용해야합니다. 첫 번째 요소 만 설정하면 작동하지 않습니다. 모든 요소를 ​​설정해야합니다. 그렇지 않은 경우 어떻게 첫 번째 요소 만 0으로 설정할 수 있습니까?


Writing a null character to the first character does just that. If you treat it as a string, code obeying the null termination character will treat it as a null string, but that is not the same as clearing the data. If you want to actually clear the data you'll need to use memset.


I thought by setting the first element to a null would clear the entire contents of a char array.

That is not correct as you discovered

However, this only sets the first element to null.

Exactly!

You need to use memset to clear all the data, it is not sufficient to set one of the entries to null.

However, if setting an element of the array to null means something special (for example when using a null terminating string in) it might be sufficient to set the first element to null. That way any user of the array will understand that it is empty even though the array still includes the old chars in memory


set the first element to NULL. printing the char array will give you nothing back.


How about the following:

bzero(my_custom_data,40);

I usually just do like this:

memset(bufferar, '\0', sizeof(bufferar));

void clearArray (char *input[]){
    *input = ' '; 
}

Try the following:

strcpy(my_custom_data,"");

참고URL : https://stackoverflow.com/questions/632846/clearing-a-char-array-c

반응형