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

 
(같은 사용자의 중간 판 하나는 보이지 않습니다)
2번째 줄: 2번째 줄:
;C++memset()
;C++memset()


==char배열==
<syntaxhighlight lang='cpp' run>
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
11번째 줄: 12번째 줄:
     memset(str, '*', 5);
     memset(str, '*', 5);
     cout << str; // ***** world
     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>
24번째 줄: 40번째 줄:
* https://cplusplus.com/reference/cstring/memset/
* https://cplusplus.com/reference/cstring/memset/


[[분류: C++]]
[[분류: 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 }}