在C中为数组分配多个值

时间:2021-10-11 21:41:52

Is there any way to do this in a condensed form?

有没有办法以浓缩形式这样做?

GLfloat coordinates[8];
...
coordinates[0] = 1.0f;
coordinates[1] = 0.0f;
coordinates[2] = 1.0f;
coordinates[3] = 1.0f;
coordinates[4] = 0.0f;
coordinates[5] = 1.0f;
coordinates[6] = 0.0f;
coordinates[7] = 0.0f;
return coordinates;

Something like coordinates = {1.0f, ...};?

像coordinates = {1.0f,...};?

7 个解决方案

#1


21  

If you really to assign values (as opposed to initialize), you can do it like this:

如果你真的要分配值(而不是初始化),你可以这样做:

 GLfloat coordinates[8]; 
 static const GLfloat coordinates_defaults[8] = {1.0f, 0.0f, 1.0f ....};
 ... 
 memcpy(coordinates, coordinates_defaults, sizeof(coordinates_defaults));

 return coordinates; 

#2


8  

The old-school way:

老派方式:

GLfloat coordinates[8];
...

GLfloat *p = coordinates;
*p++ = 1.0f; *p++ = 0.0f; *p++ = 1.0f; *p++ = 1.0f;
*p++ = 0.0f; *p++ = 1.0f; *p++ = 0.0f; *p++ = 0.0f;

return coordinates;

#3


6  

There's a trick to wrap the array into a struct (which can be initialized after declaration).

有一个技巧可以将数组包装成一个结构(可以在声明后初始化)。

ie.

即。

struct foo {
  GLfloat arr[10];
};
...
struct foo foo;
foo = (struct foo) { .arr = {1.0, ... } };

#4


3  

Exactly, you nearly got it:

确实,你几乎得到了它:

GLfloat coordinates[8] = {1.0f, ..., 0.0f};

#5


3  

You can use:

您可以使用:

GLfloat coordinates[8] = {1.0f, ..., 0.0f};

but this is a compile-time initialisation - you can't use that method in the current standard to re-initialise (although I think there are ways to do it in the upcoming standard, which may not immediately help you).

但这是一个编译时初始化 - 您不能在当前标准中使用该方法重新初始化(尽管我认为在即将推出的标准中有一些方法可以做到这一点,这可能无法立即帮助您)。

The other two ways that spring to mind are to blat the contents if they're fixed:

如果它们被修复,另外两种让人想起的方法是抨击内容:

GLfloat base_coordinates[8] = {1.0f, ..., 0.0f};
GLfloat coordinates[8];
:
memcpy (coordinates, base_coordinates, sizeof (coordinates));

or provide a function that looks like your initialisation code anyway:

或者提供一个看起来像你的初始化代码的函数:

void setCoords (float *p0, float p1, ..., float p8) {
    p0[0] = p1; p0[1] = p2; p0[2] = p3; p0[3] = p4;
    p0[4] = p5; p0[5] = p6; p0[6] = p7; p0[7] = p8;
}
:
setCoords (coordinates, 1.0f, ..., 0.0f);

keeping in mind those ellipses (...) are placeholders, not things to literally insert in the code.

请记住,这些省略号(...)是占位符,而不是字面上插入代码中的东西。

#6


0  

If you are doing these same assignments a lot in your program and want a shortcut, the most straightforward solution might be to just add a function

如果你在程序中做了很多同样的任务并想要一个快捷方式,那么最简单的解决方案可能就是添加一个函数

static inline void set_coordinates(
        GLfloat coordinates[static 8],
        GLfloat c0, GLfloat c1, GLfloat c2, GLfloat c3,
        GLfloat c4, GLfloat c5, GLfloat c6, GLfloat c7)
{
    coordinates[0] = c0;
    coordinates[1] = c1;
    coordinates[2] = c2;
    coordinates[3] = c3;
    coordinates[4] = c4;
    coordinates[5] = c5;
    coordinates[6] = c6;
    coordinates[7] = c7;
}

and then simply call

然后简单地打电话

GLfloat coordinates[8];
// ...
set_coordinates(coordinates, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f);

#7


-1  

typedef struct{
  char array[4];
}my_array;

my_array array = { .array = {1,1,1,1} }; // initialisation

void assign(my_array a)
{
  array.array[0] = a.array[0];
  array.array[1] = a.array[1];
  array.array[2] = a.array[2];
  array.array[3] = a.array[3]; 
}

