可以在一行中(重新)设置数组的所有值(在初始化之后)吗?

发布于 2024-10-04 17:19:14 字数 504 浏览 6 评论 0原文

在C中,我知道我可以创建一个像这样的数组

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

但是,想象一下数组已经被初始化了

int myarray[5];

,然后在之后的某个时刻,我想设置/更改所有值而不用去

myarray[0] = a;
myarray[1] = b;
myarray[2] = c;
myarray[3] = d;
myarray[4] = e;

,而是更像是

myarray = {a,b,c,d,e};

我问的原因这是因为如果我在堆上声明我的数组,我将像这样初始化数组:

int* myarray = malloc(5*sizeof(int));

然后我希望能够在一行中输入所有值(主要是为了让我的代码看起来更干净)

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

int myarray[5];

and then at some point afterwards, I wanted to set/change all the values without going

myarray[0] = a;
myarray[1] = b;
myarray[2] = c;
myarray[3] = d;
myarray[4] = e;

but rather, something more like

myarray = {a,b,c,d,e};

The reason why I ask this is because if I declare my array on the heap, I will initialise the array like:

int* myarray = malloc(5*sizeof(int));

Then I would like to be able to enter in all the values in one line (mostly to make my code look cleaner)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

权谋诡计 2024-10-11 17:19:14
memcpy(myarray, (int [5]){a,b,c,d,e}, 5*sizeof(int));
memcpy(myarray, (int [5]){a,b,c,d,e}, 5*sizeof(int));
坦然微笑 2024-10-11 17:19:14

这是一个兼容所有标准(C89、C99、C++)的解决方案,

它的优点是您只需担心在一个地方输入数据。其他代码都不需要更改 - 没有神奇的数字。数组在堆上声明。数据表被声明为 const。

(单击此处尝试在键盘中运行它)

#include<stdio.h>
#include<stdlib.h>

int main()
{
unsigned int i = 0;
int *myarray = 0;
static const int  MYDATA[] = {11, 22, 33, 44, 55};

  myarray = (int*)malloc(sizeof(MYDATA));
  memcpy(myarray, MYDATA, sizeof(MYDATA));

  for(i = 0; i < sizeof(MYDATA)/sizeof(*MYDATA); ++i)  
  {
    printf("%i\n", myarray[i]);
  }

  free(myarray);

  return 0;
}

Here is a solution that is all standards compatible (C89, C99, C++)

It has the advantage that you only worry about entering the data in one place. None of the other code needs to change - there are no magic numbers. Array is declared on the heap. The data table is declared const.

(Click here to try running it in Codepad)

#include<stdio.h>
#include<stdlib.h>

int main()
{
unsigned int i = 0;
int *myarray = 0;
static const int  MYDATA[] = {11, 22, 33, 44, 55};

  myarray = (int*)malloc(sizeof(MYDATA));
  memcpy(myarray, MYDATA, sizeof(MYDATA));

  for(i = 0; i < sizeof(MYDATA)/sizeof(*MYDATA); ++i)  
  {
    printf("%i\n", myarray[i]);
  }

  free(myarray);

  return 0;
}
人事已非 2024-10-11 17:19:14

不,C 没有这样的功能。如果要将所有数组元素设置为相同的值,请使用 memset(3)

No, C doesn't have such feature. If you are setting all array elements to the same value use memset(3).

唔猫 2024-10-11 17:19:14
#include<stdio.h>
#include<stdlib.h>

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;
}
#include<stdio.h>
#include<stdlib.h>

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文