void * 를 통해 값 받기 ?

kiss의 이미지

void test(int * k)
{
int s;
s = 3;
* k = s;
}

void test3(void * pp)
{
void * tt;

tt = (void *)0x33;

pp = (void *)tt;

}

int main()
{
int i = 2;
void * xx;

xx = (void *)0x22;

printf("i = %d\n", i);
test(&i);

printf("i = %d\n", i);

printf("xx = 0x%08x\n",xx );

test3(&xx);

printf("xx = 0x%08x\n",xx );

return 1;
}

i = 2
i = 3
xx = 0x00000022
xx = 0x00000022

위에 소스에서 i 값은 잘 바뀌는데 xx 값은 왜 안바뀔까요 ?

생각에는 test3 함수에서 * pp = (void *)tt; 해야 할 것 같은데

이렇게 하면 에러가 납니다.

답변 좀 부탁드립니다.

exsider의 이미지

void test3(void * pp)
{
void * tt;

tt = (void *)0x33;

pp = (void *)tt;

}

void test3(void ** pp)
{
void * tt;

tt = (void *)0x33;

*pp = (void *)tt;

}

이런 식으로 바꿔야 할 겁니다.

그리고 main에서 test3을 호출하는 부분도

void ** xy = &xx ;

test3(&xy);

이렇게 바꿔야 합니다.

제가 실력이 허접한 관계로 틀렸을수도 있습니다.

yui의 이미지

kiss wrote:

void test(int * k)
{
    int s;
    s = 3;
    * k = s;
}

 void test3(void * pp)
{
        void * tt;
        tt = (void *)0x33;
        pp = (void *)tt;
}

int형으로 선언한 i를 바꾸기 위해

void test(int * k)

를 사용하신 것처럼, (int -> int*)

void* 형으로 선언한 xx를 위해서는

 void test3(void** pp)

로 하시는게 대칭이라 이쁘지 않을까요? (void* -> void**)

마지막으로 함수의 내용에서도 대칭을 유지 하기 위해

Quote:

생각에는 test3 함수에서 * pp = (void *)tt; 해야 할 것 같은데

말씀하신 것처럼 고쳐주세요.
그럼 의도한 대로 동작할 겁니다.

@ 포인터는 %p로 찍어주세요. :)

ssehoony의 이미지

void test3(void ** pp)
{
void * tt;

tt = (void *)0x33;

*pp = (void *)tt;

}

이렇게 하는거 아닌가요?
넘겨 받는 타입이 void * 의 포인터 이기 때문에 void *의 포인터
즉 void ** 이거로 받아야 할겁니다.

exsider의 이미지

exsider wrote:

그리고 main에서 test3을 호출하는 부분도

void ** xy = &xx ;

test3(&xy);

이렇게 바꿔야 합니다.

윗 부분은 제가 잘못 생각한 것 같습니다.
원래대로

test3(&xx);

하는게 맞습니다.

댓글 달기

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