pointers as function arguments

This is a discussion on pointers as function arguments within the C++ Programming forums, part of the General Programming Boards category; I am using a pointer to a double as an argument for a function that prints out the memory address, ...

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    pointers as function arguments

    I am using a pointer to a double as an argument for a function that prints out the memory address, contents and name of the variable that the pointer points to, but I can't figure out how to output the name of the variable that the pointer points to.

    Here is the code:
    Code:
    #include <iostream>
     #include <string.h>
    
     void iaddress(double* num);
    
     int main()
     {
    	 double num1 = 1.23;
    	 double num2 = 100.75;
    	 char *str1 = new char[26];
    	 strcpy(str1, "Pointers are interesting!");
    	 double num3 = -35.5;
    	 double *ptr4 = new double;
    	 int i = 0;
    
    	 // str1 output
    	 cout << "str1 - main: address: "  << &str1 << " contents: " << str1 << endl;
    
    	//	num1 output and function call
    	 cout << "num1 - main: address: " << &num1 << " contents: " << num1 << endl;
    	 ptr4 = &num1;
    	 iaddress(ptr4);
    
    	//	num2 output and function call
    	cout << "num2 - main: address: " << &num2 << " contents: " << num2 << endl;
    	ptr4 = &num2;
    	iaddress(ptr4);
    
    	//	num3 output and function call
    	cout << "num3 - main: address: " << &num3 << " contents: " << num3 << endl;
    	ptr4 = &num3;
    	iaddress(ptr4);
    
    	//	ptr4 output and function call
    	cout << "ptr4 - main: address: " << ptr4 << " contents: " << *ptr4 << endl;
    	*ptr4 = 123.4;
    	iaddress(ptr4);
    
    
    	delete ptr4;
    	delete [] str1;
    
    
    
    	 return 0;
    
     }
    
     //	FUNCTION HEADER AND CODE
    
     void iaddress(double* num)
     {
    	cout << "	" << num << "- main: address: " << num << " contents: " << num << endl;
    	delete num;
     }

    Thanks for any help.

    Brian

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>but I can't figure out how to output the name of the variable that the pointer points to.

    Thats because you cant. And thats because there is no name to output. The pointer just points to a section of memory. The variable names in your code don't exist in the compiled product (at least, not in a form you would recognise). Its all just memory addresses. So your pointer can obtain the value of what it points to, and you can display the address of what it points to. You can even display the address of the pointer, but thats it. There's nothing else to see.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Interesting question. Answer: You can't - with some qualification.

    The compiler doesn't recognize your variable name, as such. It equates the "human" name you've given it with an address. Part of the compilation process.

    'int1' may be '0063FDE8' (stole that from your output ). As "low" as C/C++ may be, or seem to be, it's still high-level in machine-language terms.

    An incredibly coarse explanation? At compile time, a #define directive, of sorts, is initiated. Something along the lines of:

    #define int1 *0063FDE8

    (Smart people, go flame someone else! )

    From there on, whenever 'int1' is encountered, the contents of memory location '0063FDE8' are accessed. In effect, 'int1' becomes '0063FDE8'.

    -Skipper

    P.S. Thanks, lightatdawn, for typing faster than me!
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  2. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-17-2005, 12:55 AM
  3. Pointers as function arguments
    By VanJay011379 in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 09:51 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Replies: 2
    Last Post: 03-07-2002, 10:14 AM

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