![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 6
| 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;
}
|
| sasuke_0214 is offline | |
| | #2 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| You know how to print an integer variable: Code: printf("%d", yourInteger);
Code: printf("%p, &yourInteger);
|
| Adak is offline | |
| | #3 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| A few things to note:
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:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Last edited by laserlight; 05-08-2009 at 12:42 AM. | |
| laserlight is online now | |
| | #4 |
| Registered User Join Date: Jun 2005
Posts: 1,343
| As a matter of fact, it will simply fail to compile.
__________________ Right 98% of the time, and don't care about the other 3%. |
| grumpy is offline | |
| | #5 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| |
| Adak is offline | |
| | #6 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Quote:
Code: printf("%p, &yourInteger);//without ending quotes
And yes the code you wrote above works without any error and warning. Although casting is not used.
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition | |
| BEN10 is offline | |
| | #7 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
Quote:
Code: test.c: In function `main': test.c:6: warning: void format, different type arg (arg 2)
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
| | #8 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| Ah! The missing double quotation char, strikes again! << Just seeing if Grumpy was awake >> j/kWhy 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. |
| Adak is offline | |
| | #9 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
Quote:
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 Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
![]() |
| Tags |
| address, c/c++, pointers, variables |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Accessing variables with memory address | ITAmember | C Programming | 54 | 06-28-2009 03:35 AM |
| Returning the Address of a Local Variable (Array) | Jesdisciple | C Programming | 9 | 08-20-2008 02:03 AM |
| address of struct variable? | DavidDobson | C Programming | 2 | 06-09-2008 03:01 AM |
| pointers | InvariantLoop | C Programming | 13 | 02-04-2005 09:32 AM |
| Variable Allocation in a simple operating system | awkeller | C Programming | 1 | 12-08-2001 02:26 PM |