0

I have a structure and wish to print all member variable like an array.

The struct was like

#pragma pack(push)     /* push current alignment to stack */ 
#pragma pack(1)     /* set alignment to 1 boundary */ 
struct testStruct { 
    int a = 6; 
    int b[5] = {1,2,3,4,5}; 
} testStruct1 ;
#pragma pack(pop)     /* restore original alignment from stack */

I am trying stuff like

for(int i = 0; i < 6; i++)
{
    printf("%d, ", testStruct1 + i);
}

This cannot compile. I'm not willing to declare a new array to memcpy all member in it.

I wish to see

6, 1, 2, 3, 4, 5, 

Is there any way to do so??? Thanks

4
  • printf( "%d, ", teststruct1.b[i] ); Commented Sep 4, 2020 at 10:29
  • Write your own printX() or write a loop running over the array. printf() does not know how to print arrays. Commented Sep 4, 2020 at 10:30
  • This is not a correct way to initialise the struct varianle Commented Sep 4, 2020 at 10:33
  • testStruct1 + i is incorrect since a) testStruct1 isn't even a pointer to a struct and b) Pointers to structs would also not work that way . Commented Sep 4, 2020 at 10:39

2 Answers 2

2

It sounds like you wish to access the same variables through different types and names. C allows you to do that through "union type punning", like this:

#include <stdio.h>

typedef union
{
  struct  // standard C anonymous struct
  {
    int a; 
    int b[5];
  };
  int array [6];
} testArray;

int main (void)
{
  testArray test = { .a = 6, .b={1,2,3,4,5} };
  
  for(int i=0; i<6; i++)
  {
    printf("%d ", test.array[i]);
  }
  
  return 0;
}

Output:

6 1 2 3 4 5

Packing isn't necessary in this case since it's all aligned int variables.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, the code testArray test = { .a = 6, .b={1,2,3,4,5} }; only works on c, how can I do it in c++
@Olly Initialize it as you would any struct or array? In C++ you can also do it from the constructor initializer list. However you can't do type punning in C++, it is undefined behavior there. Supposedly because C++ hates being useful for hardware-related programming...
0

If you want to use pragamas that is fine, but they have no influence on your question.

I believe the syntax you want is:

struct testStruct { 
    int a; 
    int b[5]; 
} testStruct1 = {6,1,2,3,4,5};

and you can print it like:

printf("%d, %d, %d, %d, %d, %d\n",
    testStruct1.a,
    testStruct1.b[0];
    testStruct1.b[1],
    testStruct1.b[2],
    testStruct1.b[3],
    testStruct1.b[4]);

And please remove all your redundant empty lines.

3 Comments

Or OP may want to use a loop, if this code is just an example and he actually wants to use it on a seriously-sized array.
@APJo, of course it is just an example. A loop would be quite simple to implement. Maybe he can think hard and do it himself?
Absolutely, I just posted this so he would think of it. Alternatively he could use struct pointers as long as he knows what he's doing

Your Answer

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.