char num = 5;
char ber = 6;

int main(void)
{
  printf("%d\n", array.array[0]);
// ...

  // this works even after initialisation
  assign((my_array){ .array = {num,ber,num,ber} });

  printf("%d\n", array.array[0]);
// ....
  return 0;
}

#1


21  

If you really to assign values (as opposed to initialize), you can do it like this:

如果你真的要分配值(而不是初始化),你可以这样做:

 GLfloat coordinates[8]; 
 static const GLfloat coordinates_defaults[8] = {1.0f, 0.0f, 1.0f ....};
 ... 
 memcpy(coordinates, coordinates_defaults, sizeof(coordinates_defaults));

 return coordinates; 

#2


8  

The old-school way:

老派方式:

GLfloat coordinates[8];
...

GLfloat *p = coordinates;
*p++ = 1.0f; *p++ = 0.0f; *p++ = 1.0f; *p++ = 1.0f;
*p++ = 0.0f; *p++ = 1.0f; *p++ = 0.0f; *p++ = 0.0f;

return coordinates;

#3


6  

There's a trick to wrap the array into a struct (which can be initialized after declaration).

有一个技巧可以将数组包装成一个结构(可以在声明后初始化)。

ie.

即。

struct foo {
  GLfloat arr[10];
};
...
struct foo foo;
foo = (struct foo) { .arr = {1.0, ... } };

#4


3  

Exactly, you nearly got it:

确实,你几乎得到了它:

GLfloat coordinates[8] = {1.0f, ..., 0.0f};

#5


3  

You can use:

您可以使用:

GLfloat coordinates[8] = {1.0f, ..., 0.0f};

but this is a compile-time initialisation - you can't use that method in the current standard to re-initialise (although I think there are ways to do it in the upcoming standard, which may not immediately help you).

但这是一个编译时初始化 - 您不能在当前标准中使用该方法重新初始化(尽管我认为在即将推出的标准中有一些方法可以做到这一点,这可能无法立即帮助您)。

The other two ways that spring to mind are to blat the contents if they're fixed:

如果它们被修复,另外两种让人想起的方法是抨击内容:

GLfloat base_coordinates[8] = {1.0f, ..., 0.0f};
GLfloat coordinates[8];
:
memcpy (coordinates, base_coordinates, sizeof (coordinates));

or provide a function that looks like your initialisation code anyway:

或者提供一个看起来像你的初始化代码的函数:

void setCoords (float *p0, float p1, ..., float p8) {
    p0[0] = p1; p0[1] = p2; p0[2] = p3; p0[3] = p4;
    p0[4] = p5; p0[5] = p6; p0[6] = p7; p0[7] = p8;
}
:
setCoords (coordinates, 1.0f, ..., 0.0f);

keeping in mind those ellipses (...) are placeholders, not things to literally insert in the code.

请记住,这些省略号(...)是占位符,而不是字面上插入代码中的东西。

#6


0  

If you are doing these same assignments a lot in your program and want a shortcut, the most straightforward solution might be to just add a function

如果你在程序中做了很多同样的任务并想要一个快捷方式,那么最简单的解决方案可能就是添加一个函数

static inline void set_coordinates(
        GLfloat coordinates[static 8],
        GLfloat c0, GLfloat c1, GLfloat c2, GLfloat c3,
        GLfloat c4, GLfloat c5, GLfloat c6, GLfloat c7)
{
    coordinates[0] = c0;
    coordinates[1] = c1;
    coordinates[2] = c2;
    coordinates[3] = c3;
    coordinates[4] = c4;
    coordinates[5] = c5;
    coordinates[6] = c6;
    coordinates[7] = c7;
}

and then simply call

然后简单地打电话

GLfloat coordinates[8];
// ...
set_coordinates(coordinates, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f);

#7


-1  

typedef struct{
  char array[4];
}my_array;

my_array array = { .array = {1,1,1,1} }; // initialisation

void assign(my_array a)
{
  array.array[0] = a.array[0];
  array.array[1] = a.array[1];
  array.array[2] = a.array[2];
  array.array[3] = a.array[3]; 
}

char num = 5;
char ber = 6;

int main(void)
{
  printf("%d\n", array.array[0]);
// ...

  // this works even after initialisation
  assign((my_array){ .array = {num,ber,num,ber} });

  printf("%d\n", array.array[0]);
// ....
  return 0;
}