Sign in to follow this  
PennstateLion

the point of pointers

Recommended Posts

so ive spent alot of time trying to get a grasp of pointers... I understand what they do and how to do it for the most part (dont quite understand pointes to functions etc..) anyway coming from a vb.net background I cant figure out when and why use pointers, what is the point?? what does it buy me? I HATE POINTERS! anyhelp would be appreciated

Share this post


Link to post
Share on other sites
Quote:
Original post by ematsui
so ive spent alot of time trying to get a grasp of pointers...
I understand what they do and how to do it for the most part (dont quite understand pointes to functions etc..)

anyway coming from a vb.net background I cant figure out when and why use pointers, what is the point??

what does it buy me?
I HATE POINTERS!

anyhelp would be appreciated
You need pointers when you need arrays but don't know how big the array will be.
int main(int argc, _TCHAR* argv[])
{
const int count = 100;
int array[count];

int dcount = 100;
int * darray = new int[dcount];
delete [] darray;
}


With static memory you cannot dynamicaly change the size of the array, because count must always be const. Avoid dynamically alocated memory if possible.

A while back i worked with some dudes who told me "dynamically allocated memory is better then static" - nonsense, you have to know when to use which.

Share this post


Link to post
Share on other sites
First, 2 concrete examples off the top of my head:

Reason #1:


SSomeBigStruct
{
int lotsOfData[ 100000 ];
float lotsMoreData[ 1000000 ];
};

void someFunctionBad( SSomeBigStruct s )
{
// Do something to s
}

void someFunctionGood( SSomeBigStruct *s )
{
// Do something to s
}


Reason #2:


int numEntries;
cout << "How many entries do you need?";
cin >> numEntries;

int *entries = new int[ numEntries ];


Now, some links for you to read, as they specifically ask your same question.

http://www.sparknotes.com/cs/pointers/whyusepointers/
http://www.codeproject.com/cpp/pointerprelude.asp
http://www.tek-tips.com/faqs.cfm?fid=2914

Share this post


Link to post
Share on other sites
You use VB? Using pointers is similar to passing by reference. In this case, it is the address of the actual variable, or whatever it is.



class BigStruct{
// imagine this class is 512 bytes
};


void function1(BigStruct* bs)
{
// do stuff
}

void function2(BigStruct bs)
{
// do stuff
}






In the first case, the pointer (which is 4 bytes) is passed, instead of having to allocate 512 more bytes of memory on the stack, like in the second case, and since it is a class, most likely call the object's constructor as well.

So, mostly its a space saver.


Function pointers

Simply a pointer, but to a function.


int func(char* s, long number)
{
return 3;
}

void func2(void)
{

}

int main()
{
/* This declares a pointer to a function. The name of the pointer is 'f'
and the function that it points to must take a char pointer and
a long, and also return an integer.
Note:: the brackets around "*f" are important!!
*/

int (*f)(char* s, long b);
f = func;

f = func2; // compile error (wrong return type and parameters)

f("string", 8); // call the function that 'f' points to

printf("%d\n", f("s", 6) ); // print the return value

return 0;
}




This can be used to implement a primitive form of polymorphism in C. This works, because as you hopefully should know, when a program is compiled, each machine instruction gets an address. The address that 'f' points to is the address of the function.

Share this post


Link to post
Share on other sites
The only way to learn to use stuff like pointers is to keep using them, eventually the specifics of their application will become more apparent, failing that learn java or C# and never have to worry about them (although they are still present but hidden and it helps allot to have a basic understanding of how they work)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this