I declared an array of struct and initialized it at compile time.
我声明了一个struct数组并在编译时初始化它。
Now, for unit testing purposes, I would like to initialize it from a function which I can call from main() and from my unit tests.
现在,出于单元测试的目的,我想从一个函数初始化它,我可以从main()和单元测试中调用它。
For some reason, probably involving a 16 hour coding marathons & exhaustion, I can't figure it out.
出于某种原因,可能涉及16小时的编码马拉松和疲惫,我无法弄明白。
3 个解决方案
#1
3
So assuming you have
所以假设你有
struct foo {
int a;
int b;
};
struct foo foo_array[5] = {
{ 0, 0 }, { 1, 1 }, { 2, 2 }
};
int main() {
memcpy(foo_array, some_stuff, sizeof(foo_array)); // should work
...
OR you could:
或者你可以:
int main() {
int i;
for ( i = 0; i < sizeof(foo_array)/sizeof(struct foo); i++ ) {
init(&foo_array[i]);
}
}
but without looking at your code it's hard to say what's causing the trouble... i am SURE it's probably something very trivial you are overlooking because you are tired and have been at it for 16 hours.
但是没有看你的代码,很难说是什么导致了麻烦......我确定你可能会忽略一些非常微不足道的东西,因为你已经累了16个小时。
#2
1
typedef struct {
int ia;
char * pc;
} St_t;
void stInit(St_t * pst) {
if (!pst)
return;
pst->ia = 1;
pst->pc = strdup("foo");
/* Assuming this function 'knows' the array has two elements,
we simply increment 'pst' to reference the next element. */
++ pst;
pst->ia = 2;
pst->pc = strdup("bar");
}
void foo(void) {
/* Declare 'st' and set it to zero(s)/NULL(s). */
St_t st[2] = {{0}, {0}};
/* Initialise 'st' during run-time from a function. */
stInit(st);
...
}
#3
0
see this one:
看到这一个:
struct Student
{
int rollNo;
float cgpa;
};
int main()
{
const int totalStudents=10;
Student studentsArray[totalStudents];
for(int currentIndex=0; currentIndex< totalStudents; currentIndex++)
{
printf("Enter Roll No for student # %d\n" , currentIndex+1);
scanf("%d\n", &studentsArray[currentIndex].rollNo);
printf("Enter CGPA for student # %d\n", currentIndex+1);
scanf("%d\n", &studentsArray[currentIndex].cgpa);
}
}
#1
3
So assuming you have
所以假设你有
struct foo {
int a;
int b;
};
struct foo foo_array[5] = {
{ 0, 0 }, { 1, 1 }, { 2, 2 }
};
int main() {
memcpy(foo_array, some_stuff, sizeof(foo_array)); // should work
...
OR you could:
或者你可以:
int main() {
int i;
for ( i = 0; i < sizeof(foo_array)/sizeof(struct foo); i++ ) {
init(&foo_array[i]);
}
}
but without looking at your code it's hard to say what's causing the trouble... i am SURE it's probably something very trivial you are overlooking because you are tired and have been at it for 16 hours.
但是没有看你的代码,很难说是什么导致了麻烦......我确定你可能会忽略一些非常微不足道的东西,因为你已经累了16个小时。
#2
1
typedef struct {
int ia;
char * pc;
} St_t;
void stInit(St_t * pst) {
if (!pst)
return;
pst->ia = 1;
pst->pc = strdup("foo");
/* Assuming this function 'knows' the array has two elements,
we simply increment 'pst' to reference the next element. */
++ pst;
pst->ia = 2;
pst->pc = strdup("bar");
}
void foo(void) {
/* Declare 'st' and set it to zero(s)/NULL(s). */
St_t st[2] = {{0}, {0}};
/* Initialise 'st' during run-time from a function. */
stInit(st);
...
}
#3
0
see this one:
看到这一个:
struct Student
{
int rollNo;
float cgpa;
};
int main()
{
const int totalStudents=10;
Student studentsArray[totalStudents];
for(int currentIndex=0; currentIndex< totalStudents; currentIndex++)
{
printf("Enter Roll No for student # %d\n" , currentIndex+1);
scanf("%d\n", &studentsArray[currentIndex].rollNo);
printf("Enter CGPA for student # %d\n", currentIndex+1);
scanf("%d\n", &studentsArray[currentIndex].cgpa);
}
}