Iterate elements name in c/c++ structure

Posted on 2011-10-06
Last Modified: 2012-06-21
Is there any known way to iterate through the element names of a structure at compile time to build a series of ?  

typedef struct
  char element1[50];
  char element2[50];
  char element3[50];

I'd like to be able to "print" the entire structure to either a char or file stream like so,

myStruct.element1 => 'Value1'
               element2 => 'Value2'
               element3 => 'Value3'

Functionality similar to the PHP 'print_r' function.

I know that the names are not available at run time.  I have built a macro that is close

#define __dump(a, x, y) fp_exception_log << "myStruct[" << x << "][" << y << "]." << #a << ": " << myStruct[x][y].a << std::endl

but I have to enter all the element names individually.  (__dump(element1, 0, 0)... )If I could iterate through the struct to get the names, that would be great as the struct I am dealling with is quite long and subject to changes frequently.

Don't know if this can be done or not, just though I'd float it out there and see if any one has cracked this nut before.

Question by:cvhoyt
    LVL 37

    Expert Comment

    I'm not sure what you mean. The structure of the struct itself changes? What I would do is make an element struct and have it contain the name and the element

    struct Element
      char name[50];
      char value[50];

    #define NUM_ELEMENTS 5

    struct myStruct
      struct Element elements[NUM_ELEMENTS];

    Then you just use a for loop
    for(int i = 0; i < NUM_ELEMENTS; ++i)
      output myStruct.elements[i ].name //blah blah
    LVL 37

    Expert Comment

    If you insist on doing it the old way, you could use Excel to speed up the process of typing all the lines.

    Or just copy the insides of the struct to where the output lines need to be and use the find/replace feature of your IDE
    Replace '  char ' with printf( or whatever and '[50];' with the rest.
    Make sure to only do the replace in that selection of come.
    LVL 8

    Accepted Solution

    You cannot iterate thru the struct members as you do in case of an array.
    You will have to access each of them by their name and do the operation.

    Author Closing Comment

    Yes, I have come to the realization.  I'll have to do with my macro.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    TextBlock Stops Updating 2 48
    tenRun challenge 28 67
    scores100 challenge 3 68
    hasOne  challenge 59 65
    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now