C에서 문자열 배열

sloth01의 이미지

완전 멍청한 질문입니다.

char *array1[3] = {"abc", "bcd", "egf"};
char *array2[2] = {"111", "agd"};
char *array3[4] = {"afda", "hello", "world", "name"};

와 같은 다양한 크기의 String 배열을 하나로 묶고 싶은데 어떻게 해야 하는지 감이 안잡히네요.

원하는 것은 하나의 배열에 각 배열의 포인터를 넣는건데, 실력이 없다보니

char **list[3] = {array1, array2, array3};

와 같이 넣으면 각 배열의 첫번째 값만 들어가네요.

어떻게 배열의 포인터를 얻어내야 할지 헤매고 있습니다.

너무 바보같은 질문이지만 너그러운 마음으로 알려주시면 감사하겠습니다.

kby2194의 이미지

저도 초보라 잘 모르지만 string.h 헤더에 포함된 strcpy함수 사용하면 되지않을까요?

kgcrom의 이미지

#include <stdio.h>
 
int main(int argc, char* argv[])
{
  char *array1[3] = {"abc", "bcd", "egf"};
  char *array2[2] = {"111", "agd"};
  char *array3[4] = {"afda", "hello", "world", "name"};
  char **list[3] = {array1, array2, array3};
 
  printf("%s\t%s\t%s\n", list[0][0], list[0][1], list[0][2]);
//printf("%s\t%s\t%s\n", *list[0], *(list[0]+1), *(list[0]+2))
  printf("%s\t%s\n", list[1][0], list[1][1]);
  printf("%s\t%s\t%s\t%s\n", list[2][0], list[2][1], list[2][2], list[2][3]);
 
  return 0;
}

그림으로 그려가면서 하시면 이해가 빨리되요~

흘러가고있는 지금 이 시간에 충실하자.

bushi의 이미지

#include <stdio.h>
 
int main(int argc, char* argv[])
{
  char *array1[3] = {"abc", "bcd", "egf"};
  char *array2[2] = {"111", "agd"};
  char *array3[4] = {"afda", "hello", "world", "name"};
  char **list[3] = {array1, array2, array3};
 
  printf("%s\t%s\t%s\n", 0[list][0], 0[list][1], 0[list][2]);
  //printf("%s\t%s\t%s\n", *list[0], *(1+list[0]), *(2+list[0]));
  printf("%s\t%s\n", 0[list[1]], 1[list[1]]);
  printf("%s\t%s\t%s\t%s\n", 0[2[list]], 1[2[list]], 2[2[list]], 3[2[list]]);
 
  return 0;
}
익명 사용자의 이미지

흔히들 배열이름을 포인터처럼 사용하니깐 둘이 같은걸로 보는 초보분들이 많은거 같습니다. ㅋ
배열이름이랑 포인터는 엄연히 다른것이니깐 <- 이둘의 차이점부터 먼저 보세요.
그럼 왜 그게 처음 문자열만 나오는지 이해가 됩니다!
인터넷 찾아보시면 많은자료가... ㄷㄷ
열공 하시길!!

댓글 달기

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