I want to write something of this sort:
(s[i])[i] ,where s[i] is of type char[].
Example:
s[i]="PDGH";
for(i = 0 ; i < strlen(someNumber) ; i=i+3 )
(s[i])[formula]++; // on compile if s[i]='P', then i want to get P[formula]++ .
I want to write something of this sort:
(s[i])[i] ,where s[i] is of type char[].
Example:
s[i]="PDGH";
for(i = 0 ; i < strlen(someNumber) ; i=i+3 )
(s[i])[formula]++; // on compile if s[i]='P', then i want to get P[formula]++ .
What you're asking for is to map a string (well, in your case a single character) to a variable in C. This is not directly supported by the language. You could however use your choice of associative array implementation, for which there are tons of choices--see here: Looking for a good hash table implementation in C
Or, since in your example you have only single-character variables (P,D,G,H), you could build a lookup table:
int P=0, D=0, G=0, H=0;
int* targets[256] = {};
targets['P'] = &P;
targets['D'] = &D;
targets['G'] = &G;
targets['H'] = &H;
for (size_t i = 0; i < strlen(s); i += 3) {
assert(targets[s[i]]);
(*targets[s[i]])++; /* if s[i] == 'P', increment P */
}