"C++ memset()"의 두 판 사이의 차이

(새 문서: ==개요== ;C++memset() <syntaxhighlight lang='cpp' run> #include <iostream> #include <cstring> using namespace std; int main() { char str[] = "hello world"; memset(str, '*'...)
 
 
(같은 사용자의 중간 판 5개는 보이지 않습니다)
2번째 줄: 2번째 줄:
;C++memset()
;C++memset()


==char배열==
<syntaxhighlight lang='cpp' run>
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
10번째 줄: 11번째 줄:
     char str[] = "hello world";
     char str[] = "hello world";
     memset(str, '*', 5);
     memset(str, '*', 5);
     cout << str;
     cout << str; // ***** world
}
</syntaxhighlight>
 
==int배열==
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
    int nums[] = {1,2,3,4};
    for(auto n: nums) cout << n << ' '; // 1 2 3 4
    cout << '\n';
   
    memset(nums, 0, sizeof(nums));
    for(auto n: nums) cout << n << ' '; // 0 0 0 0
}
}
</syntaxhighlight>
</syntaxhighlight>
16번째 줄: 32번째 줄:
==같이 보기==
==같이 보기==
* [[C++ fill()]]
* [[C++ fill()]]
* [[C++ memcpy()]] - 메모리 블록 복사
* [[C++ strncpy()]] - 문자열에서 문자 복사
* [[C++ memcmp()]] - 두 메모리 블록 비교
* [[C언어 memset()]]
* [[C언어 memset()]]


[[분류: C++]]
==참고==
* https://cplusplus.com/reference/cstring/memset/
 
[[분류: C++ cstring]]

2024년 1월 2일 (화) 14:42 기준 최신판

1 개요[ | ]

C++memset()

2 char배열[ | ]

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char str[] = "hello world";
    memset(str, '*', 5);
    cout << str; // ***** world
}

3 int배열[ | ]

#include <iostream>
#include <cstring>
using namespace std;
int main() {
    int nums[] = {1,2,3,4};
    for(auto n: nums) cout << n << ' '; // 1 2 3 4 
    cout << '\n';
    
    memset(nums, 0, sizeof(nums));
    for(auto n: nums) cout << n << ' '; // 0 0 0 0 
}

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}