CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2014
    Posts
    7

    Increase char code doesn;t work

    Code:
    #include <iostream> 
    using namespace std; 
     
     
    int main () 
    { 
        char character = 'a';
        char * pointera;
        char * pointerb;
        pointera = &character;
        pointerb = *pointera;
        *(++pointerb);
        cout << pointerb;
    }
    the point of this code is to increase character by 1 (so from a to b in this case).
    The line highligted in red is the line that the system is rejecting at the moment (but there may be other issues). Please can someone explain why it is invalid?

    thanks
    Last edited by jsmith613; June 11th, 2014 at 04:42 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Increase char code doesn;t work

    Quote Originally Posted by jsmith613
    the point of this code is to increase character by 1 (so from a to b in this case).
    Note that it is not guaranteed that 'a' + 1 == 'b', though this is likely since it is likely that ASCII or its derivatives are in use.

    Quote Originally Posted by jsmith613
    The line highligted in red is the line that the system is rejecting at the moment (but there may be other issues). Please can someone explain why it is invalid?
    Examine the types. pointera is a pointer to char, therefore *pointera is a char. pointerb is a pointer to char. You tried to assign *pointera to pointerb, i.e., you tried to assign a char to a pointer to char, which is wrong.
    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

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Increase char code doesn;t work

    pointerb is defined as a pointer to a char - ie pointerb will hold a memory address. pointera is also a pointer to a char so again will hold a memory address. *pointera is a dereference of pointera to return the contents of memory to which pointera is pointing. In this case *pointera returns a type of char. You are therefore trying to assign a type char to a type pointer to char which are incompatible types. Try
    Code:
    pointerb = pointera;
    There is also an issue with
    Code:
    *(++pointerb);
    you are incrementing pointerb which is a memory address and then dereferencing this memory address. But the memory one past that pointed to by pointerb is not defined and initialised in the program. Try
    Code:
        ++(*pointerb);
        cout << *pointerb << endl;
    You also need the dereference in the cout statement as without the * cout treats pointerb as a pointer to a null-terminated char string and displays the contents of memory from the address pointed to until it finds a memory address containing a 0.

    I think you are having trouble with the two uses of * in relation to memory and pointers.

    When used with a type it means 'pointer to' - so char *ch; means ch is a pointer to a char.

    When used with a variable that is a pointer it means dereference the memory pointed to by the variable and obtain the contents of the memory - so cout << *ch; means display the contents of the memory pointed to by ch.

    Code:
    char chr = 'a';
    char *ch = &chr;
    cout << *ch;
    the two uses of *ch above have the two different meanings as explained above.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jun 2014
    Posts
    7

    Re: Increase char code doesn;t work

    Quote Originally Posted by 2kaud View Post
    pointerb is defined as a pointer to a char - ie pointerb will hold a memory address. pointera is also a pointer to a char so again will hold a memory address. *pointera is a dereference of pointera to return the contents of memory to which pointera is pointing. In this case *pointera returns a type of char. You are therefore trying to assign a type char to a type pointer to char which are incompatible types. Try
    Code:
    pointerb = pointera;
    There is also an issue with
    Code:
    *(++pointerb);
    you are incrementing pointerb which is a memory address and then dereferencing this memory address. But the memory one past that pointed to by pointerb is not defined and initialised in the program. Try
    Code:
        ++(*pointerb);
        cout << *pointerb << endl;
    You also need the dereference in the cout statement as without the * cout treats pointerb as a pointer to a null-terminated char string and displays the contents of memory from the address pointed to until it finds a memory address containing a 0.

    I think you are having trouble with the two uses of * in relation to memory and pointers.

    When used with a type it means 'pointer to' - so char *ch; means ch is a pointer to a char.

    When used with a variable that is a pointer it means dereference the memory pointed to by the variable and obtain the contents of the memory - so cout << *ch; means display the contents of the memory pointed to by ch.

    Code:
    char chr = 'a';
    char *ch = &chr;
    cout << *ch;
    the two uses of *ch above have the two different meanings as explained above.
    thanks for this. I have modified the programme slightly in the hope to make the code clearer:

    Code:
    int main () 
    { 
        char a = 'a';
        char * pointera;
        pointera = &a;
        cout << *pointera << endl;
        ++pointera;
        cout << *pointera;
    }
    the problem is the outputs. As expected the first output is "a" but the second output varies (I have gottten x, h, H, 8, open brackets [(] and no value). why is this?

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Increase char code doesn;t work

    Code:
    ++pointera;
        cout << *pointera;
    No! You are incrementing the pointer pointera ie you are incrementing the memory address to the next one to which pointera points. You are not incrementing the value pointed to by pointera. Therefore your second cout displays what ever happens to be in memory at the address pointed to by pointera. Consider
    Code:
    *pointera = *pointera + 1;
    cout << *pointera;
    This adds one to the contents of the memory pointed to by pointera because the memory pointed to by pointera is being deferenced before use so 1 is added to the contents rather than the pointer.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Jun 2014
    Posts
    7

    Re: Increase char code doesn;t work

    Quote Originally Posted by 2kaud View Post
    Code:
    ++pointera;
        cout << *pointera;
    No! You are incrementing the pointer pointera ie you are incrementing the memory address to the next one to which pointera points. You are not incrementing the value pointed to by pointera. Therefore your second cout displays what ever happens to be in memory at the address pointed to by pointera. Consider
    Code:
    *pointera = *pointera + 1;
    cout << *pointera;
    This adds one to the contents of the memory pointed to by pointera because the memory pointed to by pointera is being deferenced before use so 1 is added to the contents rather than the pointer.
    I think I understand now.
    I simply assumed that the letters a through z were stored in adjacent memory locations. i didn't realise that it was actually phyiscally changing the value.

    thanks for the clarification

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured