C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-08-2009, 12:26 AM   #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...
sasuke_0214 is offline   Reply With Quote
Old 05-08-2009, 12:32 AM   #2
Registered User
 
Join Date: Sep 2006
Posts: 2,512
You know how to print an integer variable:
Code:
printf("%d", yourInteger);
What does this do?
Code:
printf("%p, &yourInteger);
Adak is offline   Reply With Quote
Old 05-08-2009, 12:37 AM   #3
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
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.
__________________
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   Reply With Quote
Old 05-08-2009, 02:29 AM   #4
Registered User
 
Join Date: Jun 2005
Posts: 1,343
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%.
grumpy is offline   Reply With Quote
Old 05-08-2009, 03:05 AM   #5
Registered User
 
Join Date: Sep 2006
Posts: 2,512
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;
}
Adak is offline   Reply With Quote
Old 05-08-2009, 03:10 AM   #6
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
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.......

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   Reply With Quote
Old 05-08-2009, 09:37 AM   #7
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
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
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   Reply With Quote
Old 05-08-2009, 01:07 PM   #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/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.
Adak is offline   Reply With Quote
Old 05-08-2009, 01:09 PM   #9
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
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
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   Reply With Quote
Reply

Tags
address, c/c++, pointers, variables

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:16 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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