简体   繁体   English

指向结构的指针

[英]Pointer to struct

I am working on assignment. 我正在做作业。 I started with the struct: 我从结构开始:

struct figures_struct
{
    char figure_name[130];
    double figure_coordinates[1000000];
};

When I read name of the figure from the file, I stored it into the struct as follows: 当我从文件中读取图形名称时,将其存储到结构中,如下所示:

strcpy(figures[i].figure_name,f_name);

Now I have to modify my code and need to use dynamic memory. 现在,我必须修改代码并需要使用动态内存。 I did: 我做了:

struct figures_struct
{
    char figure_name[130];
    double figure_coordinates[100000];
};

struct figures_struct *figures = malloc(size * sizeof(struct figures_struct));

Now, how do I store figure name into my struct? 现在,如何将图形名称存储到结构中? figures[i].figure_name does not seem to be working. figures[i].figure_name似乎不起作用。

strcpy(figures[i].figure_name, f_name);

仍然可以使用指针(因为指针和数组在C中几乎可以互换)

只要“ i”小于“大小”,它将立即起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM