zoukankan      html  css  js  c++  java
  • struct和union分析实例

    1.#include <stdio.h>
    #include <malloc.h>

    typedef struct _soft_array
    {
        int len;
        int array[];
    }SoftArray;

    int main()
    {  
        int i = 0;
        SoftArray* sa = (SoftArray*)malloc(sizeof(SoftArray) + sizeof(int) * 10);
        
        sa->len = 10;
        
        for(i=0; i<sa->len; i++)
        {
            sa->array[i] = i + 1;
        }
        
        for(i=0; i<sa->len; i++)
        {
            printf("%d ", sa->array[i]);   
        }
        
        free(sa);
        
        return 0;
    }

    2.#include <stdio.h>
    #include <malloc.h>

    typedef struct _soft_array
    {
        int len;
        int array[];
    }SoftArray;

    SoftArray* create_soft_array(int size)
    {
        SoftArray* ret = NULL;
        
        if( size > 0 )
        {
            ret = (SoftArray*)malloc(sizeof(*ret) + sizeof(*(ret->array)) * size);
            
            ret->len = size;
        }
        
        return ret;
    }

    void fac(SoftArray* sa)
    {
        int i = 0;
        
        if( NULL != sa )
        {
            if( 1 == sa->len )
            {
               sa->array[0] = 1;
            }
            else
            {
                sa->array[0] = 1;
                sa->array[1] = 1;
                
                for(i=2; i<sa->len; i++)
                {
                    sa->array[i] = sa->array[i-1] + sa->array[i-2];
                }
            }
        }
    }

    void delete_soft_array(SoftArray* sa)
    {
        free(sa);
    }

    int main()
    {
        int i = 0;
        SoftArray* sa = create_soft_array(10);
        
        fac(sa);
        
        for(i=0; i<sa->len; i++)
        {
            printf("%d ", sa->array[i]);
        }
        
        delete_soft_array(sa);
        
        return 0;
    }

  • 相关阅读:
    light oj 1105 规律
    light oj 1071 dp(吃金币升级版)
    light oj 1084 线性dp
    light oj 1079 01背包
    light oj 1068 数位dp
    light oj 1219 树上贪心
    light oj 1057 状压dp TSP
    light oj 1037 状压dp
    矩阵快速幂3 k*n铺方格
    矩阵快速幂2 3*n铺方格
  • 原文地址:https://www.cnblogs.com/wxb20/p/6145881.html
Copyright © 2011-2022 走看看