C Question Index

In my built-in c program, I have a structure:

struct var{
    unsigned long value;
    unsigned long length;

    + More
}

An array of these structures is used to store variables. Most of the stored variables are simply stored in the "value", so the length is 1.
However, some of these variables are arrays, and I'm trying to save the starting address in the "value".

unsigned long lookup[10];
variables[x].length = 10;

Then I'm not quite sure how to save the address ...

variables[x].value = lookup;
// lookup is a pointer so I cant put it into value

OR

variables[x].value = (unsigned long)lookup;
// value reads back through sprintf+uart as '536874692'
// which couldnt be a valid memory address!

I can just discard and add a pointer variable to the structure

EDIT:
I did not want to add a pointer to the structure because I would have to go back and rewrite the read / write functions of the flash memory to save the pointer. They are quite complex and currently work, so I don’t want to touch them!

+3
10

value, unsigned long, . ( , , , ... , 0x20000EC4... , 0x20000000, ?)

ints ( longs) "".

unsigned long *starting_address;

? . , , , unsigned long *starting_address, "unsigned long value", - , .

+5

union.

 struct var{
     union {
        unsigned long numeric;
        void *ptr;  
     } value;
     unsigned long length;

     + More
 }

, , , .

+6

, , unsigned long ( gaurantee, sizeof), . . .

struct var{
    unsigned short type;
    union {
        unsigned long i;
        void *p;
    } value;
    unsigned long length;

    + More
}

type.

+1

. , ?

struct var
{
   unsigned long *value;
   unsigned long length;

   unsigned long othervalue1;
   unsigned long othervalue2;
};

, malloc. ,

unsigned long value[50];
0

, sprintf + uart. ?

, . - sprintf.

. int, intptr_t, , .

0

, 1? .

0

, unsigned long *.

- :

struct var{
  union{
    unsigned long solo_value;
    unsigned long* multi_values;
  }

  unsigned long length;
  + More
};

, * , . , - , ... , , - sprintf. - .

* malloc() et. . 8 , .

0

- , :

typedef struct var{
     union {
        unsigned long numeric;
        void *ptr;  
     } value;
     unsigned long length;

     // + More
 } composition_t;

:

composition_t array[2];
unsigned long lookup[10];

// Just a plain unsigned long value
array[0].value.numeric = 100;
array[0].length= 1;

// An array of 10 long values
array[0].value.ptr= lookup;
array[x].length = 10;
0

, unsigned long , , , . , . , , . , , , , , -, ( , , ), .

0

offsetof? http://en.wikipedia.org/wiki/Offsetof

, , .

There is also a (macro) (google it) container that is created using offsetof, and this can also be useful.

0
source

Source: https://habr.com/ru/post/1713857/


All Articles