Why pointer cant have only value?

asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
This is very simple question!!! Give me excat reason???

Example: Why it is wrong....
int main()
{
int * ptr;
*ptr=10;
return 0;
}
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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?
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
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 );
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
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??
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
dont tell me story of pointer that Pointer is made for only keeping some address . This is known by all.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by asadullah.ansari
dont tell me story of pointer that Pointer is made for only keeping some address . This is known by all.
Its not meant to. Some things have some responsibility and so is the case with pointer.
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
In that case, I've no idea what your question is.
technosavvy's Avatar
Contributor
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...
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
Quote:
Originally Posted by technosavvy
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;
}
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
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.