Thread: help me

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Cool help me

    i am computer newbie, i use linux for my work, i wanna learn c programming language, but i have warning with compailing on linux, please tell me about pointer variable on linux,
    example code

    Code:
    #include <stdio.h>
    
    void rischan(int a, int * b);
    
    int main(void) {
      int x;
      int *y;
    
      x=2;
      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);
      rischan(6,y);
    }
    
    void rischan(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);
    }
    the warning like this

    Code:
    c.c: In function ‘main’:
    c.c:11: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    c.c:12: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
    c.c:12: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
    c.c: In function ‘rischan’:
    c.c:17: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    c.c:18: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
    c.c:18: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’

    please tell me...why...?..thanks, and iam sorry my english is poor...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try:
    Code:
    printf("Address of x = %p, value of x = %p\n", (void *)&x, (void *)x);

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program is telling printf() to print the value of the pointer (which is an address), as an int. Of course, addresses use the %p format, not %d, because addresses are not int's.

    Welcome to the forum, indocoding.com!

    Your English is perfectly understandable.

Popular pages Recent additions subscribe to a feed