I want to count values in a struct. -
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
5 1702
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. -
// 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);
-
}
-
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.
I don't know to be honest; looping and generating a returnable value has me stumped. I can only think of something like -
#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.
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. - TYPE_TEST My[6] ={
-
{16, 50},
-
{4, 51},
-
{50, 52},
-
{47, 53},
-
{10, 54},
-
{19, 55}
-
};
-
int Newbuffer[ 16+4+50+47+10+19 ];
You can't do processing with the preprocessor. All the preprocessor let's you do is define things that will be compiled. Like:
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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
|
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...
|
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...
|
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)
| {
| ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
| |