Results 1 to 3 of 3
Hi,
i was trying to access the address of the character pointer it gives me the values stored in the variable.
Code:
#include<iostream>
using namespace std;
int main()
{
char ...
- 07-12-2010 #1Just Joined!
- Join Date
- Sep 2009
- Posts
- 5
address of a character pointer
Hi,
i was trying to access the address of the character pointer it gives me the values stored in the variable.
now my output is:Code:#include<iostream> using namespace std; int main() { char *i; i=new char[2]; i[0]='1'; i[1]='2'; cout<<i<<"\n"; delete[]i; return 0; }
then i changed the type of the variable to int,Code:12
Code:#include<iostream> using namespace std; int main() { int *i; i=new int[2]; i[0]=1; i[1]=2; cout<<i<<"\n"; delete[]i; return 0; }
the out put is the address which is expected
Code:0x9d99008
why is my code not giving the address of the variable i when it is a character pointer.
-swetha
- 07-12-2010 #2
It is the other way around,
is refering to the address of the variable both times.Code:i
In the case of 'char',
If the function cout gets such a char*, it automatically[*] knows that it should display the null-terminated string beginning at this address. (You forgot the terminating 0 by the way, making it crash-prone.)Code:type(i) = char*
[*] It knows because the operator << is overloaded for several different types on the right hand side.Debian GNU/Linux -- You know you want it.
- 07-12-2010 #3
If you want the address of the elements of a character array try:
Code:int x = 0; char *i = new char[2]; cout<<&i[x]<<"\n";
Make mine Arch Linux


Reply With Quote
