Page 1 of 1

Pointers WTF?

Posted: Fri Nov 07, 2008 8:58 pm
by JaxDragon
Can someone PLEASE explain to me what pointers are and WHY you need them? It doesn't make sense! They just make things more complex. But I understand they are used in SDL quite often.

Re: Pointers WTF?

Posted: Fri Nov 07, 2008 9:13 pm
by cypher1554R
Andree made a whole topic for those: http://elysianshadows.com/phpBB3/viewto ... &sk=t&sd=a

Re: Pointers WTF?

Posted: Fri Nov 07, 2008 9:19 pm
by avansc
JaxDragon wrote:Can someone PLEASE explain to me what pointers are and WHY you need them? It doesn't make sense! They just make things more complex. But I understand they are used in SDL quite often.
i have a thread about pointers that i update regularly.

here is the short version what pointers are and why you need them.

a pointer is just a data type that points to some address in memory, dereferencing it, gets you the value of what ever is at that address.
this is powerful because you can get to places where you usually cant. pointers are also more efficient. they are scalable, and you are in full control of them.
they are also dangerous if you dont know what you are doing. you need to at least understand pointers if you want to do more advanced computing that requires use of datastructures such as trees.

/* a copy of the int n is changed */
void not_alter(int n) {
n = 360;
}

/* the actual variable passed (by address) is changed */
void alter(int *n) {
*n = 120;
}

you can see how this can be useful right..... i hope.


on some hardware if you know the addresses you can directly manipulate memory on that hardware.

pointers alow for elegant code.

Code: Select all

struct element {
	struct element *next;
	int value;
};
 
struct element *head = NULL;
 
void insert(struct element *item) {
	struct element **p;
	for(p = &head; *p != NULL; p = &(*p)->next) {
		if(item->value <= (*p)->value) {
			break;
		}
	}
	item->next = *p;
	*p = item;
}


off wiki//

Pointers are directly supported without restrictions in languages such as C, C++, Pascal and most assembly languages. They are primarily used for constructing references, which in turn are fundamental to constructing nearly all data structures, as well as in passing data between different parts of a program.

In functional programming languages that rely heavily on lists, pointers and references are managed abstractly by the language using internal constructs like cons.

When dealing with arrays, the critical lookup operation typically involves a stage called address calculation which involves constructing a pointer to the desired data element in the array. In other data structures, such as linked lists, pointers are used as references to explicitly tie one piece of the structure to another.

Pointers are used to pass parameters by reference. This is useful if we want a function's modifications to a parameter to be visible to the function's caller. This is also useful for returning multiple values from a function.


Re: Pointers WTF?

Posted: Fri Nov 07, 2008 9:20 pm
by JaxDragon
I think C++ officially hates me. I tried referencing pointers and stuff, and got loads of errors, *panics*

Re: Pointers WTF?

Posted: Fri Nov 07, 2008 9:29 pm
by avansc
JaxDragon wrote:I think C++ officially hates me. I tried referencing pointers and stuff, and got loads of errors, *panics*
thats gonna happen, that still happens to me. post your code and i'll let you know what you are doing wrong.

Re: Pointers WTF?

Posted: Sat Nov 08, 2008 3:07 pm
by programmerinprogress
JaxDragon wrote:I think C++ officially hates me. I tried referencing pointers and stuff, and got loads of errors, *panics*
Pointers are a confusing subject, they certainly make C++ much more difficult, by god don't they make so many more things possible?

With pointers,you can allocate dynamic memory!, which in turn allows you to make dynamic data structures like linked lists or dynamic arrays!, which is great if you don't know how many elements you need before you run the program.

I'm currently messing around with pointers, trying to understand them, and using reference texts to enforce my understanding.

PS: C++ doesn't hate you, you just have to be patient, the compiler is there to help you, just imagine if the thing compiled and you didn't get told about compile-time errors?

Re: Pointers WTF?

Posted: Sat Nov 08, 2008 11:31 pm
by Falco Girgis
JaxDragon wrote:I think C++ officially hates me. I tried referencing pointers and stuff, and got loads of errors, *panics*
Post code segments and we can explain what you are doing wrong.

Re: Pointers WTF?

Posted: Wed Nov 26, 2008 7:44 pm
by JaxDragon
Lost source code, pointers work now =D. All is well in the world of Jax

Re: Pointers WTF?

Posted: Wed Nov 26, 2008 8:24 pm
by Arce
I think there's a point in everybody's life when the concept of pointers just kinda 'clicks'. At least, hopefully. And yeah, if you run into anymore trouble, just post the code and I'd be glad to help ya out.

edit:
I think there's a point in everybody's life when the concept of pointers just kinda 'clicks'.
Har, pun now intended.