簡體   English   中英

如何將指針分配給結構內的數組?

[英]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);
}

我想將本地數組指針分配給結構內的數組指針,但我似乎無法讓它工作..謝謝。

您的局部變量states也是指向數組的指針。 我認為您的意思是聲明一個實際的數組? 然后將指向該數組的指針分配給結構中的字段? 反正文是這么說的。 您的初始化程序的編寫方式,使用 0 而不是 NULL 並帶有額外的 { } ,我認為您認為您確實聲明了一個數組(那么*是什么?)。

uint8_t states[8];

現在您通常不聲明指向整個數組的指針,而是聲明指向與數組元素相同類型的指針。 所以結構字段將是:

uint8_t* flags;  // points to first of 8 consecutive values

然后你可以寫

driver->flags= states;

這將意味着。

盡管driver是一個 1 的數組,但將其稱為像這樣的指針是很奇怪的。 如果只有一個元素,那么將其設為數組有什么意義?

似乎工作,

也許 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);
}

結果:

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 

問題是這個聲明

driver->flags[flag] = 1;

您正在將 1 分配給一個地址。 相反,你應該有

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

但是,您需要有一個有效的 memory 才能指向標志。 所以你需要 malloc() 標志。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM