asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
All are knowing that all features of pointer!!! Okay dont explain!!!!

>>> My question is why pointer can't work on my above post code? Just answer me .......

1. Never should work
2. Should be implemented as for value keeping by pointer in spite of keeping address
3. Not required to keep the value only
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
The last code you posted is broken, does that answer your question?
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
Currently in all compilers, As my last code posted will broke. Offcourse, You can'nt assign any value to Uninitialized pointer But My aim is not to ask this...

I wanna ask that " Why This features Not added ? i.e. Why we can'nt assign value to a pointer before initialize to some memory i.e. Why Pointer can'nt have both features " Normal Variable" as well as " Present Pointer Features"?"

Just it's a brain storming!!!!!
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
Where would you have it point as a sensible default? Bear in mind that "sensible" needs to cover everything from writing C on a super computer, through your desktop machine and right down to the embedded system inside your toaster.

And having made it point somewhere, who's responsibility would it then be to free that associated memory at some future time. You can't allocate it on the stack because of the problems of someone returning the pointer itself.

Besides, being a pointer, how would you decide how much memory to point to?
Just one int, or maybe 10. How about 10000?

*p = 10;
p[0] = 10;
are the same as far as the language is concerned, so there's no stopping someone from doing say p[1] or p[1234] either. So now you've replaced an uninitialised pointer problem with an out of bound access problem. In other words, you've just rearranged the deck chairs on the Titanic.

int var;
This has no default value either, so in that respect a pointer is no different. It too starts out with junk until YOU specifically do something to initialise or assign a value.

C does exactly what you ask it to. There is no baggage for keeping things in order because of programmer lazyness.

If you want that kind of behaviour, then use a std::vector in C++.

IMO, the sensible default is to declare
int *p = NULL;
Which on most machines gives a predictable response to trying to dereference the pointer before assigning it a memory location to point to. This is far better than it randomly working and the programmer believing that everything is still OK.