Thread: I thought pointers were pointers...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Does this differ in C++? In C++ are arrays not glorified pointers?

    To demonstrate:
    I was hoping you would demonstrate the difference yourself using sizeof as was mentioned earlier, but you did not

    To demonstrate:
    Code:
    #include <iostream>
    
    int main() {
        char pointer[] = "keira";
        char* otherPointer = pointer;
        std::cout << sizeof(pointer) << " " << sizeof(otherPointer) << std::endl;
        return 0;
    }
    On my system, the output is "6 4". Basically, C++ has the same distinction between arrays and pointers as C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Alright thank you for your help everyone and sorry I'm so thick headed... This is a concept I thought I had completely locked down after spending many months in assembly and C++.

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by keira View Post
    Why is the size of c being read as the size of a 32bit pointer instead of the size of the buffer it points to? What is different about the pointers?
    Nothing is different. c is a pointer. Therefore sizeof(c) is the size of the pointer. Why would you expect anything different?

  4. #19
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I don't understand where you got the idea that I was comparing dynamically allocated memory with local memory...
    When you use pointer you usually use dynamically allocated memory. What is the local memory then? You mean arrays?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #20
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Local would be stack memory, dynamic would be heap memory.

    The expression: char* string = "keira"; Creates both the pointer string and the consecutive ordering of bytes that translate to "keira" on the stack.

    Basically to answer my original question, this was what i was looking for: (taken from http://c-faq.com/aryptr/aryptrequiv.html)

    That is, whenever an array appears in an expression, the compiler implicitly generates a pointer to the array's first element, just as if the programmer had written &a[0]. (The exceptions are when the array is the operand of a sizeof or & operator, or is a string literal initializer for a character array. [footnote] See questions 6.23, 6.12, and 1.32, respectively.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. 2D Array of Pointers
    By Slavakion in forum C++ Programming
    Replies: 12
    Last Post: 03-31-2004, 05:05 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 08:30 AM
  5. Array of pointers
    By falconetti in forum C Programming
    Replies: 5
    Last Post: 01-11-2002, 07:26 PM