0

I have a struct of char arrays defined as follows:

    struct SD_Data {
       char time[8];    
       char vac[4];     
       char vdc1[4];     
       char vdc2[4];     
       char freq1[4];   
       char freq2[4];    
       char freq3[4];    
       char error_status[4];  
       char run_status[10];  };

struct SD_Data sdData;

  // data is put into struct here...

I want to use a variable within a loop that corresponds with the size of each array.

Normally I can use:

size = sizeof(sdData.time);
size = sizeof(sdData.vac);
etc

Is there a way to replace the name with a pointer instead and increment the pointer as it gets the sizes of each array?

I have tried by defining a pointer to the start of the strut and then getting the sizeof the deferenced pointer but only gives the size of the pointer

int* numPtr = (int*)&sdData;
size = sizeof(*numPtr );

Thanks.

MXG123
  • 197
  • 1
  • 10
  • 3
    No, it’s not possible. Pointers don’t carry information about the items they point to – Sami Kuhmonen Dec 12 '18 at 13:38
  • 2
    @SamiKuhmonen Unless, it's a pointer to the array itself, is not it? – Sourav Ghosh Dec 12 '18 at 13:40
  • @SouravGhosh No, a pointer is a pointer. If it’s an array it has a specific size based on elements. But if you take a pointer to it it becomes just a pointer like any other. – Sami Kuhmonen Dec 12 '18 at 13:41
  • 1
    @SamiKuhmonen how about this? `int main(void) { int arr[4] = {0}; int (*p)[4] = &arr; printf("%zu\n", sizeof(*p)); return 0; }` – Sourav Ghosh Dec 12 '18 at 13:42
  • 2
    What is the actual problem you are trying to solve? Calculate the size of the whole struct minus padding, or what? – Lundin Dec 12 '18 at 13:48
  • Looking to loop through and update data. I was using pointer2 - pointer2 to get the data size. worked ok until I got to the last one as there is no plus one in the struct. I would prefer to use sizeof. – MXG123 Dec 12 '18 at 14:01
  • 2
    Note that you cannot access contents of the structure with `numPtr` without breaking strict aliasing rules. That aside, this seems like an XY problem. It's unclear what you are really trying to do, but it sounds like your approach is not what you should be doing. – user694733 Dec 12 '18 at 14:10
  • The sizes in the brackets are in the declaration. Is it possible to reference those? – MXG123 Dec 12 '18 at 14:10
  • @MXG123 Perhaps, it depends on what you are actually doing, or wanting to do. e.g. if you want to pass this to a function: `foo(sd.freq)` , you instead have to also pass the size explicittly. `foo(sd.freq, sizeof sd.freq)`. Please tell us why you need a pointer, and what you actually need to do - perhaps there is a way. – nos Dec 12 '18 at 14:55
  • I want to add a comma 0x2C to the last element of each and 0x0D,0x0A to the very last one as this will make up a csv file. This will be done using a nested for loop but because the arrays are different sizes the inner loop count needs to be determined. I could use a list of the sizes but it will grow and be added to later. I just want to be able to update the struct and nothing else. – MXG123 Dec 12 '18 at 15:52
  • https://stackoverflow.com/questions/3553296/sizeof-single-struct-member-in-c – Juraj Dec 13 '18 at 10:37
  • You will have to turn the data into a string anyway, so use strlen(). So add one byte more for each segment and '\0' char at the end. Thus you can use `strlen()` – Hairi Dec 20 '18 at 16:34

0 Answers0