[C++] char 배열을 memset으로 끝까지 0으로 초기화 한 후 출력하면 에러

jang900824의 이미지

char temp[35];
memset(temp, '0', 35);
cout<<temp<<endl;

위의 소스코드를 실행하면 0이 35개가 출력되어야 하지만 실제론 0이 35개 출력되고 그 뒤에 이상한 글자 4~5개 정도가 더 출력 됩니다.
아마 temp[34]가 공백이 아니라서 문자열의 끝부분을 찾지 못하는것 같은데, temp[34]를 \0으로 설정하는 대신 다른 방법이 있을까요?

qiiiiiiiip의 이미지

없습니다.

champion의 이미지

#include "stdafx.h"
#include <memory.h>
#include <iostream>
using namespace std;
void _tmain(int argc, _TCHAR* argv[])
{
char temp[35];
memset(temp, '0', 35);
for(int i=0;i<sizeof(temp);i++)
{
 
cout <<temp[i];
 
}
 
}

정말 불쌍한 코드죠..~~

완벽한 세상을 위하여!

익명 사용자의 이미지

char temp[35] = { 0, };
memset(temp, '0', 34);
cout<

qiiiiiiiip의 이미지

없는것같은데 이렇게라도 3

    string str;
    str.append( 35, '0' );
    cout << str << endl;
HDNua의 이미지

없는것같은데 이렇게라도 4

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
 
#define BUF_MAX     10
 
int main(void)
{
    char tmp[BUF_MAX];
    char *ptr = tmp;
 
    memset(tmp, '0', BUF_MAX);
 
    while (ptr!=tmp+BUF_MAX)
        cout<<*ptr++;
 
    return 0;
}

저는 이렇게 생각했습니다.

jang900824의 이미지

float temp[35];
memset(temp, '0', 34);
temp[34]='\0';
cout<<temp<<endl;

으로 할 수 밖에 없었네요.

shint의 이미지

00000 문자열을. memcpy 나 strcpy 등으로 복사해서 사용하는것도 가능해 보입니다.

char * temp1 = "00000";
cout<< temp1 <<endl;
 
char temp2[5+1] = "00000";
cout<< temp2 <<endl;

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

ymir의 이미지

일반적으로 string 이라 함은 null-terminated string 을 의미합니다.
대부분의 string 을 다루는 함수들은 null-terminated string 을 가정하고 작성되어 있습니다.
만약 null-terminated 되지 않은 문자열을 다루고자 한다면, 사실상 binary data 와 다를 바 없으니..
길이(length) 정보를 주고 해당 길이만큼만 정확하게 처리하는 루틴을 따로 만드셔야 합니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.