-
June 11th, 2014, 04:26 AM
#1
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.
-
June 11th, 2014, 04:49 AM
#2
Re: Increase char code doesn;t work
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.
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.
-
June 11th, 2014, 04:59 AM
#3
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
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)
-
June 11th, 2014, 06:51 AM
#4
Re: Increase char code doesn;t work
Originally Posted by 2kaud
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
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?
-
June 11th, 2014, 07:15 AM
#5
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)
-
June 11th, 2014, 07:50 AM
#6
Re: Increase char code doesn;t work
Originally Posted by 2kaud
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|