I am using a pointer to a struct array inorder to display the content of that array. The code compiles without errors but the output is incorrect.
i have tried using the following formats
*(ptr).re
,*(ptr)[j]
.re or just(*ptr)
.re to see if any of them displays of inputed values
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
}
}
i expected: 10 + J10 9 + J9 . . . 1 + J 1 but got nothing and a non 0 error for the return 0 in main
最佳答案
这段代码是错误的
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);
}
关于c++ - 使用结构体指针显示一维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57338684/