泌阳杨集刘庄吧 关注:1贴子:74
  • 1回复贴,共1

structure variable's initialization and assignment

只看楼主收藏回复

Initialization is different fromassignment. For structure variable, I can set its original value with a {}.
But this method is valid only once, and must be at the same time---when it is defined.
After it is defined, if I want to change one or more its members' value, I can't use the same method:{},
and mustn't do that. The only valid method is assign them one by one. Below is an example:
struct statistic gsta;
void echo_init(void)
{
gsta =
{
.nsend = 0,
.nrecv = 0,
...
};
}
very very bad!!! Don't do that like me again.


IP属地:广东1楼2017-07-16 11:24回复
    KEYWORD:__restrict__
    It can only be used with pointer, used by compiler for optimization. It tells the compiler that the memory location
    can only be accessed by the pointer.
    For instance:
    int *restrict a = (int*)malloc(9*sizeof(int));
    int arr[9] = {0};
    int *ptr = arr;
    void init(void)
    {
    a[3] = 2;
    ptr[4] += 5;
    a[3] += 9;
    ptr[4]+= 8;
    ...
    }
    can be optimized to:
    void init(void)
    {
    ptr[4] += 5;
    ptr[4] += 8;
    a[3] += 11;
    }
    That's what it is!


    IP属地:广东2楼2017-07-16 11:51
    回复