c++ - 使用结构体指针显示一维数组

标签 c++ c

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/

相关文章:

c++ - 如何使用 Visual Studio 修复 "fatal error C1083: Cannot open include file"

c - 中止陷阱 6 : Freeing unallocated pointer

c - ARM Keil 错误 #17 预期为 "]"、C3910W : Old syntax

c - 动态二维数组,无内存

结合 uint8_t、uint16_t 和 uint8_t

c - 为什么我们在运行Perl脚本时需要使用chmod赋予可执行权限,而在运行c程序时却没有这样做?

c++ - 加载程序如何从动态模块分配/释放静态数据

c++ - 非局部均值降噪算法在图像处理中的实现

c++ - 如何理解 void (*&&)() 函数

c++ - 在线程中更改 Windows 窗体控件