char and others different???
if i type in the following code i know that p will output the memory address of the first element, *p will output the value of the first element,p[a] will output the value of the element in b with the index number a and &p[a] will output the memory address of the same...
and when i typed this in i was right:
Code:
#include <iostream>
using namespace std;
int main(){
int *p;
int b[10] = {1234,12,351,25,235,123,5,235,1235,000};
p = b;
for(int a = 0; a < 10; a++){
cout << p << "\t" << *p << "\t" << p[a] << &p[a] << endl;
}
}
but when i try the same code with char datatype it doesn't work out well:
Code:
#include <iostream>
using namespace std;
int main(){
char *p;
char b[5] = {'a','a','z','a','d'};
p = b;
for(int a = 0; a < 10; a++){
cout << p << "\t" << *p << "\t" << p[a] << &p[a] << endl;
}
}
please friends out there, help me...
Thanx a lot GReaper... but i have a bit more doubts, which arose due to your reply...
Quote:
Originally Posted by
GReaper
When "cout" is told to print a char pointer( char* ), it assumes that you meant to print the null-terminated string that this address is pointing to, not the address itself.
hey GReaper,
you told that cout prints out assuming that the user meant to print the whole string so what to do when i want the address of the first element? i'll be grateful to you once you clear that doubt of mine and one last thing how to get the memory address of a particular element which in case of int arrays is done something like:
Code:
int *ptr;
int a[10] = {1,2,3,4,5,6,7,8,9,10};
cout << &ptr[4];
//this should output the memory address of the element containing 5