C에서 문자열을 계속 연결시키고 싶습니다. 어떻게 해야 하나요?

jagalchee의 이미지

예를 들어,

char *a;
char *b;
...
...

이렇게 있다고 할때, a에는 가장 처음에 'abc' 라는걸 가리키고 잇다고 하죠.
근데 b에는 처음에 'def' 가 저장되어 있습니다. 그리고 a와 b를 연결시켜서 이걸 a가 가리키도록 하면, a는 이제 'abcdef'를 가리키게 됩니다. 이 후에 b는 다시 'ghijklmn'을 가리키게 되고, 다시 a와 b를 연결시켜서, a가 'abcdefghijklmn'을 가리키게 하고..이것을 계속 반복하고 싶습니다. 어떤 함수를 이용해서, 어떤 방법을 이용하면 될런지가 잘 감이 안오네요. 한번만 a와b를 붙이는건 알겠는데, 계속해서 이런 작업을 할려면 어쨋든 루프문 안에서 넣어야 할 터인데..b는 그렇다 쳐도, a에 할당한 메모리 공간이 계속 늘어나니까 문제가 될거 같습니다..(a를 엄청나게 크게 malloc하시라고 하면 어쩔 수 없지만요. ^^)

아시는 분께서는 답변 꼭 부탁드립니다.

qufdl113의 이미지

realloc이라는것도 있어요. 동적으로 계속 메모리공간을 늘리면서 확보해줘여.
자세한건 man realloc해 보세요.

sangwoo의 이미지

realloc(3)? ;-)

ps. 늦었군요 :oops:

----
Let's shut up and code.

bluemoon의 이미지

jagalchee wrote:
한번만 a와b를 붙이는건 알겠는데, 계속해서 이런 작업을 할려면 어쨋든 루프문 안에서 넣어야 할 터인데..b는 그렇다 쳐도, a에 할당한 메모리 공간이 계속 늘어나니까 문제가 될거 같습니다..(a를 엄청나게 크게 malloc하시라고 하면 어쩔 수 없지만요. ^^)

realloc을 사용하시면 됩니다만 성능상의 문제는 여전히 남아있을테고
제가볼땐 큐를 구현하시면 될듯 합니다. 혹 삭제와 수정과 같은 연산이
필요하시면 단일 연결리스트도 편할것 같네요.

댓글 달기

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