배열의 첨자에 관해서...

익명 사용자의 이미지

어떤 문자열1을 입력 받아서 strlen()함수로 길이를 구했습니다.

또한 문자열2에다가도 다른 길이의 문자를 입력 받았습니다.

또한 strlen()함수로 길이를 구했습니다.

문제는 arry[]의 크기를 문자열1과 문자열2의 더한 값으로 하고 싶은데

되질 않네요.

예)

char *ptr1 = "abcdef";
char *ptr2 = "aaa";
int num1 = 0;
int num2 = 0;

num1 = strlen(ptr1);
num2 = strlen(ptr2);

char array[num1 + num2];

위와같은 식으로 하고 싶은데... 당연히 안되겠죠? array의 크기를

넣어 줄 수 있는 방법이 있을까요? 위는 그냥 제가 한거고 임의로

문자를 입력 받게 해서 할려고 그러는데...

익명 사용자의 이미지

이런경우 쓰는게 malloc나 calloc죠.

array = (char *)malloc((num1 + num2)*sizeof(char));
이렇게 바꿔주시면 되겠네요.
포인터는 배열처럼 인덱싱 할 수 있는건 아시죠?
처리하고 나서 free 하는거 잊지 마시고요..
그럼..

익명 사용자의 이미지

김석훈 wrote..
어떤 문자열1을 입력 받아서 strlen()함수로 길이를 구했습니다.

또한 문자열2에다가도 다른 길이의 문자를 입력 받았습니다.

또한 strlen()함수로 길이를 구했습니다.

문제는 arry[]의 크기를 문자열1과 문자열2의 더한 값으로 하고 싶은데

되질 않네요.

예)

char *ptr1 = "abcdef";
char *ptr2 = "aaa";
int num1 = 0;
int num2 = 0;

num1 = strlen(ptr1);
num2 = strlen(ptr2);

char array[num1 + num2];

위와같은 식으로 하고 싶은데... 당연히 안되겠죠? array의 크기를

넣어 줄 수 있는 방법이 있을까요? 위는 그냥 제가 한거고 임의로

문자를 입력 받게 해서 할려고 그러는데...

C 문자열에 실제로 필요한 공간은 strlen의 리턴값 + 1 입니다.
마지막에 '\0' = 0 = NULL 을 넣어서 문자열의 끝을 나타내니까요.

문자열을 편하게 다루시려면 ANSI C++ 라이브러리의 string 타입을 쓰시는 걸 권장합니다.

예를 들어,

string str1 = "abcde", str2 = "aaa", str;
str = str1 + str2;
assert (str == "abcdeaaa");
assert (str[4] == "e");
assert (str[5] == "a");

또한 가변 크기의 배열을 쓰시려면,
동적할당 배열 대신 vector 컨테이너의 사용을 권장합니다.

vector vec(num1 + num2);

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.