Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Having difficulty with the type char* and pointers in general (c++)

Options
  • 20-12-2012 5:14pm
    #1
    Registered Users Posts: 1,450 ✭✭✭


    How do,
    I'm relearning C++ and just trying to get my head around everything as opposed to just accepting it works and have struck a wall in relation to pointers. I understand the concept of them and why they're used but an example I have is proving difficult. It is when a pointer is assigned to a char type.

    It begins by stating in Private within a Class:

    char* buffer;

    Now my understanding of this is that 'buffer' now contains an address to a character variable (ie one of the 256 available). It has not been initialised NULL or anything so at the moment there is a junk address in it which I consider to be bad (please correct me if I'm way off here).

    Then in the Public section of the class, buffer pops up again with this line:

    buffer=new char[strlen(Input)+1];

    Input is a string I've entered in (eg "boards.ie ate my balls"). Now my understanding of this is that the program is now requesting more memory to the size of the length of my string plus one space for the null terminating factor.

    Next line is as follows:

    strcpy(buffer, Input);

    Now, this is the Input (my string) been copied into buffer which there should be enough space because I requested it with new. That's the way I see it and please correct me if I'm way off but the problem I have is that isn't buffer a pointer? shouldn't it only contain an address and nothing else? If I wanted to see the contents shouldn't I have to use the dereferencing operator (*)? I don't understand why if I type

    cout<<buffer<<endl;

    I get the Input ("boards.ie ate my balls") instead of an address (eg 0025F78). Any help would be appreciative. I'm learning out of books so have no goto guy to consult.


Comments

  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    There is an ostream operator<< overload that takes a pointer to a char that prints the characters until it hits a null terminator. Similarly, printf in C will take a pointer to a char and just print away until it hits the same terminator. It's why things like that are considered unsafe.

    If you dereference a pointer to a char, you get the 1 byte of memory at the address the pointer pointed do.


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    It is when a pointer is assigned to a char type.

    char* buffer;

    Well for starters you are thinking about it wrong. It's not a pointer assigned to a char type. It's a pointer, and it just happens to point at a char.

    If you look at the raw data for the char* you will see it is NOT a char. It is either a 32bit or 64bit number (depending on whether you compiled as 32 or 64). This number is an address in memory. Your actual char (1 byte) resides at this memory address.

    A similar example (common cause of mistakes):
    uint32* whatever;
    whatever++; // this adds sizeof(uint32) to the value, not 1!

    Regarding your statement about variable initialisation, the C++ standard says nothing about this so it's undefined.

    int i;
    With visual studio release mode this will be 0. In debug mode it will be garbage. On gcc it will be garbage always. Very common source of cross-platform bugs. So always initialise stuff.

    It seems the source of your problem is trying to mix strings and chars. Just don't, stick with one. You can go somestring.c_str() to get a pointer to the underlying array, but it might be in unicode or whatever. Not necessarily chars at all.

    Just to be ABSOLUTELY clear:
    In C++ there is a class called std::string. Do not say "string" unless you are referring to this class. A char array is not a string! Most of your post is actually talking about C, with a tiny bit of C++ thrown in (cout).

    Regarding your question about strcpy():
    This is a C function that operates on char arrays. Yes it expects a pointer as input. It relies on the trailing null character to know where the array ends. If there is no trailing null (at position = strlen+1) then you will have a bad day.


  • Registered Users Posts: 1,450 ✭✭✭actuallylike


    Thanks for the responses, still confusing me but feel a little Eureka moment about to happen in the next few hours so I'll keep my head in the book and read over your replies.


Advertisement