How to count in Preprocessor C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cillo
    New Member
    • Nov 2014
    • 2

    How to count in Preprocessor C

    I want to count values in a struct.
    Code:
    typedef struct
    {
      int x;
      int test;
    }TYPE_TEST;
    
    TYPE_TEST My[6] ={
        {16, 50},
        {4,  51},
        {50, 52},
        {47, 53},
        {10, 54},
        {19, 55}
    };
    int Newbuffer[ ????? ]
    Now I want to make the size of Newbuffer the sum of all "x", that is 146. Not the size of X.

    But how to sum all "x".

    /Cillo
  • divideby0
    New Member
    • May 2012
    • 131

    #2
    I'm not sure that a macro works well for summing and returning a value. You might try a function instead.

    However you get the sum of ".x", you cannot size an array like you're wanting to do. Use dynamic allocation instead.

    Code:
    // declare struct
    ...
    
    int sumX(TYPE_TEST *arr, int arr_size)
    {
       // code to sum "x" and return its value
    }
    ...
    
    TYPE_TEST my[6] = { { ... } };
    int *Newbuffer = malloc(sizeof(*Newbuffer) * sumX(my, sizeof(my)/sizeof(my[0])));
    
    if(Newbuffer)
    {
        // do something with Newbuffer
        free(Newbuffer);
    }

    Comment

    • Cillo
      New Member
      • Nov 2014
      • 2

      #3
      Nop.
      I can't do any thing dynmaic.
      This is just a eks. I also need this no for somthing else, so it must be knowen at the compile time.

      Comment

      • divideby0
        New Member
        • May 2012
        • 131

        #4
        I don't know to be honest; looping and generating a returnable value has me stumped. I can only think of something like

        Code:
        #define SUM_X(struct_var)\
        (\
           (struct_var[0].x) + \
            ...\
           (struct_var[5].x)\
        )
        
        #define SUM SUM_X(my)
        SUM would still be treated as a non-constant, I believe, so int Newbuffer[SUM] probably wouldn't compile. Hopefully, donbock, Banfa, or wfc will answer because I wouldn't mind knowning this myself.

        Sorry, I could offer anything useful.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          The C Preprocessor cannot do what you want.
          Or more precisely: if there is a way it involves tricky and esoteric features of the Preprocessor that would take a long time to discover ... and success would not be assured.

          Dynamic allocation is the most robust solution, but you have ruled that out. Best I can suggest is to do the addition explicitly so you crosscheck it visually.
          Code:
          TYPE_TEST My[6] ={
               {16, 50},
               {4,  51},
               {50, 52},
               {47, 53},
               {10, 54},
               {19, 55}
          };
          int Newbuffer[ 16+4+50+47+10+19 ];

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You can't do processing with the preprocessor. All the preprocessor let's you do is define things that will be compiled. Like:

            Code:
            #define BLUE 5
            Then in my code wherever there is the word BLUE it is changed to 5 and it is the 5 that is compiled and not BLUE.

            You could, of course write your own preprocessor which could read this code, locate the values, add them up, and put the result into your program, which is then compiled. I have seen this done more than once. In fact, it is the approached used by Oracle to install Oracle support in a program that you have written. Check out how Pro*C solved the problem.

            Comment

            Working...