포인터와 sizeof() 관련해서 질문드립니다.

theageha의 이미지

첨부된 화면에 보시면, 실제 실행 코드에서 sizeof(imsi+0)의 대한 출력 값이 '4'인 것을 확인할 수 있습니다.

그런데 디버거에서 살펴보면 화면 오른쪽 아래 '24'로 출력이 되는데,

왜 이런 차이가 생기는 지 궁금합니다.

그리고 어떤게 맞는 건지도 궁금하네요;;;@_@;;

제 생각으로는

imsi는 전체 배열을 다 포함한 주소를 가리키므로 전체 사이즈인 '24'가 나오는 것은 알겠는데

imsi+0 도 똑같이 '24'가 나와야 하는게 아닌가 싶은데요;;

File attachments: 
첨부파일 크기
Image icon K-11.jpg161.47 KB
Fe.head의 이미지

sizeof( imsi ) 하면 24가 나올것이고 imsi[3][2] 크기
sizeof( *imsi) 하면 8이 나올것이고 imsi[2] 4*2 = 8

sizeof( imsi+0 )하면 imsi+숫자 하면 imsi가 포인터 연산이 되므로 sizeof(imsi+숫자)는 sizeof( 포인터 ) 결국 4

sizeof( *imsi[0] ) 하면 imsi[0]이 가르키는 값이므로 sizeof(int)
sizeof( (*imsi)[0] ) 하면 (*imsi) == imsi[][2] 에 0번째이므로 sizeof(int)가 될듯합니다.

int형으로 말고 char형으로 해보세요. -포인터 크기랑 햇갈리지 않겠죠..

#include <stdio.h>
 
int main()
{
    char    imsi[3][2];
    int i;
    /*
     *{{ 00, 01 },
     * { 10, 11 },
     * { 20, 21 } }
     * 배열로 채운다.
     */
    for( i = 0 ; i < 3 ; ++i ) {
        int j;
        for( j = 0 ; j < 2 ; ++j ) {
            imsi[i][j] = i*10 + j;
        }
    }
    printf("%#x %d %d\n" , imsi, imsi, sizeof( imsi ) );
    printf("%#x %d %d\n" , *imsi, *imsi, sizeof( *imsi ) );
    printf("%#x %d %d\n" , imsi+0, imsi+0, sizeof( imsi+0 ) );
    printf("%#x %d %d\n" , *imsi[0], *imsi[0], sizeof( *imsi[0] ) );
    printf("%#x %d %d\n" , (*imsi)[0],(*imsi)[0], sizeof( (*imsi)[0] ) );
    return 0;
}
 
 
결과
0xbf9554a2 -1080732510 6
0xbf9554a2 -1080732510 2
0xbf9554a2 -1080732510 4
0 0 1
0 0 1

-----------------------
과거를 알고 싶거든 오늘의 네 모습을 보아라. 그것이 과거의 너니라.
그리고 내일을 알고 싶으냐?
그러면 오늘의 너를 보아라. 그것이 바로 미래의 너니라.

고작 블로킹 하나, 고작 25점 중에 1점, 고작 부활동
"만약 그 순간이 온다면 그때가 네가 배구에 빠지는 순간이야"

댓글 달기

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