Double pointer 주소값 구하는법

rriiaa의 이미지

안녕하세요 프로그래밍 초보인데 궁금한 점이 있습니다!

더블 포인터로 2D array를 malloc으로 동적할당할 경우에

virtual address는 어떻게 볼 수 있나요??

아래 코드에서 &array[i][j] 라고 하면 안되더라고요...

    int **array = (int **) malloc(sizeof(int *) * (row_size));
    for(int i=0; i<row_size; i++)
        array[i] = (int *) malloc( col_size * sizeof(int) );
 
    for(int i=0; i<row_size;i++){
        for(int j=0; j<row_size;j++){
            array[i][j]=i;
            printf(array value=%d, array address=%x \n, array[i][j], &array[i][j]);
        }
    }
raymundo의 이미지

딱히 문제 없어보이는데, "안되더라고요"가 정확히 어떤 의미인지 적어주시는 게 좋아보입니다.
저 출력문에서 아무 값도 안 나오진 않았을테고, 뭐가 나오길 기대했는데 실제론 어땠다는 식으로요.
(안쪽 루프에 col_size를 써야 할 자리에 row_size가 적혔다거나 printf에 따옴표가 빠진 문제는 여기에 옮겨적으시다 그런 것 같고)

좋은 하루 되세요!

rriiaa의 이미지

괄호로 지적하신 부분은 옮기다가 그렇게 됬습니다 ㅠㅠ

예를 들어서

printf("%p", array);
printf("%x", &array[0][0]);

두개의 주소가 다르게 나오던데 그 이유가 무엇인가요?

raymundo의 이미지

어... 다르니까요. (?!)

포인터와 malloc() 조합을 써서 2차원 배열과 동일하게 array[i][j] 처럼 접근하여 쓸 수는 있지만 그래도 이것은 2차원 배열과는 다릅니다.

(출처: http://c-faq.com/aryptr/aryptr2.html)

a는 배열이고, 수식에서 a라고만 쓰면(뒤에 [2]와 같은 인덱스 없이) 그것은 a[0]을 가리키는 포인터"처럼" 처리됩니다. 즉 a는 &a[0] 과 값도 같고 타입도 같습니다.(이 경우는 a는 char의 배열이고, 수식에서 쓰인 a는 char * 타입의 포인터처럼 쓰이겠네요)

p는 포인터이고 p의 값은 'w'가 저장된 주소이며, p[0], p[1] 등과 같이 배열과 동일하게 접근할 수 있지만, p는 엄연히 별개의 저장공간을 차지하고 있습니다.

(출처: https://dojang.io/mod/page/view.php?id=307)

numArr은 2차원 배열이고,
numArr이 수식에 쓰이면 &numArr[0]처럼 처리되며, 타입은 int (*)[4]
numArr[0]은 1차원 배열이고,
numArr[0]이 수식에 쓰이면 &numArr[0][0]처럼 처리되며, 타입은 int *
하지만 배열의 시작주소 = 첫원소의 시작주소이기 때문에 결과적으로

&numArr                     // int (*)[3][4]
numArr    == &numArr[0]     // int (*)[4]
numArr[0] == &numArr[0][0]  // int *

위의 다섯 가지는 모두 동일한 값입니다. 타입은 제각각이죠.

(출처: https://codeng.tistory.com/8)

이게 본문과 동일한 케이스이고, arr 은 포인터입니다.
malloc으로 확보한 공간의 시작주소를 가리키고 있고,
그 공간을 쪼개어 1차원 배열처럼 쓰고 있는데 이 배열의 각 원소는 다시 malloc으로 확보한 공간을 가리키는 포인터이죠.

여기서는 그림에서 보다시피
&arr 과 arr의 값은 서로 다르고(포인터 변수의 주소와 값)
arr의 값은 arr[0]의 주소이며, 이것은 arr[0][0]의 주소와 같지 않고,
arr[0]의 값이 arr[0][0]의 주소입니다.

&arr                  // int ***
 !=
arr == &arr[0]        // int **
 !=
arr[0] == &arr[0][0]  // int *

좋은 하루 되세요!

rriiaa의 이미지

와우!!너무 친절한 답변 감사드립니다!!

댓글 달기

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