erDaren
V2EX  ›  问与答

如果用包含柔性数组的结构体组成一个数组,那么应该怎么声明与定义?

  •  
  •   erDaren · Oct 21, 2015 · 1776 views
    This topic created in 3941 days ago, the information mentioned may be changed or developed.

    像这样吗?还有没有别的更好的方法
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct _user{
        int total_score;
        int pefect_score;
        int scores[];
    }User;
    
    int main()
    {
        int user_num, problem_num;
        scanf("%d %d", &user_num, &problem_num);
        User* users = (User*)malloc(user_num * (sizeof(User) + problem_num*sizeof(int)));
    
        return 0;
    }
    
    Supplement 1  ·  Oct 21, 2015
    忘说了,我用的 C
    1 replies    2015-10-21 09:22:22 +08:00
    MCVector
        1
    MCVector  
       Oct 21, 2015
    如果是 C++ 的话,我会在 struct 里 加 con/destructor. C 的话我想能不能在外面实现一个 con/desstructor.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1000 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:40 · PVG 06:40 · LAX 15:40 · JFK 18:40
    ♥ Do have faith in what you're doing.