char *str(char *p1, const char *p2)传入这个函数的p1参数,会改变实参的值么
时间: 2024-03-21 10:34:39 浏览: 55
根据提供的引用内容,函数形参`char *str(char *p1, const char *p2)`中的`p1`参数是一个指向`char`类型的指针变量。当将实参传递给这个函数时,可以通过`p1`指针来修改实参的值。因此,传入这个函数的`p1`参数会改变实参的值。
范例:<<引用:#include <iostream> using namespace std; void changeValue(char *p) { *p = 'Y'; } int main() { char str[] = "Hello"; cout << "Before change: " << str << endl; changeValue(str); cout << "After change: " << str << endl; return 0; }>>
```cpp
#include <iostream>
using namespace std;
void changeValue(char *p) {
*p = 'Y';
}
int main() {
char str[] = "Hello";
cout << "Before change: " << str << endl;
changeValue(str);
cout << "After change: " << str << endl;
return 0;
}
```
输出:
```
Before change: Hello
After change: Yello
```
相关问题
设计自定义异常类NotTriangleException;设计Triangle(三角形)类; (1)Triangle类基本信息:三个顶点坐标; (2)带三个顶点坐标参数的构造函数;当传入实参为不合理数据时(例如:输入的三角形的顶点不能组成一个三角形),抛出自定义异常类NotTriangleException的对象; (3)有返回三角形面积的函数getArea()。 (4)各个类的声明放在与类同名的头文件中(*.h),类的实现放在与类同名的源文件中(*.cpp); (5)只能在主函数中输入输出,各个类的成员函数中不能进行输入输出。 (6)在主函数中,接收用户输入的三角形三个顶点坐标,然后,传给三角形对象的构造函数,并捕获NotTriangleException异常对象。当捕获到异常后,提示用户输入数据错误,并要求用户重新输入。
NotTriangleException类的声明(NotTriangleException.h):
```c++
#ifndef NOT_TRIANGLE_EXCEPTION_H
#define NOT_TRIANGLE_EXCEPTION_H
#include <exception>
#include <string>
class NotTriangleException : public std::exception {
public:
NotTriangleException() : msg("Not a valid triangle.") {}
NotTriangleException(const std::string& message) : msg(message) {}
virtual const char* what() const noexcept {
return msg.c_str();
}
private:
std::string msg;
};
#endif // NOT_TRIANGLE_EXCEPTION_H
```
Triangle类的声明(Triangle.h):
```c++
#ifndef TRIANGLE_H
#define TRIANGLE_H
class Point;
class Triangle {
public:
Triangle(const Point& p1, const Point& p2, const Point& p3);
double getArea() const;
private:
Point* vertex[3];
};
#endif // TRIANGLE_H
```
Triangle类的实现(Triangle.cpp):
```c++
#include "Triangle.h"
#include "Point.h"
#include "NotTriangleException.h"
#include <cmath>
Triangle::Triangle(const Point& p1, const Point& p2, const Point& p3) {
vertex[0] = new Point(p1);
vertex[1] = new Point(p2);
vertex[2] = new Point(p3);
// 判断三个点是否能组成一个三角形
double a = vertex[0]->distanceTo(*vertex[1]);
double b = vertex[0]->distanceTo(*vertex[2]);
double c = vertex[1]->distanceTo(*vertex[2]);
if (a + b <= c || a + c <= b || b + c <= a) {
throw NotTriangleException();
}
}
double Triangle::getArea() const {
double a = vertex[0]->distanceTo(*vertex[1]);
double b = vertex[0]->distanceTo(*vertex[2]);
double c = vertex[1]->distanceTo(*vertex[2]);
double p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
```
Point类的声明(Point.h):
```c++
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point(double x = 0, double y = 0);
Point(const Point& other);
double getX() const;
double getY() const;
double distanceTo(const Point& other) const;
private:
double x;
double y;
};
#endif // POINT_H
```
Point类的实现(Point.cpp):
```c++
#include "Point.h"
#include <cmath>
Point::Point(double x, double y) : x(x), y(y) {}
Point::Point(const Point& other) : x(other.x), y(other.y) {}
double Point::getX() const {
return x;
}
double Point::getY() const {
return y;
}
double Point::distanceTo(const Point& other) const {
double dx = x - other.x;
double dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
```
主函数(main.cpp):
```c++
#include "Triangle.h"
#include "Point.h"
#include "NotTriangleException.h"
#include <iostream>
int main() {
double x1, y1, x2, y2, x3, y3;
while (true) {
std::cout << "请输入三角形的三个顶点坐标(x1,y1,x2,y2,x3,y3):";
std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
try {
Point p1(x1, y1);
Point p2(x2, y2);
Point p3(x3, y3);
Triangle t(p1, p2, p3);
std::cout << "三角形的面积为:" << t.getArea() << std::endl;
break;
} catch (NotTriangleException& e) {
std::cout << e.what() << "请重新输入。" << std::endl;
}
}
return 0;
}
```
阅读全文