SRL Archive - C/C++ Help and Tutorials

Pointers?


mat_de_b

2007-10-31 11:46:36

I get what they do, if im not mistaken they point to the adress at which data is stored, im just wondering what they are useful for? we dont use them in scar? and so im interested in their uses….

Mat

Yakman

2007-10-31 14:52:02

they do point to the address of a variable, and they are useful for a few reasons, heres some

its much easier to copy the referance than the whole variable, so when you call a method with a string, its faster to pass a pointer-to-string than the whole string, that way you copy only a 32-bit number, instead of a whole array of characters

also, you can add and subtract pointers, so its a bit better than accessing arrays and places in strings, heres an example
(btw, i dont know c++ syntax too well.. hopefully it wont show)



int myArray[10];
int* pointerToArray = &myArray;
for(int f = 0; f < 10; f++) {
cout << "index = " << f << " is " << *pointerToArray[0] << endl;
pointerToArray++;
}

its much faster to increment a pointer then go along in the array

R0b0t1

2007-10-31 18:59:52

Pointers are like… Having WT-Fakawi teaching you SCAR. Yes, they are that awesome.

mat_de_b

2007-11-01 07:45:02

From: R0b0t1
Pointers are like… Having WT-Fakawi teaching you SCAR. Yes, they are that awesome.


Useful :p

Thanks yakman i see what your getting at with speed, guess itll all fall into place soon :D thanks

Paul23

2007-11-04 16:12:04

Sorry to be a pain but this the way this has been expressed is potentially confusing.

A pointer contains an address. This is an address in memory. What is contained at this address could be anything. It could be an integer (or even another address).

They do NOT point to the address of a variable. They contain an address. When defined as a pointer the compiler will know to treat them as a pointer and know that what they contain is an address and reference the address.

If a pointer pointed to the address of a variable it would be a pointer to a pointer. (This is possible and is often used but is not what a pointer is). It contains the address - it doesn't point to it. A pointer simply contains an address.

They are not used in SCAR and SRL because these languages abstract that level of detail away.

If you wish to know more then I would suggest that you type 'c programming' into Google (or whatever) and see what you can find.

To start you off try this

http://www.cs.cf.ac.uk/Dave/C/node10.html#SECTION001000000000000000000

btw - hope this is helpful and not confusing (and hope even more that it is correct). Apologies if I have failed in any of these aims

Paul

R0b0t1

2007-11-04 20:04:10

Might as well explain offsets while your at it, eh?

Paul23

2007-11-05 04:18:16

Robot,

quite happy to explain offsets. But just to be sure I'm answering the correct question, can you explain a little further, maybe with an example what exactly you wish to know.

Paul

mat_de_b

2007-11-05 16:21:11

I knew that :D just didnt explain it properly thanks anyway

Paul23

2007-11-05 16:40:08

Okay Mat, no problem

It is just that in C and with tricky subject like pointers I have found over the years that it is important to be very precise. Pointers like the opportunity to misbehave if you give them any chance at all.

Paul