NSBCoding
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Crash course to pointers

2 posters

NSBCoding :: Coding :: C++

Go down

Crash course to pointers Empty Crash course to pointers

Post  Admin Wed Jun 08, 2011 12:43 pm

Pointers are cool.

Pointers are 4 byte integers that hold an address in memory. You declare a pointer like thus:

Code:
int* pointerToSomething; // an integer pointer

Pointers all have a type. If you tried to assign a int pointer to the address where a, say, char type variable lives, then the program will explode in your face.

Code:
int a = 42;
int* p = &a; // in english, this means: "set the pointer p to point to the address of a (& means address of).
cout << *p << endl; // this means: "get the value (not address) at where p points to" (42)
cout << p << endl; // this means: "get the address of where p points to" (can be anything really, something like 0x0314014FF
(*p)++; // this means, increment the value of where p points to. (43)
p++; // this means to add 1 to the ADDRESS of where p points to.

cout << *p << endl; // can be anything now. it prints out whatever is next to where "a" lives.

Pointers are cool. You can do cool stuff like:
Code:
int a = 42;
int *p = &a;
int *q = p; // sets q to point to where p points (the address of a).
int array[100];
array[0] = 5;
array[1] = 10;
array[2] = 12;

p = array; // sets q to point to where the first element in "array" lives in memory.
p++; // p now points to the second element. (brownie point for anyone who can answer "why?")
p++; // again, p now points to the third delement
cout << *p << endl; // 12

Admin
Admin

Posts : 21
Join date : 2011-06-04

https://nsbcoder.board-directory.net

Back to top Go down

Crash course to pointers Empty Re: Crash course to pointers

Post  Elusive Wed Jun 08, 2011 1:04 pm

Admin wrote:
Pointers are 4 byte integers that hold an address in memory. You declare a pointer like thus:

MSDN wrote:
An int and a long are 32-bit values on 64-bit Windows operating systems. For programs that you plan to compile for 64-bit platforms, you should be careful not to assign pointers to 32-bit variables. Pointers are 64-bit on 64-bit platforms, and you will truncate the pointer value if you assign it to a 32-bit variable.

This is why some programs won't run (or are very unstable, i.e. crash a lot) on a 64-bit machine. Don't do this. Please.

Also, if you change the address of a pointer you can change it to an invalid location (crash if you try to access that location). Try not to do that.

It is also possible to accidentally access some other program's section of memory. This is why pointers are considered somewhat dangerous, and the OS will really hate you. It might even give you a nice BSoD if you do something funny.

A null pointer is conceptually different from an uninitialized pointer. A null pointer is known not to point to any object; an uninitialized pointer might point anywhere.
When you have finished using a pointer, always set it to null (p = NULL;). Just in case.

Elusive
Newbie

Posts : 10
Join date : 2011-06-04

Back to top Go down

Back to top

- Similar topics

NSBCoding :: Coding :: C++

 
Permissions in this forum:
You cannot reply to topics in this forum