This is very simple question!!! Give me excat reason??? Example: Why it is wrong.... int main() { int * ptr; *ptr=10; return 0; }
First you submitted your query as article and I have moved to the forum for discussion and next thing is your query and I don't see anything wrong there?
It's wrong because you did this *ptr = 10; before you did this ptr = somewhere; You can't just declare a pointer and magically hope it points to somewhere useful. You have to do it yourself. Chaos and madness result if you don't. int a; ptr = &a; Gets you a pointer to ONE integer int a[10]; ptr = a; Gets you a pointer to TEN integers ptr = malloc( sizeof(*ptr) * 10 ); This too is a pointer to 10 integers. But at some point, you need to do free( ptr );
That's fine . I did'nt mean that. Fetaures should be as ... 1. Pointer keeping Only Address 2. Pointer Keeping Only Values 3. Both 1 & 2 approximately same. But why 2 is not supported??
dont tell me story of pointer that Pointer is made for only keeping some address . This is known by all.
friend if pointer will only have values then what do you want to do with the variables !! Code: int var; var can have values... if pointer will only have values then what is the use of pointers...
I wanna use like this manner.... Code: int main() { int *p,x=23; *p=10; //Some operation like simple variables ...... ...... printf("%d", *p); //After some Instant p=&x; // Some operation like pointer features As popular) ...... ...... printf("%d", *p); return 0; }
Sure, there's no problem with reassigning the pointer to point somewhere else, with p = &x; The real problem with your code is that at *p = 10, your pointer is UNINITIALISED. That means the code is broken. Try Code: int *p; int old, new; p = &old; *p = 10; ... p = &new; *p = 20; Remember, before you do *p = something;, you need a p = somewhere; statement before it, otherwise you've no idea what's happened.
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
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!!!!!
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.