Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-24-2004, 11:28 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
pointer question

total newb example, but i was wondering about pointers ..

please read the comments:
Code:
void swap(int *a1, int *a2)
{
  // is this next line right?
  // it seems like i should use *a1>*a2
  if(a1 > a2)
  {
    int temp;
    temp = *a1;
    *a1 = *a2;
    *a2 = temp;
  }
}
__________________
Mike
sde is offline   Reply With Quote
Old 05-25-2004, 03:13 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Your comment is correct. Use *.
But you need to pass arguments like:
Code:
int a, b;
	a = 8;
	b = 5;
	swap(&a, &b);
You could do yourself a favor and implement this, wich is much neater:
Code:
void swap(int &a1, int &a2)
{
	if(a1 > a2)
	{
		int temp;
		temp = a1;
		a1 = a2;
		a2 = temp;
	}
}


int main(int argc, char* argv[])
{

	int a, b;
	a = 8;
	b = 5;
	swap(a, b);
	cout<<"a: "<<a<<" b: "<<b<<endl;
	return 0;
}
You could do yourself even another favor: not calling your functions "swap".
Why? Because swap is already defined in the namespace std;
Check this complete code out:
Code:
#include<iostream>

using namespace std;

int main(int argc, char* argv[])
{
	int a, b;
	a = 8;
	b = 5;
	swap(a, b);
	cout<<"a: "<<a<<" b: "<<b<<endl;
	return 0;
}
Better rename your functions to something everyone understands in the given situation in general.
__________________
Valmont is offline   Reply With Quote
Old 05-25-2004, 06:44 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
ok, thanks.

i just picked up a book i've had and was going through it and that line didn't make sense.

i just want to verify ... that line should be ..

if( *a1 > *a2 )

right?
__________________
Mike
sde is offline   Reply With Quote
Old 05-25-2004, 01:06 PM   #4 (permalink)
Amaranthine
Registered User
 
Amaranthine's Avatar
 
Join Date: May 2004
Posts: 47
Amaranthine is on a distinguished road
couldn't you use the assembly xchg instruction for that?
Amaranthine is offline   Reply With Quote
Old 05-25-2004, 01:43 PM   #5 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Quote:
i just want to verify ... that line should be ..
right.

Code:
void swap(int &a1, int &a2)
this doesn't work in c. you didn't specify whether you wanted c or c++. anyway, this is less flexible (since you actually can't use it for type "int *" variables, only type "int").

Quote:
couldn't you use the assembly xchg instruction for that?
no.
joe_bruin is offline   Reply With Quote
Old 05-25-2004, 01:47 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
yes, it was a c++ example

so i passed 2 pointers into the function.

i compare the value of a1 to the value of a2 without the *

yet, when i assign a new value to a1, i need to use the * before it.

i guess not everything has to make sense. =)

thanks joe
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DB Design Question Part II sde Program Design and Methods 10 09-02-2008 11:37 AM
Question marks and apostrophes Belisarius HTML, XML, Javascript, AJAX 4 05-28-2004 04:11 PM
pointer to function with class? Kportertx Standard C, C++ 5 04-11-2003 05:12 AM
Nagging question jeffro Lounge 1 03-08-2003 06:46 PM


All times are GMT -8. The time now is 09:28 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting