Pointers WTF?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Pointers WTF?

Post 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.
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

Re: Pointers WTF?

Post by cypher1554R »

Andree made a whole topic for those: http://elysianshadows.com/phpBB3/viewto ... &sk=t&sd=a
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointers WTF?

Post 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.

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Re: Pointers WTF?

Post by JaxDragon »

I think C++ officially hates me. I tried referencing pointers and stuff, and got loads of errors, *panics*
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointers WTF?

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Pointers WTF?

Post 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?
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Pointers WTF?

Post 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.
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Re: Pointers WTF?

Post by JaxDragon »

Lost source code, pointers work now =D. All is well in the world of Jax
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: Pointers WTF?

Post 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.
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
Post Reply