the point of pointers

Started by
4 comments, last by PennstateLion 18 years, 9 months ago
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
Advertisement
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.
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
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 valuereturn 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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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)
Thanks to all the replies, it is alot more clear..

now to start using them

This topic is closed to new replies.

Advertisement