Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualslicksk8te

Posted 11 October 2012 - 10:42 AM

Ok, so this topic is one of the most confusing for new programmers and the confusion is not usually 'what' a pointer is but instead 'why' are they useful and 'how' are they used. Also, the whole value vs reference thing gets really tricky if you don't understand how functions are called.

Lets start with the 'what', just to insure we are on the same page. Pointers are a variable like anything else, except that instead of holding a value directly, it holds an address where that value is in memory. For example:
[source lang="cpp"]// Declare a variable xint x = 1;// Declare a pointer variable pxint *px;//Setting px to the address of xpx = &x; // & is the address of any variable// Now get the value that px points to and put it in yint y = *px; // * means get value at address stored in px// Now y has 1[/source]
Stay with me on this because it is important later. Note that px is just another variable that is 32-bits (or 64-bits on x64 systems). Now why does this matter? Because if you had a variable x that was was 2kb (for example) then you could avoid having to pass the whole variable around by using a pointer. Lets see a code example of this:

[source lang="cpp"]struct{ int x[500];} ReallyBigData;//The sizeof(ReallyBigData) is 2000 bytesstruct ReallyBigData x;// now if you pass it by valuefoo(x)//Then all of x is being passed to the function, yep all 2000 bytes// if you had thisReallyBigData* px = &x;//Then px is only 4 bytes!// So an alternate function that takes a pointer instead of the struct would bebar(px);// Now it only passes the pointer to the data, Only 4 bytes![/source]

This basicly means that if you have a variable that is large, then it is better to pass a pointer (pass by reference) rather than passing the entire value (pass by value).
This is why you always us char* for strings. Example of functions foo and bar are below:
[source lang="cpp"]void foo(ReallyBigStruct x){ if( x.x[0] == 1) { printf("It is one!\n"); }}void bar(ReallyBigStruct *px){ if( px->x[0] == 1) { printf("It is one!\n"); }}[/source]


The difference between '->' and '.' is that it differentiates the type of variable access your using. For example:
[source lang="cpp"]// Declare xReallyBigData x;// Fill the pointer with the address of xReallyBigData* px = &x;// Manipulate x directlyx.x[0] = 1;// Manipulate x indirectly through a reference (px)px->x[0] = 1;// Alternatively you could us px just like x by dereferencing it(*px).x[0] = 1; // remember (*px) gets the value that px is pointing to[/source]

#2slicksk8te

Posted 11 October 2012 - 10:29 AM

Ok, so this topic is one of the most confusing for new programmers and the confusion is not usually 'what' a pointer is but instead 'why' are they useful and 'how' are they used. Also, the whole value vs reference thing gets really tricky if you don't understand how functions are called.

Lets start with the 'what', just to insure we are on the same page. Pointers are a variable like anything else, except that instead of holding a value directly, it holds an address where that value is in memory. For example:
[source lang="cpp"]// Declare a variable xint x = 1;// Declare a pointer variable pxint *px;//Setting px to the address of xpx = &x; // & is the address of any variable// Now get the value that px points to and put it in yint y = *px; // * means get value at address stored in px// Now y has 1[/source]
Stay with me on this because it is important later. Note that px is just another variable that is 32-bits (or 64-bits on x64 systems). Now why does this matter? Because if you had a variable x that was was 2kb (for example) then you could avoid having to pass the whole variable around by using a pointer. Lets see a code example of this:

[source lang="cpp"]struct{ int x[500];} ReallyBigData;//The sizeof(ReallyBigData) is 2000 bytesstruct ReallyBigData x;// now if you pass it by valuefoo(x)//Then all of x is being passed to the function, yep all 2000 bytes// if you had thisReallyBigData* px = &x;//Then px is only 4 bytes!// So an alternate function that takes a pointer instead of the struct would bebar(px);// Now it only passes the pointer to the data[/source]
This basicly means that if you have a variable that is large, then it is better to pass a pointer (pass by reference) rather than passing the entire value (pass by value).

The difference between '->' and '.' is that it differentiates the type of variable access your using. For example:
[source lang="cpp"]// Declare xReallyBigData x;// Fill the pointer with the address of xReallyBigData* px = &x;// Manipulate x directlyx.x[0] = 1;// Manipulate x indirectly through a reference (px)px->x[0] = 1;// Alternatively you could us px just like x by dereferencing it(*px).x[0] = 1; // remember (*px) gets the value that px is pointing to[/source]

#1slicksk8te

Posted 11 October 2012 - 10:18 AM

Ok, so this topic is one of the most confusing for new programmers and the confusion is not usually 'what' a pointer is but instead 'why' are they useful and 'how' are they used. Also, the whole value vs reference thing gets really tricky if you don't understand how functions are called.

Lets start with the 'what', just to insure we are on the same page. Pointers are a variable like anything else, except that instead of holding a value directly, it holds an address where that value is in memory. For example:
[source lang="cpp"]// Declare a variable xint x = 1;// Declare a pointer variable pxint *px;//Setting px to the address of xpx = &x; // & is the address of any variable// Now get the value that px points to and put it in yint y = *px; // * means get value at address stored in px// Now y has 1[/source]
Stay with me on this because it is important later. Note that px is just another variable that is 32-bits (or 64-bits on x64 systems). Now why does this matter? Because if you had a variable x that was was 2kb (for example) then you could avoid having to pass the whole variable around by using a pointer. Lets see a code example of this:

PARTNERS