一、参数指针形参和数组名实参
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<stdlib.h>
#include<cstring>
using namespace std;
void error(char* message){ //形参是一个char*类型的指针
printf("erroe : %s\n", message);
}
int main(){
char error_[50];
char* FileName = "inputFile";
sprintf(error_, "File %s not found", FileName);
error(error_); //实参是一个数组的数组名,相当于地址
return 0;
}
二、二维矩阵的指针操作(以一行为单位将数据写入矩阵)
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<stdlib.h>
#include<cstring>
using namespace std;
class Matrix {
public:
int row_num;
int col_num;
double **p = NULL;
};
Matrix* sampleMatrix = NULL;
void matrixInitialization(Matrix* matrix, int row, int col) {
//matrix = new Matrix;
matrix->row_num = row;
matrix->col_num = col;
matrix->p = new double*[matrix->row_num];
for (int i = 0; i < matrix->row_num; i++) {
matrix->p[i] = new double[matrix->col_num];
}
}
void ZeroMatrix(Matrix* matrix) {
for (int i = 0; i < matrix->row_num; i++) {
for (int j = 0; j < matrix->col_num; j++) {
matrix->p[i][j] = 0;
}
}
}
void ValueMatrix(Matrix* matrix, int value) {
for (int i = 0; i < matrix->row_num; i++) {
for (int j = 0; j < matrix->col_num; j++) {
matrix->p[i][j] = value;
}
}
}
void matrixDelete(Matrix* matrix) {
for (int i = 0; i < matrix->row_num; i++) {
delete[] matrix->p[i];
}
delete[] matrix->p;
}
void fun(double* p){
for(int i=0;i<5;i++){
p[i] = 9;
}
for(int i=0;i<5;i++){
cout<<p[i]<<" ";
}
cout<<endl;
}
int main(){
int rows = 10;
int cols = 7;
sampleMatrix = new Matrix;
matrixInitialization(sampleMatrix, rows, cols);
ValueMatrix(sampleMatrix, 6);
// for(int i=0;i<sampleMatrix->row_num;i++){
// for(int j=0;j<sampleMatrix->col_num;j++){
// cout<<sampleMatrix->p[i][j]<<" ";
// }
// cout<<endl;
// }
for(int i=0;i<10;i++){
fun(sampleMatrix->p[i]); //每一个p[i]是一个指向指针的指针,表征每一个样本
}
matrixDelete(sampleMatrix);
delete sampleMatrix;
return 0;
}