C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-24-2009, 09:04 PM   #1
Registered User
 
Join Date: Jun 2009
Posts: 2
Exclamation Using Pointers in a function

I am learning C, and am am trying to create my own "swap" function for practice that takes in two numbers and puts them in two different variables. I then set one pointer to each. This is where I get stuck.

What should I pass into the function parameters: by this I mean do I pass say, *num1, or the address it points to, num1. I am confused. Please provide code examples.


Thanks
greeneyehawk is offline   Reply With Quote
Old 06-24-2009, 09:10 PM   #2
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
Yes, the address.

Code:
void swap( int* a, int* b );

int main( void )
{
	int 
		i, 
		j;
	swap( &i, &j );
	int
		* p = &i, 
		* q = &j;
	swap( p, q );	
}
Sebastiani is offline   Reply With Quote
Old 06-24-2009, 09:10 PM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by greeneyehawk View Post
Please provide code examples.
It will probably be easier if you provide code examples. But I think Sebastiani might have nailed this one.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 06-24-2009, 09:11 PM   #4
Registered User
 
ITAmember's Avatar
 
Join Date: Mar 2009
Location: Nebraska
Posts: 72
I'm assuming you know how memory addresses work. I'm also assuming other people will post code for you. This is just how I learned how pointer operators work.

* gets the value at an address
& gets the address of a value

That's it. They really aren't that complicated once you understand them
__________________
Super amazing game!
ITAmember is offline   Reply With Quote
Old 06-24-2009, 09:40 PM   #5
Registered User
 
Join Date: Jun 2009
Posts: 2
Smile Thanks!

Thank you so much, I got it.
greeneyehawk is offline   Reply With Quote
Reply

Tags
function, pointers, swap

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers InvariantLoop C Programming 13 02-04-2005 09:32 AM
Problem with function pointers vNvNation C++ Programming 4 06-13-2004 06:49 AM
Staticly Bound Member Function Pointers Polymorphic OOP C++ Programming 29 11-28-2002 01:18 PM
Contest Results - May 27, 2002 ygfperson A Brief History of Cprogramming.com 18 06-18-2002 01:27 PM
function pointers and member functions thor C++ Programming 5 03-19-2002 04:22 PM


All times are GMT -6. The time now is 10:44 PM.


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