如何返回在函数里初始化的指针,而且数据不变
时间: 2024-03-21 13:40:18 浏览: 21
如果需要在函数内初始化一个指针,并在函数结束后返回该指针,同时保证该指针所指向的数据不变,可以考虑以下两种方法:
1. 将指针指向的数据复制到一个新的内存空间中,并返回指向该内存空间的指针。这样就可以确保返回的指针所指向的数据不会被改变。例如:
```
int* func() {
int a = 10;
int* p = malloc(sizeof(int));
*p = a;
return p;
}
```
在这个例子中,使用malloc函数分配了一个新的内存空间,将a的值复制到该空间中,并返回指向该空间的指针。由于返回的指针指向的是动态分配的内存空间,因此可以确保该指针所指向的数据不会发生改变。
2. 将指针作为函数参数传递,并在函数内部修改指针所指向的数据。这样可以确保修改后的数据可以被调用函数所使用,同时也避免了返回指针所指向的数据可能会被改变的问题。例如:
```
void func(int* p) {
int a = 10;
*p = a;
}
int main() {
int b;
func(&b);
printf("%d", b); // 输出10
return 0;
}
```
在这个例子中,将一个指向int类型变量的指针作为函数参数传递给了func函数。在函数内部,将a的值赋值给了指针所指向的变量。这样就可以确保修改后的数据可以被调用函数所使用。同时,由于没有返回指针,因此也避免了返回指针所指向的数据可能会被改变的问题。
相关问题
#include <iostream> using namespace std; class Student {public: 填空 //利用参数初始化表进行数据初始化 void display(); private: int num; float score; }; void Student::display() {cout<<num<<" "<<score<<endl;} int main() { Student stud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; //定义对象数组 填空 //定义对象指针指向对象数组 for( 填空 )//显示第1、3、5名学生信息 p->display(); return 0; } }在写了填空的地方填空,其余不变
下面是填空后的代码实现:
```cpp
#include <iostream>
using namespace std;
class Student {
public:
Student(int p_num, float p_score) : num(p_num), score(p_score) {} // 利用参数初始化表进行数据初始化
void display();
private:
int num;
float score;
};
void Student::display() {
cout << num << " " << score << endl;
}
int main() {
Student stud[5] = {
Student(101, 78.5),
Student(102, 85.5),
Student(103, 98.5),
Student(104, 100.0),
Student(105, 95.5)
}; // 定义对象数组
Student* p = stud; // 定义对象指针指向对象数组
for (int i = 0; i < 5; i += 2) // 显示第1、3、5名学生信息
(p+i)->display();
return 0;
}
```
在这个代码中,我们利用了参数初始化表来进行数据初始化,这样代码更加简洁。同时,我们也定义了对象指针 `p` 指向对象数组 `stud` 的首元素,然后在 `for` 循环中通过 `(p+i)->display()` 显示第1、3、5名学生信息。需要注意的是,因为 `p` 是 `Student` 类型的指针,所以要使用箭头运算符 `->` 来访问 `display()` 函数。
QT 如何禁止鼠标指针跟随系统缩放
要禁止QT应用程序中鼠标指针跟随系统缩放,可以通过以下步骤实现:
1. 在应用程序的`main`函数中,使用`QApplication::setOverrideCursor`函数设置应用程序的全局鼠标指针。
```cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 设置全局鼠标指针为箭头光标
a.setOverrideCursor(QCursor(Qt::ArrowCursor));
// 其他初始化操作
return a.exec();
}
```
这将设置应用程序的全局鼠标指针为箭头光标,无论系统的缩放设置如何,鼠标指针都将保持不变。
2. 可以选择性地在需要自定义鼠标指针的窗口或小部件中,使用`setCursor`函数设置特定的鼠标指针样式。
```cpp
// 在窗口或小部件的构造函数或其他适当的位置调用
setCursor(QCursor(Qt::ArrowCursor));
```
这样做将覆盖全局设置,确保特定窗口或小部件中的鼠标指针始终为指定的样式(在此示例中为箭头光标)。
通过以上步骤,您可以禁止QT应用程序中鼠标指针跟随系统缩放,并始终保持指定的鼠标指针样式。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)