programing

C에서 가변 주소를 인쇄하는 방법은 무엇입니까?

prostudy 2022. 6. 2. 21:29
반응형

C에서 가변 주소를 인쇄하는 방법은 무엇입니까?

이 코드를 실행하면요

#include <stdio.h>

void moo(int a, int *b);

int main()
{
    int x;
    int *y;

    x = 1;
    y = &x;

    printf("Address of x = %d, value of x = %d\n", &x, x);
    printf("Address of y = &d, value of y = %d, value of *y = %d\n", &y, y, *y);
    moo(9, y);
}

void moo(int a, int *b)
{
    printf("Address of a = %d, value of a = %d\n", &a, a);
    printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);
}

컴파일러에서 이 오류가 계속 발생합니다.

/Volumes/MY USB/C Programming/Practice/addresses.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c: In function ‘moo’:
/Volumes/MY USB/C Programming/Practice/addresses.c:23: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’

좀 도와 주시겠습니까?

고마워요.

부라그만

사용하고 싶다%p포인터를 인쇄합니다.사양부터:

p 인수는 에 대한 포인터여야 합니다.포인터의 값은 구현 정의 방식으로 일련의 인쇄 문자로 변환됩니다.

그리고 출연진도 잊지 마세요.

printf("%p\n",(void*)&a);

변수 또는 포인터의 메모리주소를 인쇄하는 경우는,%d주소 대신 번호를 인쇄하려고 해도 메모리 주소가 숫자가 아니기 때문에 컴파일 오류가 발생합니다.가치0xbfc0d878확실히 숫자가 아니라 주소입니다.

사용하는 것은%p.예.,

#include<stdio.h>

int main(void) {

    int a;
    a = 5;
    printf("The memory address of a is: %p\n", (void*) &a);
    return 0;
}

행운을 빕니다.

변수의 주소를 인쇄하려면%p포맷합니다. %d부호 있는 정수용입니다.예를 들어 다음과 같습니다.

#include<stdio.h>

void main(void)
{
  int a;

  printf("Address is %p:",&a);
}

%p: 포인터 인쇄를 사용하고 있는 것 같습니다.

온라인 컴파일러 https://www.onlinegdb.com/online_c++_compiler에서 시도했습니다.

int main()
{
    cout<<"Hello World";
    int x = 10;
    int *p = &x;
    printf("\nAddress of x is %p\n", &x); // 0x7ffc7df0ea54
    printf("Address of p is %p\n", p);    // 0x7ffc7df0ea54

    return 0;
}

언급URL : https://stackoverflow.com/questions/5286451/how-to-print-variable-addresses-in-c

반응형