Results 1 to 5 of 5

Thread: Arrays and Pointers - C++

  1. #1
    Join Date
    Sep 2011
    Location
    Wandering
    Beans
    75
    Distro
    Ubuntu 10.04 Lucid Lynx

    Arrays and Pointers - C++

    Is it valid to say in c/c++ that the name of an array is a pointer?
    I already know that this is valid
    Code:
    int c[10];
    &c[0]==c;
    From this can i assume that the name of an array is a pointer?
    -You can't just turn on creativity like a faucet. You have to be in the right mood.
    -What mood is that?
    -Last-minute panic.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Arrays and Pointers - C++

    in some context it behaves like one, but not always
    check the difference between sizeof(c) and sizeof(&c[0])
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Arrays and Pointers - C++

    Quote Originally Posted by dep0 View Post
    Is it valid to say in c/c++ that the name of an array is a pointer?
    Make up your mind, C or C++? In C, no. In C++, I don't know.
    「明後日の夕方には帰ってるからね。」


  4. #4
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Arrays and Pointers - C++

    Quote Originally Posted by dep0 View Post
    Is it valid to say in c/c++ that the name of an array is a pointer?
    I already know that this is valid
    Code:
    int c[10];
    &c[0]==c;
    From this can i assume that the name of an array is a pointer?
    arrays and pointers are different types. But both types represent a very similar concept, except arrays have the additional notion of the size of memory pointed to.
    It is very important to remember that arrays decays into a pointer on a change of stack frame.
    That means as soon as you are in a different frame the size information is lost
    e.g.

    Code:
    main()
    {
      int c[10]
      assert(sizeof(c == sizeof(int) * 10));
      f(c);
    }
    
    f(int a[])
    {
      assert(sizeof(a == sizeof(int*)));
      // a is now a pointer to the array in the previous stack frame!!
      // [] syntax here is just "syntactic sugar", int * a is the exact same thing
    }
    Last edited by MadCow108; November 18th, 2012 at 01:25 AM.

  5. #5
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Arrays and Pointers - C++

    Quote Originally Posted by MadCow108 View Post
    Code:
      assert(sizeof(c == sizeof(int) * 10));
    
      assert(sizeof(a == sizeof(int*)));
    Careless mistake, I'm sure, but both of those evaluate to assert(sizeof(int)). Actually maybe in C++ it's sizeof(bool)... correct me if I'm wrong.

Bookmarks

Posting Permissions

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