Error » Certification & Programming center Error !! » Programming tutorials » C++: Pointers, Pass by Value, Pass by Reference

Programming tutorials All Knowledge Info and links to posted here

Post New Thread Reply
  C++: Pointers, Pass by Value, Pass by Reference
LinkBack Thread Tools Display Modes
Old 03-Dec-2006, 11:26 PM   #1 (permalink)
Administrator
 
Admin's Avatar

Posts: 876
Join Date: Oct 2005
Rep Power: 10 Admin has disabled reputation

IM:
Default C++: Pointers, Pass by Value, Pass by Reference

You just can't properly explain pointers, without first knowing the basics of passing by value/reference.

I'm sure the odd person who reads this will say "but you can also pass a pointer" while this is all well and true, the pointer passed is still a primitive variable and will in turn either be passed by reference or by value.

When passing a value, in eithe case, the call will look the same, it's the recieving method which determines how the pass is made.

By Value:


Code:
void aMethod(aParameter p) { }
When passing by value a temporary COPY is made in memory taking up space and making any changes to the variable useless outside of the aMethod scope. Once control returns to the calling procedure any changes to the variable will not be recognized, and you will still have the original variable.

By Reference:


void aMethod(aParameter& p) { }


NOTE: the & can be place either against the parameter type, against the parameter name, or there can be a space on either side.

This essentially passes a pointer, but not the same kind of pointer you would get when creating a:
Variable* v;
That is why it is called by reference, since that is what you have, p now acts as if it were the parameter just like in the by value way, but any changes affect the original and stay changed outside of the aMethod scope.

*Now if that parameter was a structure, class, or template, etc. It will have fields that are accesable by reference . or pointers ->

In both cases of the above passing, the parameter passed can be accessed using the . or dot operator.
Examples:
*assuming fields within the class type.
Person person; //person is an object of class Person
person.name
person.Chlid.name

*It can also be used to call methods within the type.
person.getFriends() //which would likely return a vector or array

If the methods above were constructors or other member functions, accessed in a fasion like the method call just above, then there is a this operator for accessing the object which called it. Who's fields can also be accessed, but only by pointers.

Since the object is passed implicitly it is passed as a pointer reference by default.
Examples:
-this now represents a pointer to a Person object
this->name
person->Child.name //if child can access it's name by reference it stays that way

Once again this can be done with methods.
this->getFriends()


Other examples requiring pointers:
int* i = new int(0); //this defines a dynamic int in memory pointed to by i
cout<<*i; would print the value that i points to

The * operator in both cases are NOT the same!!
-the first is a pointer reference, belonging to the type, in this case int.
-the second is a de-referencing operator belonging to the variable which resolves it into the variable it is point to.


Keep in mind that pointers are limitless:
int**** i = new int(1);
cout<<****i;

All that is needed is to dereference as many times as is originally referenced... but you're probably thinking, "why would anyone EVER do that?"
Quick answer: arrays!!

Arrays are complicated and cumbersome to code, if there is a faster and easier way shouldn't we do it? The answer is YES!!
By defining arrays by pointer, they are dynamic, and can be created at run time, not based on static values, they can also be traversed in the same way thanks to the C++ ingenuity of:
sizeOf and overloading the [] operator.
I will not discuss why or how this works, maybe when i write the [] overloading function, just know that it does.

Examples:
int* i = new int[3]; //defines a pointer to an array of 3 ints
cout<<i[0]; //returns the value at i[0]

It may seem strange, but it is a little complex for this recipe, and involves the stack, bytes, and other pointers.

This again is limitless:
int** i = new int*[3]; //define a pointer to an array of pointers to ints
cout<<*i[0]; //now we must dereference the final referencing that is not array specific.

Play around with pointers, they are the best part of C and C++, even though they can cause the most headaches, when a * is missplaced. It opens up a whole new range of storage and arraging possiblities.
Admin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
King of Dragon Pass SEO Game Cheats 0 01-Apr-2008 12:11 AM
Wii looks to pass Xbox 360 sales soon Anilrgowda wii news 0 14-Aug-2007 12:21 AM
Flash- Pass URL variable into flash movie. Sangeetha Knowledge Base 1 27-Feb-2007 03:53 AM
Pointers Ow Can They Work kingaff Programming tutorials 0 23-Feb-2007 08:07 AM
Pass Variables to a New Thread in C# Anilrgowda Programming tutorials 0 15-Dec-2006 01:21 AM


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

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228