urgent! variable address

This is a discussion on urgent! variable address within the C Programming forums, part of the General Programming Boards category; my teacher gave out an assignment pertaining to variable address. she wants us to add something to the program to ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Unhappy urgent! variable address

    my teacher gave out an assignment pertaining to variable address. she wants us to add something to the program to display the address of the variables a, b, and c. and i can't seem to figure out how to do it and i'm on a tight schedule and i need it before May 11,2009. here is the program...

    Code:
    #include<stdio.h>
    
    void main(){
    int a,b,c,i;
    a=1;b=2;c=3;
    
    trace(&a,&b,c);printf("%d %d %d\n",a,b,c);
    trace(&a,&b,c);printf("%d %d %d\n",a,b,c);
    trace(&c,&b,a);printf("%d %d %d\n",a,b,c);
    
    for(i=1;i<=3,i++)
        trace(&a,&b,c);
    printf("%d %d %d\n",a,b,c);
    getch();}
    
    
    void trace(int *x, int *y, int z){
    int t;
    
    z+=*x;
    *x=*y+z;
    t=z;
    z=*y;
    *y=*x;
    *x=t;
    }
    pls help me...

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You know how to print an integer variable:
    Code:
    printf("%d", yourInteger);
    What does this do?
    Code:
    printf("%p, &yourInteger);

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,778
    A few things to note:
    • Your schedule is your problem. Avoiding telling the world that you urgently need an answer by a given date, especially since the more malicious among us would only give you an answer after the stated date, whereas if they did not know that it was urgent, they would urgently reply.
    • The main function should be declared with a return type of int, not void. Correspondingly, you should return 0 at the end of the main function, but this is optional with respect to the 1999 edition of the C standard.
    • You should indent your code properly and consistently. Along the same lines, it is usually bad practice to place multiple statements on the same line.
    • You should declare (i.e., provide a function prototype for) the trace function before calling it, as you do in the main function.
    • You should not use getch since it is non-standard and unnecessary.


    Now, on to your problem: you are looking to use the %p format specifier for printf. This format specifier is for printing a pointer to void, so all you need to do is to use it as you use the %d format specifier, except that now you would cast the address of the desired variable to void*.

    EDIT:
    Quote Originally Posted by Adak
    What does this do?
    It results in undefined behaviour.
    Last edited by laserlight; 05-08-2009 at 12:42 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,313
    Quote Originally Posted by Adak View Post
    What does this do?
    Code:
    printf("%p, &yourInteger);
    Quote Originally Posted by laserlight View Post
    It results in undefined behaviour.
    As a matter of fact, it will simply fail to compile.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by grumpy View Post
    As a matter of fact, it will simply fail to compile.
    As a matter of fact it compiles without errors or warnings, and runs just fine!

    Code:
    #include <stdio.h>
    
    int main(void) {
      int i = 0;
    
      printf("%p", &i);
    
      i = getchar();
      return 0;
    }

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Adak View Post
    As a matter of fact it compiles without errors or warnings, and runs just fine!

    Code:
    #include <stdio.h>
    
    int main(void) {
      int i = 0;
    
      printf("%p", &i);
    
      i = getchar();
      return 0;
    }
    I think grumpy was talking about
    Code:
    printf("%p, &yourInteger);//without ending quotes
    It sounds to me as a typo but actually you wrote it in your previous post.
    And yes the code you wrote above works without any error and warning. Although casting is not used.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,778
    Quote Originally Posted by Adak
    As a matter of fact it compiles without errors or warnings, and runs just fine!
    Quote Originally Posted by BEN10
    And yes the code you wrote above works without any error and warning. Although casting is not used.
    According to the MinGW port of gcc 3.4.5:
    Code:
    test.c: In function `main':
    test.c:6: warning: void format, different type arg (arg 2)
    You should increase your warning levels and/or test with a range of compilers. Yet, a clean compile at the highest warning level does not indicate that undefined behaviour is not involved.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ah! The missing double quotation char, strikes again! << Just seeing if Grumpy was awake >> j/k

    Why would I want or need to cast the address of the variable, before printing it?

    Laserlight, what do you need to code up on your compiler, to print out the address of a variable, which has no warnings or errors?

    My compiler's warnings are turned all the way up, already. I never turn them down.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,778
    Quote Originally Posted by Adak
    Why would I want or need to cast the address of the variable, before printing it?
    You need to cast because %p is used to print a pointer to void, but you want to print a pointer to int. Okay, but you might not need to cast, but then you would be relying on undefined behaviour.

    Quote Originally Posted by Adak
    Laserlight, what you you need to code up on your compiler, to print out the address of a variable, which has no warnings or errors.
    What I described in post #3: a cast to void*.
    Code:
    #include <stdio.h>
    
    int main(void) {
      int i = 0;
    
      printf("%p", (void*)&i);
    
      i = getchar();
      return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing variables with memory address
    By ITAmember in forum C Programming
    Replies: 54
    Last Post: 06-28-2009, 03:35 AM
  2. Returning the Address of a Local Variable (Array)
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 08-20-2008, 02:03 AM
  3. address of struct variable?
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 06-09-2008, 03:01 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21