Simple C array declaration / assignment question

后端 未结 9 1876
醉话见心
醉话见心 2021-01-05 06:13

In higher level languages I would be able something similar to this example in C and it would be fine. However, when I compile this C example it complains bitterly. How can

相关标签:
9条回答
  • 2021-01-05 06:37

    It is also possible to hide the memcpy by using the compiler's block copy of structs. It makes the code ugly because of all the .i and i: but maybe it solves your specific problem.

    typedef struct {
        int i[3];
    } inta;
    
    int main()
    {
        inta d = {i:{1, 2, 3}};
    
        if (1)
            d = (inta){i:{4, 5, 6}};
    
        printf("%d %d %d\n", d.i[0], d.i[1], d.i[2]);
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-05 06:41

    you can declare static array with data to initialize from:

    static int initvalues[3] = {1,2,3};
    …
    if(1)
        memmove(values,initvalues,sizeof(values));
    
    0 讨论(0)
  • 2021-01-05 06:47
     //compile time initialization
     int values[3] = {1,2,3};
    
    //run time assignment
     value[0] = 1;
     value[1] = 2;
     value[2] = 3;
    
    0 讨论(0)
  • 2021-01-05 06:51

    There is also this... :)

    char S[16]="";
    strncpy(S,"Zoodlewurdle...",sizeof(S)-1);
    

    Test what happens if you declare S[8] or S[32], to see why this is so effective.

    I wrote my own string functions based on the logic of OpenBSD's strlcpy, aimed at ensuring a terminator byte MUST exist in the event of overflow, and standard strncpy won't do this so you have to watch carefully how you use it.

    The method above is effective because the ="" at declaration ensures 0 bytes throughout, and sizeof(S)-1 ensures that if you overdo the quoted string passed to strncpy, you get truncation and no violation of the last 0 byte, so this is safe against overflow now, AND on accessing the string later. I aimed this at ANSI C so it ought to be safe anywhere.

    0 讨论(0)
  • 2021-01-05 06:53

    I would post this as a comment, but I don't have enough reputation. Another (perhaps dirty) way of initializing an array is to wrap it in a struct.

    #include <stdio.h>
    
    struct wrapper { int array[3]; };
    
    int main(){
        struct wrapper a;
        struct wrapper b = {{1, 2, 3}};
    
        a = b;
    
        printf("%i %i %i", a.array[0], a.array[1], a.array[2]);
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-05 06:57

    You can only do multiple assignment of the array, when you declare the array:

    int values[3] = {1,2,3};
    

    After declaration, you'll have to assign each value individually, i.e.

    if (1) 
    {
      values[0] = 1;
      values[1] = 2;
      values[2] = 3;
    }
    

    Or you could use a loop, depending on what values you want to use.

    if (1)
    {
      for (i = 0 ; i < 3 ; i++)
      { 
        values[i] = i+1;
      }
    }
    
    0 讨论(0)
提交回复
热议问题