Can one (re)set all the values of an array in one line (after it has been initialized)?

前端 未结 4 2195
不思量自难忘°
不思量自难忘° 2021-02-19 13:45

In C, I know I can make an array like this

int myarray[5] = {a,b,c,d,e};

However, imagine the array was already initialised like



        
4条回答
  •  無奈伤痛
    2021-02-19 14:07

    #include
    #include
    
    int *setarray(int *ar,char *str)
    {
        int offset,n,i=0;
        while (sscanf(str, " %d%n", &n, &offset)==1)
        {
            ar[i]=n;
            str+=offset;
            i+=1;
        }
        return ar;
    }
    
    int main()
    {
        int *sz=malloc(5*sizeof(int)),i;
    
        //call
        setarray(sz,"10 30");
    
        //output
        for(i=0;i<2;i++)
            printf("%d\n",sz[i]);
    
        return 0;
    }
    

提交回复
热议问题