C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 22nd, 2002, 08:50 AM
Petskull's Avatar
Petskull Petskull is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 12 Petskull User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Petskull
Pointers 101

I worte this a while back to help me remember pointer stuff... thought it'd be nice to share with other newbies of my caliber..

Code:
#include <stdio.h>

/***************************************************************
*                Voodoo 'Doll' Petskull                        *
*               petskull@twistedport.com                       *
*              http://www.twistedport.com                      *
*--------------------------------------------------------------*
*--pointers101.c--                                             *
*-----------------                                             *
*     Shooting for my 'Expert Pointers Merit Badge'            *
*     Things to remember:                                      *
*     '*' means 'Dereference' (Given a pointer, get the        *
*                              thing referenced)               *
*     '&' means 'Address of'  (Given a thing, point to it)     *
*     ----------------------------------------------------     *
*     thing       | Simple thing (variable)                    *
*     &thing      | Pointer to variable 'thing'                *
*     thing_ptr   | Pointer to an integer (may or may not be   *
*                 |                        specific integer    *
*                 |                       'thing')             *
*     *thing_ptr  | Integer at the address that 'thing_ptr'    *
*                 | points to                                  *
*                                                              *
***************************************************************/

int thing;        /* define a thing */
int *thing_ptr;    /* define a pointer to a thing */

int main(){

 thing = 14;       /* the box 'thing' now contains the value '14' */
 printf("The value of 'thing' is: %d\n", thing);

 thing_ptr = &thing;     /* Assigns the address(&) of 'thing' to the pointer 'thing_ptr' */
 *thing_ptr = 8;        /* thing_ptr points to the memory allocated to the variable 'thing',*/
                 /* so- by changing the value in that memory to '8'- 'thing' now equals '8' */
 printf("The value of 'thing' is now: %d\n", thing);

 return(0);
}

Reply With Quote
  #2  
Old July 22nd, 2002, 12:02 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
i read that

void func(*thing)

and

void func(&thing)

are NOT the same even though they behave the same. can anyone explain the actual difference? is this a pre-processor thing or what?

tia...
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old July 22nd, 2002, 02:10 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 13
I think it's really just a matter of preference. Would you rather operate with thing being a pointer (hence requiring dereferencing, and -> operators), or treating it as just another variable? Both allow you to do the same thing (manipulate a variable with foreign scope), but each just differs the syntax of how you do it.

Personally, when I prototype a function, if it's meant to take an object, I use pointers, MyFunction(MyClass* myThing). If it's a simple variable, then I use references, MyFunction(primitive_type& myVal).

Reply With Quote
  #4  
Old July 22nd, 2002, 04:13 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 15 m 18 sec
Reputation Power: 4080
Personally, I prefer func_by_pointer(foo *p); over func_by_reference(foo &p); in most cases. My reason is that when calling the functions, the syntax is:
Code:
#include "myheader.h"
int main(void) {
  int param = 5;
  func_by_pointer(& param);
  func_by_reference(param);
  return 0;
}

It is immediately obvious from the syntax, that the first function is accepting the address to the param and may change the value of the param. The syntax for the second function does not indicate that this function may actually alter the value of param and you wouldn't know it unless you actually looked up the declaration of func_by_reference() in myheader.h. Hence I prefer using pointer notation for functions like this. The only times I ever use reference notation is for copy constructors and operator overloading.

Then again, this is a personal preference and I'd love to hear other views on the subject.

Reply With Quote
  #5  
Old July 23rd, 2002, 02:45 PM
Petskull's Avatar
Petskull Petskull is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 12 Petskull User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Petskull
ok, I'd just like to point out that I hate pointers....

Not what they do, just dealing with them is a bitch...

...hence the snippet...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Pointers 101

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap