Why pointer cant have only value?

TechCake
11Jan2008,17:05   #1
asadullah.ansari's Avatar
This is very simple question!!! Give me excat reason???

Example: Why it is wrong....
int main()
{
int * ptr;
*ptr=10;
return 0;
}
Go4Expert Founder
11Jan2008,19:22   #2
shabbir's Avatar
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?
Ambitious contributor
11Jan2008,19:31   #3
Salem's Avatar
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 );
TechCake
11Jan2008,20:09   #4
asadullah.ansari's Avatar
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??
TechCake
11Jan2008,20:14   #5
asadullah.ansari's Avatar
dont tell me story of pointer that Pointer is made for only keeping some address . This is known by all.
Go4Expert Founder
11Jan2008,21:05   #6
shabbir's Avatar
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.
Ambitious contributor
12Jan2008,14:29   #7
Salem's Avatar
In that case, I've no idea what your question is.
Contributor
14Jan2008,15:41   #8
technosavvy's Avatar
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...
TechCake
16Jan2008,12:19   #9
asadullah.ansari's Avatar
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;
}
Ambitious contributor
16Jan2008,13:28   #10
Salem's Avatar
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.