#include <iostream>
usingnamespace std;
void dnl();
int digit = 25; // Variables
float number = 12.5;
char letter = 'A';
int set[4] = {2,4,6,8};
int *Iptr; // Pointers
float *Fptr;
char *Cptr;
int *Aptr;
Iptr = &digit; //I get errors here
Fptr = &number;
Cptr = &letter;
Aptr = set;
void dnl()
{
cout << "The address of digit is " << *Iptr << endl;
cout << "The size of digit is " << sizeof(digit) << " bytes\n";
cout << "The value in digit is " << digit << endl;
system ("pause");
}
int main()
{
dnl();
system ("pause");
}
cout << "The address of digit is " << Iptr << endl;
cout << "The size of digit is " << sizeof(digit) << " bytes\n";
cout << "The value in digit is " << digit << endl << endl;
cout << "The address of number is " << Fptr << endl;
cout << "The size of number is " << sizeof(number) << " bytes\n";
cout << "The value in number is " << number << endl << endl;
cout << "The address of letter is " << Cptr << endl;
cout << "The size of letter is " << sizeof(letter) << " bytes\n";
cout << "The value in letter is " << letter << endl << endl;
Type char* is a special case. char pointers are treated as C-style strings... so when you cout a char pointer, it will print the string that the pointer points to, rather than the address.
To get around this, you can cast the pointer to a different pointer type:
1 2
cout << Cptr; // prints it as a string
cout << static_cast<void*>(Cptr); // prints it as a pointer
Darn I am having more trouble now, I need to find information on "set" which is an array, how can I do that? I tried what I did earlier but that did not work.
Also if somebody could explain or refer me to learn how to find the address of specific value IN an array that would be very helpful! These pointers have got me very confused.
#include <iostream>
usingnamespace std;
void dnl();
void setinfo();
int digit = 25; // Variables
float number = 12.5;
char letter = 'A';
int set[] = {2,4,6,8};
void dnl()
{
int *Iptr; // Pointers
float *Fptr;
char *Cptr;
int *Aptr;
Iptr = &digit;
Fptr = &number;
Cptr = &letter;
Aptr = set;
cout << "The address of digit is " << Iptr << endl;
cout << "The size of digit is " << sizeof(digit) << " bytes\n";
cout << "The value in digit is " << digit << endl << endl;
cout << "The address of number is " << Fptr << endl;
cout << "The size of number is " << sizeof(number) << " bytes\n";
cout << "The value in number is " << number << endl << endl;
cout << "The address of letter is " << static_cast<void*>(Cptr) << endl;
cout << "The size of letter is " << sizeof(letter) << " bytes\n";
cout << "The value in letter is " << letter << endl << endl;
system ("pause");
}
void setinfo()
{
cout << "The address of set is " << Aptr << endl;
system ("pause");
}
int main()
{
dnl();
setinfo();
system ("pause");
}
Darn I am having more trouble now, I need to find information on "set" which is an array, how can I do that? I tried what I did earlier but that did not work.
The way you did it is right. It won't work because Aptr can't be "seen" from setinfo(). Aptr is a local variable declared inside main dnl function. Just ditch setinfo() and put that code inside main. Try declaring Aptr inside setinfo(). It should work.
Also if somebody could explain or refer me to learn how to find the address of specific value IN an array that would be very helpful! These pointers have got me very confused.
It's set==&set[0] i.e. the address of the array itself is the address of the first element of the array. To get the others you can simply write set+1 or &set[1] for the second element, set+2 or &set[2] for the third and so on...