다차원 포인터 배열 어떻게 인자로 넘겨줘야할까요?

khris의 이미지

제가 어떤 포인터들의 이차원 배열을 만들고싶어서 다음과 같은 코드를 작성했습니다.

Block * _Array[FIELD_WIDTH][FIELD_HEIGHT];

그런데 이 배열을 다른 함수에 인자로 넘겨줘야 할 일이 생겨서 다음과 같이 캐스팅해서 넘겨주었습니다.

(Block ***)_Array

Block *** _Data = (Block ***)_Array 인 상태라면 _Array[1][1]을 억세스할때, _Data로는 어떻게 해 주어야할까요?

_Data[1][1] 하면 안되더군요... 아무래도 _Data[1][1]과 _Array[1][1]은 다른가봅니다. 다른 메모리 주소를 내뱉네요.

wariua의 이미지

_Array[1][1] == *(_Array + 1 * FIELD_HEIGHT + 1)
_Data[1][1] == (*(_Data + 1))[1] == *(*(_Data + 1) + 1)

이다보니...

고전적인 문제네요:) 검색 키워드는 "차원 and 배열" 정도가 될까요-
http://bbs.kldp.org/viewtopic.php?t=56742
http://bbs.kldp.org/viewtopic.php?t=48067
...

$PWD `date`

khris의 이미지

감사합니다. :)

오늘도 많이 배워가는군요...

갈수록 C/C++이 싫어져가는 견습 프로그래머입니다.

───────────────────────
yaourt -S gothick elegant
khris'log

ixevexi의 이미지

khris wrote:
제가 어떤 포인터들의 이차원 배열을 만들고싶어서 다음과 같은 코드를 작성했습니다.

Block * _Array[FIELD_WIDTH][FIELD_HEIGHT];

그런데 이 배열을 다른 함수에 인자로 넘겨줘야 할 일이 생겨서 다음과 같이 캐스팅해서 넘겨주었습니다.

(Block ***)_Array

Block *** _Data = (Block ***)_Array 인 상태라면 _Array[1][1]을 억세스할때, _Data로는 어떻게 해 주어야할까요?

_Data[1][1] 하면 안되더군요... 아무래도 _Data[1][1]과 _Array[1][1]은 다른가봅니다. 다른 메모리 주소를 내뱉네요.

저도 방금까지 고민해봤네요 ^^
우선 block[][]과 block **은 다릅니다.
저렇게 _Data를 선언하면 incompatible pointer어쩌구 에러가 나야하는데 저기서 캐스팅을 선언해서 에러가 나지 않았습니다
(C의 고질적인? 문제 아닐가요? :wink: )
정확히 _Data는 이렇게 선언되어야 합니다.
Block * (*data)[FIELD_HIEGHT] = _Array;
이젠 모든게 OK!

복잡한 선언문 으로 구글링 하시거나
전웅님의 C언어 펜더멘탈의 선언부분을 다룬 챕터를 읽어보셔용

C++, 그리고 C++....
죽어도 C++

doldori의 이미지

선언이 복잡해서 어지러우면 typedef를 써서 생각해보는 것도 좋은 방법입니다.

typedef Block* Array[FIELD_HEIGHT];
void f(Array* _Data);

Array _Array[FIELD_WIDTH];
f(_Array);

ps. _로 시작하는 식별자는 사용하지 않는 것이 좋습니다.

khris의 이미지

그렇군요... 대충 감은 잡히는데 아직 아리까리한점이 있습니다.

제가 목표로 하는것은 Block 의 2차원 배열이 아닌, Block의 포인터(Block *)의 2차원 배열입니다.

그런경우 제가

Block * _Array[X][Y]

라고 하는게 맞는것인지,

Block (* _Array)[X][Y]

라고 하는게 맞는것인지 잘 모르겠습니다.

그럼 이번에도 ㅊㄱㅅ분들의 멋진 답변 기대하겠습니다.

───────────────────────
yaourt -S gothick elegant
khris'log

wariua의 이미지

역시 다소 고전적인 문제입니다 :twisted:

http://bbs.kldp.org/viewtopic.php?t=39819

[]가 *보다 우선순위가 높은지라

Block * _Array[X][Y]


Block * (_Array[X][Y])

와 동등합니다. 그리고 이걸 풀어서 읽어보자면 (어느 KLDP wiki 문서에 따르면) "_Array is array[X][Y] of a pointer to Block"이 됩니다. 그리고,
Block (* _Array)[X][Y]

는 "_Array is a pointer to array[X][Y] of Block"이 되구요.

$PWD `date`

댓글 달기

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