# Pointers



## xfire (Jun 7, 2009)

Can any one tell me the advantage of a pointer to a pointer.
Also the advantage of array of pointers i.e int *p[20];
I've read that it can be used as a 2d array. Is that true?


----------



## FordGT90Concept (Jun 7, 2009)

FYI, pointers are not recommended because they're difficult for the OS to catch and handled.  Arrays with indexors/enumerators are the preferred method of storing/recalling data.


----------



## xfire (Jun 7, 2009)

I have an exam on C programming tomorrow, can't write that on the paper .
edit:sorry missed the fyi part


----------



## jagass (Jun 7, 2009)

Haha...Put it in your phone...lol


----------



## xfire (Jun 8, 2009)

The exam's over. It went well. This question wasn't asked.  
I'm guessing that just like an array name is the pointer to the first element of the array an array of pointers can be used to create a 2D array.
I guess a mod can close this thread down.


----------



## Darknova (Jun 8, 2009)

I've seen people pass exams not by answering the questions, but by showing knowledge above and beyond what they should have learnt during the education year.

Glad it went well for you anyway, I always sucked at exams. Hope you get the result you want!


----------



## FordGT90Concept (Jun 8, 2009)

Points are simply a value (address) that "points" to a value.  It has no extra information about what it is pointing to (hence the danger).  When using arrays/indexers, the operating system knows this is a collection that is this long and is of this type.  Any operation that steps outside of those constraints is caught and handled.  On the other hand, if a pointer so much as points to the wrong address, all the values will subsequently be wrong.

Pointers are faster but more risky.


----------



## xfire (Jun 9, 2009)

Darknova said:


> I've seen people pass exams not by answering the questions, but by showing knowledge above and beyond what they should have learnt during the education year.
> 
> Glad it went well for you anyway, I always sucked at exams. Hope you get the result you want!


It's the same over here. You need to put everything you know into the paper. I just didn't understand how it worked as a 2D array. It struck me while writing the exam. 



FordGT90Concept said:


> Points are simply a value (address) that "points" to a value.  It has no extra information about what it is pointing to (hence the danger).  When using arrays/indexers, the operating system knows this is a collection that is this long and is of this type.  Any operation that steps outside of those constraints is caught and handled.  On the other hand, if a pointer so much as points to the wrong address, all the values will subsequently be wrong.
> 
> Pointers are faster but more risky.


All in the coding. Maybe use an if statement to keep it going out of bounds.


----------



## W1zzard (Jun 9, 2009)

a pointer is really nothing more than a variable of machine word size (32 bit / 64 bit) that points to a byte memory address.

pointers to pointers are usually used when you call a function that returns a pointer.
void foo(char** mydata);

often in conjunction with memory allocated inside the function that you have to free yourself - typically such practice is bad(tm) unless you know exactly what you are doing.
void foo(...) { *mydata=malloc(10); strcpy(*mydata, "hello"); }

array of pointers could be used as a 2d array if each element points to an array.

pointers are a great thing if you know how to use them, your imagination is the limit. almost all more complex data structures are implemented using pointers. "safe" languages just mask the pointer access from you and give you a nice interface - they still all use pointers

in c you can use any pointer as an array with array indexing.

const char[] foo = "foo";
char* s=&foo;
s[0] -> 'f'
s[1] -> 'o'


----------



## xfire (Jun 9, 2009)

It works 


> #include<stdio.h>
> main()
> {
> int *p,n[2];
> ...





> #include<stdio.h>
> main()
> {
> int *p[2],n[2][1];
> ...


----------



## W1zzard (Jun 9, 2009)

read: http://stackoverflow.com/questions/377164/whats-your-preferred-pointer-declaration-style-and-why

good discussion there


----------



## denice25 (Jun 9, 2009)

great info FordGT90Concept....


----------



## FordGT90Concept (Jun 9, 2009)

xfire said:


> All in the coding. Maybe use an if statement to keep it going out of bounds.


There's no easy way to test for that.  The memory is just a giant array of bytes.  If your pointer changes location, it can literally give any value under the sun (hence, a leak).  The only way you can test for a bad pointer is to verify all the values are inside of acceptable parameters.  Even if they are, they may still not be the values originally intended.


Yes, pointers are at the core of any software dealing with storage of anytype (cache, RAM, hard drives, CDs/DVDs, etc.); however, operating systems, 9 times out of 10 can handle all the pointers with few instructions from the application author.

Ten years ago, handling the pointers was too much of a burden on the lowly processors to be common place.  Now, only the most demanding of software (like games) still requires the performance boost from pointers.  As processors get faster and more parallel, there will come a time when the performance even for games doesn't outweigh the necessity to prevent fatal crashes.


----------



## xfire (Jun 9, 2009)

I'm guessing you know that if an integer pointer increased by one, then it points to two blocks after(since int takes two bytes).
Using an if statement to keep the memory going out of bounds whenever it is incremented.  Like you want an array of 10, using a dynamic allocation create it of the required size. Using realloc whenever needed. Each time seeing that the memory is not exceeding. Bit hard to put it in exact words.(I'm not skilled enough to make such a program yet )


----------



## FordGT90Concept (Jun 9, 2009)

Incrementing a pointer is a...

address += sizeof(type)

...operation.  If the type is incorrect for the data contained, it will encounter all kinds of logical errors.


The compiler takes care of incrementing a pointer--it's one of the few tasks that are indrectly managed in dealing with pointers.


----------



## W1zzard (Jun 9, 2009)

FordGT90Concept said:


> Incrementing a pointer is a...
> 
> address += sizeof(type)
> 
> ...



aye. the compiler will also not let you increment a void (typeless) pointer.


----------



## FordGT90Concept (Jun 9, 2009)

address + null = null


----------



## W1zzard (Jun 9, 2009)

FordGT90Concept said:


> address + null = null



no, sir

#define NULL ((void *)0) // that's how NULL is defined in the compiler

address + NULL = address


----------



## FordGT90Concept (Jun 9, 2009)

Pointers are a numeric value.  Numeric values can never be null (as you said, resolves to 0).  You're right that address + null = address.  The compiler will error because it fails to increment the address.


Null really only has meaning when applied to classes (pointer points to 0; class instance doesn't exist).


----------



## W1zzard (Jun 9, 2009)

FordGT90Concept said:


> Pointers are a numeric value.  Numeric values can never be null (as you said, resolves to 0).  You're right that address + null = address.  The compiler will error because it fails to increment the address.
> 
> 
> Null really only has meaning when applied to classes (pointer points to 0; class instance doesn't exist).



no ... you can add NULL just fine and do any other arithmetic with it as well. (i assume we're talking about C / C++ here just like in this whole thread before). the compiler won't complain, feel free to compile and execute it yourself.

NULL has a valid meaning for any pointer, typically it is used as pointer uninitialized = "empty"

you can use it for anythign else you want as long as you don't dereference it


i think what you are talking about is

char* foo=NULL;
*foo++; // runtime error because it increments the char at address 0 which it isnt allowed to access


----------



## FordGT90Concept (Jun 9, 2009)

W1zzard said:


> char* foo=NULL;
> *foo++; // runtime error because it increments the char at address 0 which it isnt allowed to access


Exactly.


----------

