C++文件流,指针

1、简单文件输出流

ofstream outFile;
outFile.open("filetest.txt");
outFile << "这是文本输出流的测试文件" << endl;
outFile.close();9

2、文本输入测试

ifstream infile("filetest.txt");
if (infile.is_open()==false){   
    cout << "不能打开 " << endl;
}
else
{
    char buffer[50];
    infile.getline(buffer, 50);
    cout << buffer << endl;
}

3、函数原型中可以不含变量名

void cheers (int );

4、形参传递中数组名和实际和指针代表的含义相同.使用sizeof 打印传递的数组的长度,实际上智能显示指针的长度,因为实际算上传递的就是指针。

int sum_arr(int arr[], int n) ;
int sum_arr(int *arr,  int n);

5、

void show_array(const double arr[], int n);

意味着 其中在本函数中 ,通过 arr[]指向的那个数组,通过使用arr[x]的方式不能修改其值

6、const 与指针

  • const 变量地址 –>const指针 合法
  • const 变量地址 –>普通指针 非法
  • 非const变量指针–>const指针 合法
  • const变量地址 –>const指针 合法
int age =39;
const int * pt=&age;

const float g_earth =9.80;
const float *pe =&g_earth;

const float g_moon 1.163;
float *p =&g_moon;
  • 哪个参数是const 的注意看const后面是什么,是指针还是解引用
int * const finger =&sloth;       //const 后面是指针finger 故指针不能变化,指向的内容可以变
int const * finger =&sloth;       //const 后面的是* finger 故解引用不可变,指针可以变化
const int * const finger &sloth; //两个const 故指针和解引用均不可变

image

7、指针数组与数组指针

  • 通过结合性分析,若没有括号,则应该为右结合性,说明是个数组;若有括号,则括号内先结合,应该是个指针
int  * arr[4];     //指针数组,存储4int* 的数组
int  (*arr)[4]     //数组指针,指针指向一个形如 int a[4] 的数组

8、函数指针,返回函数的指针

double pam (int );
double (*pf)(int);

pf=pam;

pam(5);
(*pf)(5);     //以上两种都是调用pam 函数,功能相同。
  • 以下几项等价
double *f1 (const double ar[], int n);
double *f2 (const double []  , int n);
double *f3 (const double *   ,int n);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章