pointer question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: pointer question

  1. #1
    Join Date
    Mar 2009
    Posts
    82

    pointer question

    Hello!

    I got one code:
    Code:
    int main()
    {
        int *test1,a;
        
        test1=&a;
        
        test1[0]=5;
        
        cout<<test1<<endl;
        
    
        system("PAUSE");
        return 0;
    }
    How is possible that compiler lets me use array test1[0] or something even if the test1 is pointer?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: pointer question

    Arrays are just a special case of pointers, that's why.

    test1[0] is simply another way of writing *(test1 + 0).

  3. #3
    Join Date
    Mar 2009
    Posts
    82

    Re: pointer question

    Thanks for the reply.
    But what if I write
    test[1]=5;

    and

    cout<<*(test1+1)<<endl;

    the program crashes. What's the problem?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: pointer question

    Quote Originally Posted by StGuru View Post
    Thanks for the reply.
    But what if I write
    test[1]=5;

    and

    cout<<*(test1+1)<<endl;

    the program crashes. What's the problem?
    test1 points to an int. One and only one int. test1 is valid as it points to a. test1 + 1 isn't.

  5. #5
    Join Date
    Mar 2009
    Posts
    82

    Re: pointer question

    So if I put a[5], then it would work right?

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: pointer question

    Quote Originally Posted by StGuru
    So if I put a[5], then it would work right?
    a is neither a pointer nor an array, but an int.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: pointer question

    Quote Originally Posted by laserlight View Post
    a is neither a pointer nor an array, but an int.
    I think he means if he declared it as an array like

    int a[5].

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: pointer question

    Quote Originally Posted by StGuru View Post
    So if I put a[5], then it would work right?
    Almost. In that case, a becomes a pointer, so you'd have

    int a[5];
    int* test = a;

    You don't need a separate pointer though.

  9. #9
    Join Date
    Mar 2009
    Posts
    82

    Re: pointer question

    Yes, your right. :-)
    I got one task where it was test= new int[5];

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: pointer question

    If you do that, don't forget to delete[] that memory when you're done with it.

  11. #11
    Join Date
    Mar 2001
    Posts
    2,375

    Re: pointer question

    Personally I wouldn't use new if there is a low volume of POD or plain old data involved. Just makes life easier.
    ahoodin
    To keep the plot moving, that's why.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center