473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to count in Preprocessor C

2
I want to count values in a struct.
Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.   int x;
  4.   int test;
  5. }TYPE_TEST;
  6.  
  7. TYPE_TEST My[6] ={
  8.     {16, 50},
  9.     {4,  51},
  10.     {50, 52},
  11.     {47, 53},
  12.     {10, 54},
  13.     {19, 55}
  14. };
  15. int Newbuffer[ ????? ]
  16.  
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
Nov 6 '14 #1
5 1702
divideby0
131 128KB
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.

Expand|Select|Wrap|Line Numbers
  1. // declare struct
  2. ...
  3.  
  4. int sumX(TYPE_TEST *arr, int arr_size)
  5. {
  6.    // code to sum "x" and return its value
  7. }
  8. ...
  9.  
  10. TYPE_TEST my[6] = { { ... } };
  11. int *Newbuffer = malloc(sizeof(*Newbuffer) * sumX(my, sizeof(my)/sizeof(my[0])));
  12.  
  13. if(Newbuffer)
  14. {
  15.     // do something with Newbuffer
  16.     free(Newbuffer);
  17. }
  18.  
Nov 6 '14 #2
Cillo
2
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.
Nov 6 '14 #3
divideby0
131 128KB
I don't know to be honest; looping and generating a returnable value has me stumped. I can only think of something like

Expand|Select|Wrap|Line Numbers
  1. #define SUM_X(struct_var)\
  2. (\
  3.    (struct_var[0].x) + \
  4.     ...\
  5.    (struct_var[5].x)\
  6. )
  7.  
  8. #define SUM SUM_X(my)
  9.  
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.
Nov 7 '14 #4
donbock
2,426 Expert 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. TYPE_TEST My[6] ={
  2.      {16, 50},
  3.      {4,  51},
  4.      {50, 52},
  5.      {47, 53},
  6.      {10, 54},
  7.      {19, 55}
  8. };
  9. int Newbuffer[ 16+4+50+47+10+19 ];
Nov 7 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
You can't do processing with the preprocessor. All the preprocessor let's you do is define things that will be compiled. Like:

Expand|Select|Wrap|Line Numbers
  1. #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.
Nov 13 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Bram Stolk | last post by:
Hi there, What could I use to parse CPP macros in Python? I tried the Parnassus Vaults, and python lib docs, but could not find a suitable module. Thanks, Bram
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
24
by: Nalla | last post by:
Hi, I want a program. It should be a command line one. you can input the path of a folder(preferably) or a file...it should count the no. of lines between the compiler directives, ifdef win32 and...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
26
by: G Patel | last post by:
Hi, I'm wondering if anyone knows if the following function will function properly as a set-bit counter on non 2s complement machines (as K&R2 implies). | int bitcount(unsigned x) | { | ...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
7
by: ddehterov | last post by:
Hello, I'm wondering, is #warning preprocessor command is documented in standart C or it's implementation specific? I cannot find any documentation about it. It works with gcc on freebsd and...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
1
by: Peter Ammon | last post by:
I was watching a video about a new C front end for the LLVM C compiler (on youtube, of all places), and the author made an interesting claim: that writing a C preprocessor is much harder, and...
7
by: Mark Riphenburg | last post by:
Hello, I'm working on a project where I need to count the current recursion depth of a macro to abstract recursion so that given instances don't collide. The idea is to provide say 8 instances of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.