LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2003, 01:47 AM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
pass by address between functions


Code:
void print (int* ip) {
   cout<<*ip<<endl;
} 
int main ()
{
   int i;
   print (&i);
}
I think the compiler will think int* ip=&i in this case.
Code:
void print (int* i) {
   cout<<*i<<endl;
} 
int main ()
{
   int i;
   print (&i);
}
I think the compiler will think int* i=&i in this case.
Then my trouble comes up:
We know in c++, the expression int* i=&i is evil. Howeve, The code still make sense, why?
thank you.

Last edited by Xiangbuilder; 10-10-2003 at 01:55 AM.
 
Old 10-10-2003, 04:47 AM   #2
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
The reason is that they are different i's and not the same.

They are in different scope and therefore do not do as you said.

int *i=&i; // would set the value of i to the address of the pointer i, probably not much use, ie its sets the pointer to the address of its self since they are the same variable!

But since the two i's your talking about are in different scope it does not do this.

Look at it this way..

int i; // lets say this gets addres 0x000300

print(&i); // passes 0x000300 to print

void print (int *i) // another variable called *i which gets address say 0x000302

now when you call print(&i) from main, the value 0x000302 is written to memory location 0x000300.

Then when you call cout << *i <<endl; it will print out the value which is stored at memory location 0x000300.

but when you say:

int *i = &i;

lets say *i gets the address 0x000700, then you it says, set the pointer to point to 0x000700, so now it points to its self, now your data type for i is a pointer to an int, but you have assigned it to point to a pointer (which is a pointer to an int, and not an int), so the types are probably not compatible.

Have I made any sense?

One more example to help clarify what I am saying.

int *i = &i; // This is similar to this:

int *k;
int *m;

k=&m; // bad...

k=m; // makes more sense because now they are both the same type...


Have I made sense yet? (I dont think i'm very good at explaining things : )


Cheers
 
Old 10-10-2003, 06:54 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
"I dont think i'm very good at explaining things"
My English and c++ skill are so poor, still can understand your words.
Thank you.
For your detailed clear information.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
how to pass argv[] & argc to functions? skie_knite007 Programming 2 05-13-2005 11:12 PM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
Command to pass the address of sys_call_table to a module appas Programming 3 07-23-2004 12:30 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration