c언어 포인터 공부하던중에....

sjg0120의 이미지

안녕하세요 초보가 공부하던중에 이해안가는 부분이 있어서 이렇게 질문을 올립니다
'다시체계적으로 배우는 c언어 포인터' 를 보고있는데 결과값이 좀 다르게나와서요..

#include

int main(void){

int imsi = 5;
int *imsip;
int temp;

imsip = &imsi;

temp = imsip;

printf("imsi = [%#010x] , imsip = [%#010x] , temp = [%#010x] \n", &imsi, imsip, temp);

printf("(int *)temp = [%d],*imsip = [%d]", *((int *)temp), *imsip);

return 0;
}

책에 나와있는대로라면 temp에 있는값이 정수로 인식이되기 때문에 비록 imsip와 출력값은 같을지라도 타입이 다르다고 하더군요
그래서 캐스트 연산자를 사용해서 imsip가 가리키는 값과 동일한 곳의 정수값을 취하려고 하는데
여기서 세그먼트 폴트 에러가 나네요 ㅜㅜ 책에있는대로라면 *imsip 와 같은 5가 출력되어야하는데...
무엇이 잘못된걸까요?? 선배님들의 조언 부탁드리겠습니다~

doldori의 이미지

int와 int* 사이의 변환은 캐스팅을 통해 가능하지만 그 결과는 환경에 따라 다릅니다.
책의 저자가 실행한 환경과 님의 환경이 다르다면 그럴 수 있습니다.
배경 설명 없이 대뜸 이런 예제를 실은 책이 문제라면 문제겠지요.
그리고 이런 예제는 포인터를 체계적으로 이해하는 데 별로 도움이 못됩니다. -_-;

sjg0120의 이미지

그런데 한가지 더 의문이 처음 printf문장에서 temp의 값이 imsip의 값과 동일하게
출력이 됐거든요? 물론 타입은 다르겠지만.. 그런데 2번째printf문장에서 temp를 int *로 형변환해서 결과값을
출력했을때 오류가 나는 이유가 뭘까요...ㅡㅡ;;

thyoo의 이미지

An integer may be converted to any pointer type. Except as previously specified, the
result is implementation-defined, might not be correctly aligned, might not point to an
entity of the referenced type, and might be a trap representation.

- The mapping functions for converting a pointer to an integer or an integer to a pointer are intended to
be consistent with the addressing structure of the execution environment.

doldori님 말씀대로 implementation-defined라
제대로 동작하라는 보장이 없읍니다.

// "Except as previously specified"
// --> null pointer만 보장된다는 얘깁니다.

간단한 예)
sizeof(int) == 4
sizeof(void*) == 8
___________________________________
Less is More (Robert Browning)

___________________________________
Less is More (Robert Browning)

lovewar의 이미지

다음과 같은 환경이라면 결과를 얻을 수 있을겁니다.
하지만, 사용하지 않는 것이 좋습니다.

sizeof(int) = 4
sizeof(long long) = 8
인 경우의 시스템에서

int temp;
대신
long long temp;

로 변경하면 같은 값을 얻을 수 있을겁니다.

댓글 달기

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