0
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.

asked Mar 10, 2021 at 19:42
3
  • Why are you declaring a pointer like that? Length-"limited" pointers are pretty useless. Commented Mar 10, 2021 at 19:42
  • 2
    What is the exact statement that you are attempting, and exactly where is it? Please don't ask us to "guess." Commented Mar 10, 2021 at 19:43
  • all sorted, thanks guys. Commented Mar 11, 2021 at 9:09

3 Answers 3

2

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?

answered Mar 10, 2021 at 19:46
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, yes that makes sense. the point of making it a fixed size array is to avoid using malloc since this will need to be misra compliant, also this is just a small example of the real implementation.
0

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 
answered Mar 10, 2021 at 20:18

Comments

0

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);
answered Mar 10, 2021 at 22:44

2 Comments

I was trying to put a pointer to each array element in a 'mirror' array inside the struct, but as JDługosz points out, all I need is just a pointer of the same type to the first item of the array. Nothing wrong in having a one dimentional array, and avoid using malloc() altogether, memory is allocated the same way, being the difference one is defined at compile time, the other at runtime. thanks!
I was trying to put a pointer to each array element in a 'mirror' array inside the struct oh, that would be (uint8_t*)flags[8]; that is an array of 8 pointers. You wrote a pointer to an array of 8 integers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.