c++ - 使用结构指针显示一维数组
问题描述
我正在使用指向结构数组的指针来显示该数组的内容。代码编译没有错误,但输出不正确。
我尝试使用以下格式
*(ptr).re
,*(ptr)[j]
.re 或仅(*ptr)
.re 来查看它们中的任何一个是否显示输入值
struct structure //creating struct called structure to contain the real
and imaginary parts of a complex vector
{
float Re; //Data type float for Real part called Re
float Im; //Data type float for Imaginary part called Im
};
/*simple function for inputing user difined Re and Im values and storing them using a pointer to the sturct variable in the main function*/
void extract_Re_and_Im(structure *complex)
{
printf("please enter the real number\n"); //this stores the real part
scanf("%i",&(*complex).Re);
printf("please enter the Imaginary number\n");//this stores the Imaginary part
scanf("%i",&(*complex).Im);
}
/*function with a return of pointer of data type sturcture.the function should store multiple complex vectors*/
structure *extract_array_of_Re_and_Im(structure*complex,int size_of_array,int i)
{
structure complex_1[size_of_array];
i++; //this is a static variable in main
extract_Re_and_Im(complex); //this function allows user to input the complex vector and stores it into a varible in the function main by using a pointer
complex_1[i].Re=(*complex).Re;
complex_1[i].Im=(*complex).Im;
return complex_1;
}
int main()
{
const int SIZE=9;//creating SIZE this specifies how many complex vectors the program holds
for(i;i<SIZE;i++)//a loop used to allow user to enter all the complex vectors
{
extract_array_of_Re_and_Im(&complex_number,SIZE,i); //a function that creates a 1-D matrix of data type structure for storing user entered complex vectors
}
Ptr_for_complex=extract_array_of_Re_and_Im(&complex_number,SIZE,i);
//this stores the memory address thats returned by the function, the addr is for the 1-D matrix of data type structure
printf("everything is ok\n"); //just a failure checker
for(int j=0;j<SIZE;j++) //this is a loop to display the user inputed data in the correct format N + J M
{
printf("your Re and Im numbers are %.2f and J%.2f\n",Ptr_for_complex[j].Re,Ptr_for_complex[j].Im);
//this should display the contents of the structure array
}
}
我预计: 10 + J10 9 + J9 。. . 1 + J 1 但在 main 中返回 0 时一无所获并且出现非 0 错误
解决方案
这段代码是错误的
structure *extract_array_of_Re_and_Im(structure*complex,int size_of_array,int i)
{
structure complex_1[size_of_array];
i++; //this is a static variable in main
extract_Re_and_Im(complex); //this function allows user to input the complex vector and stores it into a varible in the function main by using a pointer
complex_1[i].Re=(*complex).Re;
complex_1[i].Im=(*complex).Im;
return complex_1;
}
有两种不同的方式是错误的。首先,它不是合法的 C++,因为在 C++ 中,数组大小必须是编译时常量。在您的代码size_of_array
中是一个变量。
其次,更严重的是,该函数返回一个指向数组的指针,complex_1
但该数组在函数退出后不再存在。所以在这段代码中
Ptr_for_complex=extract_array_of_Re_and_Im(&complex_number,SIZE,i);
Ptr_for_complex
指向一个不再存在的数组。
整个代码真的很混乱,而且太复杂了。您正在尝试做的事情并不需要所有这些复杂性。
这是正确执行此操作的一种方法
void extract_Re_and_Im(structure *complex)
{
printf("please enter the real number\n"); //this stores the real part
scanf("%i", &complex->Re);
printf("please enter the Imaginary number\n");//this stores the Imaginary part
scanf("%i", &complex->Im);
}
int main()
{
const int SIZE=9;
structure complex_array[SIZE];
for (int i = 0; i < SIZE; ++i)
extract_Re_and_Im(&complex_array[i]);
for (int j = 0; j < SIZE; j++)
printf("your Re and Im numbers are %.2f and J%.2f\n", complex_array[j].Re, complex_array[j].Im);
}
推荐阅读
- reactjs - 与 React 一起使用时 Apollo 重新获取
- c# - 映射空值导致默认类型值(Automapper ADO.NET)
- ruby-on-rails - Rails 范围内的正则表达式未捕获正确的值
- spring-security - Keycloak - 将客户端登录限制为组成员
- node.js - 通过 webhook 将 basecamp 3 与 Mavenlink 集成?
- reactjs - Keycloak - 保护 Spring Boot 应用程序
- c - 如何通过喜欢的列表编写出队的功能出队
- html - 如何防止延迟样式加载每个页面加载而不是第一次加载?
- google-app-maker - AppMaker - 仅在选项卡中显示过滤的项目
- javascript - 尝试访问 JSON 数组中的密钥