Thread: Confusing pointer

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Confusing pointer

    In my question paper there was a question to find output of the following program

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    int main()
    {
      int ia[3]={2,5,9};
      int *ptr=ia;
      
      for(int i=0;i<3;i++)
      {
        cout<<*(ia++);//Error Lvalue reqd.
        cout<<" "<<*ptr++;
      }
    getch();
    return(0);
    }
    On compiling it on my pc i am getting strange error l value reqd.Can anyone tell me why i am getting it. I also request you to compile it on your compiler as i am using old turbo c++.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If you're trying to say *ia + 1, then that isn't what *(ia++) means. Try changing it to *ia + 1

    When you say *(ia++) you're trying to use a non-lvalue as an lvalue.
    Last edited by SlyMaelstrom; 11-13-2005 at 03:25 AM.
    Sent from my iPadŽ

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Array names are constant pointers...you can not alter them...read further from here

    http://www.fredosaurus.com/notes-cpp...spointers.html

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    thanks sunny,
    It did not strike me during paper.
    I have one more doubt.
    Code:
    int *p=new int[10];
    int p[i]=3;
    how can a pointer change to array here?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    int *p=new int[10];
    p[i]=3;
    This should work fine.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by vaibhav
    thanks sunny,
    It did not strike me during paper.
    I have one more doubt.
    Code:
    int *p=new int[10];
    int p[i]=3;
    how can a pointer change to array here?
    Your declaring a new variable p in the second line, which should give you a compiler error saying something like "redefinition of p". A variable named p already exists in the first line.

  7. #7
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by vaibhav
    how can a pointer change to array here?
    Now you are dynamically allocating 10 bytes of memory which is pointed by pointer p.....and it is one of many ways to access the memory.

    p[i] is equivalent to *(p+i) and i[p]

    Note: Pointers are not arrays and arrays are not pointers

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Assuming the the int size is 4 then allocating 10 ints allocates 40 bytes.

  9. #9
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    sorry for that.....and thanx for correction

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Quantum1024
    Code:
    int *p=new int[10];
    p[i]=3;
    This should work fine.
    You should probably also free the memory:
    Code:
    delete [] p;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  2. confusing char pointer
    By vaibhav in forum C Programming
    Replies: 7
    Last Post: 08-29-2005, 07:45 PM
  3. Pointer To Functions In COM Confusing
    By bartybasher in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2004, 03:08 PM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. Replies: 2
    Last Post: 11-22-2001, 12:22 PM