[英]how can I assign a pointer to an array inside a struct?
typedef struct
{
uint8_t (*flags)[5];
} type_t;
void setstate(type_t* driver, uint8_t flag)
{
driver->flags[flag] = 1;
}
void printall(type_t* driver)
{
for (int a = 0; a < 5; a++)
{
printf("%d\n", driver->flags[a]);
}
}
int main(int argc, char** argv)
{
static type_t driver[1];
uint8_t (*states)[5] = { 0 };
driver->flags = states;
setstate(driver, 2);
printall(driver);
}
I wanted to assign a local array pointer to an array pointer inside a struct but I cant seem to get this to work.. thanks.我想将本地数组指针分配给结构内的数组指针,但我似乎无法让它工作..谢谢。
your local variable states
is also a pointer to an array.您的局部变量
states
也是指向数组的指针。 I think you meant to declare an actual array?我认为您的意思是声明一个实际的数组? And then assign a pointer to that array to the field in the structure?
然后将指向该数组的指针分配给结构中的字段? That's what the text said, anyway.
反正文是这么说的。 The way your initializer is written, with 0 instead of NULL and with extra { } around it, I think you are thinking that you did declare an array (so what's with the
*
?).您的初始化程序的编写方式,使用 0 而不是 NULL 并带有额外的 { } ,我认为您认为您确实声明了一个数组(那么
*
是什么?)。
uint8_t states[8];
Now you normally don't declare a pointer to a whole array, but a pointer to the same type as the array element.现在您通常不声明指向整个数组的指针,而是声明指向与数组元素相同类型的指针。 So the structure field would be:
所以结构字段将是:
uint8_t* flags; // points to first of 8 consecutive values
then you can write然后你可以写
driver->flags= states;
and it will mean that.这将意味着。
Though driver
being an array of 1, it is very strange to refer to it as a pointer like that.尽管
driver
是一个 1 的数组,但将其称为像这样的指针是很奇怪的。 What's the point of making it an array, if there is only one element?如果只有一个元素,那么将其设为数组有什么意义?
Seems work,似乎工作,
Maybe setstate need work.也许 setstate 需要工作。
#include<stdlib.h>
#include<stdio.h>
#include<stdint.h>
typedef struct _type
{
uint8_t * flags[5];
uint8_t * flag;
uint8_t data[5];
} type_t;
void m_setstate(type_t* driver, uint8_t flag)
{
*driver->flags[flag] = 1;
}
void printall(type_t* driver)
{
for (int a = 0; a < 5; a++)
{
if( driver->flags[a] != NULL )
{
printf("INIT DONE %p FIRST VALUE %d \n", driver->flags[a], driver->flags[a][0]);
}
else
{
printf("NULL >> %p\n", driver->flags);
}
}
}
int main(int argc, char** argv)
{
static type_t driver = { 0 };
uint8_t case0_states[5] = { 5, 0 };
uint8_t case1_states[4] = { 4, 0 };
uint8_t case2_states[3] = { 3, 0 };
uint8_t case3_states[2] = { 2, 0 };
uint8_t case4_states[1] = { 1 };
driver.flag = &(case0_states[0]);
driver.data[0] = 3;
printf("DEBUG\n");
printall(&driver);
printf("DEBUG\n");
driver.flags[0] = (uint8_t*) &(case0_states[0]);
driver.flags[1] = (uint8_t*) &(case1_states[0]);
driver.flags[2] = (uint8_t*) &(case2_states[0]);
driver.flags[3] = (uint8_t*) &(case3_states[0]);
driver.flags[4] = (uint8_t*) &(case4_states[0]);
printall(&driver);
m_setstate(&driver, 2);
printall(&driver);
}
And result:结果:
koala@K-desktop:~/Workspace/koala-home/15-Stack/01-20210310$ gcc -o hello.exe hello.c
koala@K-desktop:~/Workspace/koala-home/15-Stack/01-20210310$ ./hello.exe
DEBUG
NULL >> 0x559eeef4e040
NULL >> 0x559eeef4e040
NULL >> 0x559eeef4e040
NULL >> 0x559eeef4e040
NULL >> 0x559eeef4e040
DEBUG
INIT DONE 0x7ffeb3406143 FIRST VALUE 5
INIT DONE 0x7ffeb340613f FIRST VALUE 4
INIT DONE 0x7ffeb340613c FIRST VALUE 3
INIT DONE 0x7ffeb340613a FIRST VALUE 2
INIT DONE 0x7ffeb3406139 FIRST VALUE 1
INIT DONE 0x7ffeb3406143 FIRST VALUE 5
INIT DONE 0x7ffeb340613f FIRST VALUE 4
INIT DONE 0x7ffeb340613c FIRST VALUE 1
INIT DONE 0x7ffeb340613a FIRST VALUE 2
INIT DONE 0x7ffeb3406139 FIRST VALUE 1
The problem is this statement问题是这个声明
driver->flags[flag] = 1;
You are assigning 1 to an address.您正在将 1 分配给一个地址。 Instead you should have
相反,你应该有
*(driver->flags[flag]) = 1;
But then, you need to have a valid memory for flags to point to.但是,您需要有一个有效的 memory 才能指向标志。 So you need to malloc() flags.
所以你需要 malloc() 标志。
driver->flags = (uint8_t(*)[])malloc(5);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.