The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Pointers 101
Discuss Pointers 101 in the C Programming forum on Dev Shed. Pointers 101 C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 22nd, 2002, 08:50 AM
|
 |
Member
|
|
Join Date: Jun 2002
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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);
}
|

July 22nd, 2002, 12:02 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
|
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...
|

July 22nd, 2002, 02:10 PM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
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).
|

July 22nd, 2002, 04:13 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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. 
|

July 23rd, 2002, 02:45 PM
|
 |
Member
|
|
Join Date: Jun 2002
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|