- 0. 小结
- 3. pointers
- 4. dereferenceing Pointers
- 6. Pointers Programming Quiz
- 7. More on Pointers Answer
0. 小结
指针这部分很重要,也很容易出错,本章非常简单的介绍了指针,自己还要下去扩展,见C++ primer的书第4章。
3. pointers
/*Goal: Examine pointers!*/
#include <iostream>
int main()
{
int a = 54;
std::cout<<"a = "<<a<<"\n";
std::cout<<"address of a is at &a = "<< &a<<"\n";
return 0;
}
a = 54
address of a is at &a = 0x7ffc7b8d65ec
4. dereferenceing Pointers
But what if we have a pointer and want to access the value stored in that address? That process is called dereferencing, and it is indicated by adding the operator *
before the variable’s name.
/*Goal: Examine pointers!*/
#include <iostream>
int main()
{
// this is an integer variable with value = 54
int a = 54;
// this is a pointer that holds the address of the variable 'a'.
// if 'a' was a float, rather than int, so should be its pointer.
int * pointerToA = &a;
// If we were to print pointerToA, we'd obtain the address of 'a':
std::cout << "pointerToA stores " << pointerToA << '\n';
// If we want to know what is stored in this address, we can dereference pointerToA:
std::cout << "pointerToA points to " << * pointerToA << '\n';
return 0;
}
pointerToA stores 0x7ffdd96b3534
pointerToA points to 54
6. Pointers Programming Quiz
//*Sample program for Pointers*/
#include<iostream>
#include<string>
int main()
{
std::string name;
int givenInt;
float givenFloat;
double givenDouble;
std::string givenString;
char givenChar;
int *pointerGivenInt;
int **pointerPointerGivenInt;
pointerGivenInt = &givenInt;
pointerPointerGivenInt = &pointerGivenInt;
//Get the values of each variable
std::cout<<"integer = \n";
std::cin>>givenInt;
std::cout<<"float = \n";
std::cin>>givenFloat;
std::cout<<"double = \n";
std::cin>>givenDouble;
//We need to use cin.ignore so cin will ignore
//the characters in the buffer leftover
//from the givenDouble
std::cin.ignore();
std::cout<<"character = \n";
std::cin>>givenChar;
std::cout<<"string = \n";
std::cin.ignore();
std::getline (std::cin,givenString);
//The value stored in the data
std::cout<<"integer = "<<givenInt<<"\n";
std::cout<<"float = "<<givenFloat<<"\n";
std::cout<<"double = "<<givenDouble<<"\n";
std::cout<<"string = "<<givenString<<"\n";
std::cout<<"character = "<<(char)givenChar<<"\n\n";
//The address of the data - use pointers
std::cout<<"address integer = "<<&givenInt<<"\n";
std::cout<<"address float = "<<&givenFloat<<"\n";
std::cout<<"address double = "<<&givenDouble<<"\n";
std::cout<<"address string = "<<&givenString<<"\n";
std::cout<< "address character = " << (void *) &givenChar<<"\n\n";
//Use indirection to the get the value stored at the address
std::cout<< "pointer of givenInt = " << *pointerGivenInt<<"\n";
std::cout<< "pointer of pointer of givenInt = " << **pointerPointerGivenInt<< "\n";
return 0;
}
integer =
float =
double =
character =
string =
integer = 32
float = 64.212
double = 4.76545
string = Hey look at me! I know pointers!"
character = *
address integer = 0x7ffd812a9258
address float = 0x7ffd812a925c
address double = 0x7ffd812a9278
address string = 0x7ffd812a9270
address character = 0x7ffd812a9257
pointer of givenInt = 32
pointer of pointer of givenInt = 32
注意上面的语句中使用std::cout<< "address character = " << (void *) &givenChar<<"\n\n";
,有(void *)
,如果没有的话,输出的不是地址,而是字符本身 address character = *
7. More on Pointers Answer
/*Goal: Find out why you care about pointers*/
#include<iostream>
#include<string>
int main ()
{
int * pointerI;
int number;
char character;
char * pointerC;
std::string sentence;
std::string *pointerS;
pointerI = &number;
*pointerI = 45;
pointerC = &character;
*pointerC = 'f';
pointerS = &sentence;
*pointerS = "Hey look at me, I know pointers!";
std::cout << "number = "<<number<<"\n\n";
std::cout<<"character = "<<character<< "\taddress1: " << (void *)pointerC << "\n";
std::cout<<"\t\taddress2: " << pointerC << "\n\n";
std::cout<<"sentence = "<<sentence<<"\n";
return 0;
}
number = 45
character = f address1: 0x7ffd0232917b
address2: f-
sentence = Hey look at me, I know pointers!