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
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.
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.
Re: Increase char code doesn;t work
Quote:
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?
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.
Re: Increase char code doesn;t work
Quote:
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