1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Why pointer cant have only value?

Discussion in 'C' started by asadullah.ansari, Jan 11, 2008.

  1. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    This is very simple question!!! Give me excat reason???

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

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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?
     
  3. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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 );
     
  4. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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??
     
  5. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    dont tell me story of pointer that Pointer is made for only keeping some address . This is known by all.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Its not meant to. Some things have some responsibility and so is the case with pointer.
     
  7. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    In that case, I've no idea what your question is.
     
  8. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    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...
     
  9. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA

    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;
    }
    
     
  10. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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.
     
  11. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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
     
  12. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    The last code you posted is broken, does that answer your question?
     
  13. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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!!!!!
     
  14. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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.
     

Share This Page