I am trying to improve a program I have, but there is one thing I cannot figure out to do... so, trying to make this short, this is what I'd like to accomplish:

Is there a way for C to count how many variables are equal to a particular value?
For example, say I have integers a,b,c,d and assign them like this:
a=4;
b=2;
c=4;
d=4;

Is there a way to save the amount of variables that equal a? (which would be 2.. or 3 if you count a).

I just really want to save a single numerical value that tells how many variables I have equal to something.

Dani AI

Generated

Short answer: C has no runtime way to inspect separate named variables, so group related values as data. is correct that the idiomatic solution is to put the values in an array (or an array of structs if you need names). If you cannot change how the variables are declared, collect their addresses into an array of pointers and iterate those pointers to count matches.

Example (different approach than the earlier snippets):

int a = 4, b = 2, c = 4, d = 4;
int *vars[] = { &a, &b, &c, &d };
size_t n = sizeof(vars) / sizeof(vars[0]);
int key = a; /* value to match */
int matches = 0;
for (size_t i = 0; i < n; ++i)
    if (*vars[i] == key) ++matches;

If you want to exclude the reference variable (the a whose value you used as key), either omit &a when you build vars or subtract one from matches afterwards.

Notes and pitfalls: 's line int *a = {2,3,4,...}; is invalid C; brace initializers create arrays or aggregate initializers, they do not initialize a single pointer. Also ensure any pointers you put into vars point to objects that remain in scope (no dangling pointers). For a one-time check the O(n) scan above is simplest. For many repeated queries build a frequency map (counts per value) or sort once and use binary searches — these make repeated lookups much faster at the cost of extra work up front.

Best practice: keep related data together (arrays/struct arrays). It makes counting, searching, and future changes far easier and less error-prone than juggling separately named variables.

Recommended Answers

All 2 Replies

There is not a short and easy way to do that. If you need to search your variables for matching values, then they're related enough to put in an array:

int a[] = {4, 2, 4, 4};

And if you need to keep the names, it's only slightly more complex:

struct Variable
{
    char *name;
    int value;
};

struct Variable a[] =
{
    {"a", 4},
    {"b", 2},
    {"c", 4},
    {"d", 4},
};

An array makes the whole process of looking for matches much easier because you can throw everything in a loop:

for (int x = 0; x < sz; ++x)
{
    if (a[x] == key) ++matches;
}

I do agree with TOM GUNN.Its a nice way to do things using structure.

Still u can use simple variables.

int  *a={2,3,4,5,6,7};

int no_of_variables=6;
int value_to_match=2;
int counter=0;

for(i=0;i<no_of_variables;i++)
{
if(a[i]==value_to_match) counter++;
printf("match variable is a_%d",i);
}
printf("Total mathch variales found is %d",counter);

}

NOTE:
Here variable are refered by a_0,a_1,a_2 like this in the output screen.
internaly a_1 means a[1] and so on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.