Thanks, I will try. But I am not "cout" ing a char*, I am counting the address of the first element? Are you saying that &test[0] and *test are equivalent?
Hello indy2005.
You should just take a couple of days to read about pointers and you will grasp C just fine.
int array1[30];
- array1 points to the memory zone where the 30 int array begins.
int *array2;
- array2 points to the memory zone where a virtually unlimited int array begins, where the size is limited by the available memory.
You can quickly realize that array[i] and *(array+i) represent the same thing,since "array" is a pointer.
Declaring int array[30]; is meant to be used when you know the number of elements in the array (in our case 30),
while int* array; is used when you don't know how many elements the array will contain. Read about dynamic allocation and you'll make another big step in grasping C.
#include <iostream>
usingnamespace std;
void print(int n)
{
cout << "int overload called!" << endl;
cout << n << endl;
}
void print(char c)
{
cout << "char overload called!" << endl;
cout << c << endl;
}
int main()
{
int a;
a=65;
print(a); // prints 65
print((char)(a)); // prints A
cout << "\nhit enter to quit...";
cin.get();
return 0;
}
Notice that both functions have the same name (print) but they accept different argument types (int, char).
The compiler is not confused though; it knows the right function to call by looking at the argument you pass.
So, the first call of the print function in main calls the int overload, while the second one calls the char overload.
The << operator is also an overloaded function. Its behavior depends on the argument you pass.
The char * overload prints the whole cstring, while the other pointer overloads print the address.
Thanks. cout and its overloads can be a bit confusing when you are trying to learn pointers, as you are grappling with cout behaviour as you describe also. I wasnt sure why you need to cast to a pointer just to get the address, but cout is overloaded to behave in a way which prints out the address of an int* pointer in hex?
cout is overloaded to behave in a way which prints out the address of an int* pointer in hex?
Yes, cout is overloaded to print int pointers in hex. Ok, I now understand what you want.
If you want to print the address in base 10, then, yes, you should cast it to an (unsigned) int/long.