Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Why pointer cant have only value? (http://www.go4expert.com/forums/pointer-value-t8202/)

asadullah.ansari 11Jan2008 17:05

Why pointer cant have only value?
 
This is very simple question!!! Give me excat reason???

Example: Why it is wrong....
int main()
{
int * ptr;
*ptr=10;
return 0;
}

shabbir 11Jan2008 19:22

Re: Why pointer cant have only value?
 
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 11Jan2008 19:31

Re: Why pointer cant have only value?
 
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 11Jan2008 20:09

Re: Why pointer cant have only value?
 
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 11Jan2008 20:14

Re: Why pointer cant have only value?
 
dont tell me story of pointer that Pointer is made for only keeping some address . This is known by all.

shabbir 11Jan2008 21:05

Re: Why pointer cant have only value?
 
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 12Jan2008 14:29

Re: Why pointer cant have only value?
 
In that case, I've no idea what your question is.

technosavvy 14Jan2008 15:41

Re: Why pointer cant have only value?
 
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 16Jan2008 12:19

Re: Why pointer cant have only value?
 
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 16Jan2008 13:28

Re: Why pointer cant have only value?
 
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 times are GMT +5.5. The time now is 07:38.