简体   繁体   中英

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. 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 * ?).

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. What's the point of making it an array, if there is only one element?

Seems work,

Maybe setstate need work.

#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. Instead you should have

*(driver->flags[flag]) = 1;

But then, you need to have a valid memory for flags to point to. So you need to malloc() flags.

driver->flags = (uint8_t(*)[])malloc(5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM