Reputation: 11
I have a large global array in which I keep changing some of the values which when compiled for different purposes requires the table to be altered. Basically the table as a core structure and based on different purposes additional values may get added/removed from it. The values are somewhat like:
int global_array[] =
{
...
6, 6, 78, 9,
12,
13,5
19,
47, 768, 98, 89
...
};
I need to access some of core structure values (lets say "12" here) inside these table. So while compiling for different purposes, index of concerned value ("12") changes. For same reason I cannot keep this table as structure. Keep in mind this is a huge table and for some reason we don't write values in uniform fashion (read linear).
So for every new purpose I have to manually count index of value "12" which is tedious. I want a future proof process.
I was wondering if I could use something like:
int global_array[] =
{
...
6, 6, 78, 9,
INDEX: 12,
13,5
19,
47, 768, 98, 89
...
};
And access/modify values at run-time as below:
*(uint8 *)INDEX = 20;
Upvotes: 0
Views: 501
Reputation: 32522
You can keep additional pointer variables around that point to specific entries. Whenever needed, you can adjust the array entry the pointer points to.
Example:
#include <stdio.h>
int global[] = {1,2,3,4,5,6,7};
int *idx = &global[0];
int main() {
*idx = 20;
printf("%d\n", *idx);
return 0;
}
Alternatively, you could use a preprocessor macro (if the location that is referred to is known at compile time and will not change):
#include <stdio.h>
int global[] = {1,2,3,4,5,6,7};
#define INDEX (global[0])
int main() {
INDEX = 20;
printf("%d\n", INDEX);
return 0;
}
Given you only need to do this once at program start, maybe you just need a function that does the counting for you.
Example:
#include <stdio.h>
int global[] = {1,2,3,4,5,6,7};
int find_index(int value, int *array, size_t size) {
for (int i = 0; i < size; i++)
if (array[i] == value)
return i;
return -1;
}
int main() {
int value = 4;
int index = find_index(value, global, sizeof(global)/sizeof(*global));
printf("index of %d: %d\n", value, index);
return 0;
}
Here's the output:
$ gcc tt.c -std=c99 && ./a.out
index of 4: 3
If the positions of lots of entries needs to be tracked throughout the entire runtime of the application you should consider using a key-value storage (e.g., a binary search tree) to keep track of the values' indices. You should then use special methods that encapsulate the update and retrieval operations that will also adjust the indices stored in that "index" data structure.
Upvotes: 2
Reputation: 72667
C does not provide any syntax to do what you want.
One way to solve this so you don't have to count something with each change, is to have a tool of yours do the counting. After all, machines are good at counting things. For example, you could mark the interesting postitions in the initializer with a special comment, i.e.
int global_array[] =
{
6, 6, 78, 9,
12, /* INDEX12 */
13,5,
19,
47, 768, 98, 89
...
};
The index for the array element is then found by counting the commas from the start of the declaration, minus one. Turn the result into a compile-time macro with -DINDEX12=42
, et voilà! All you need is an idea of where the declaration starts (putting it in a separate header file is the easy route), and run for example
$ awk '/global_array/,/INDEX12/ { commas += gsub(/,/, ",") }
END {print "-DINDEX12=" commas - 1}' file.h
-DINDEX12=4
This is easily extended to deal with any number of INDEXNNN
macros. Beware of the potential off-by-one for the last array initializer (which doesn't have a comma in your snippet, but C does allow it...)
Placing this preprocessing in a snugly Makefile
left as an exercise.
Upvotes: 0
Reputation: 6555
From ISO/IEC9899:
6.8.6.1 The goto statement
Constraints
1 The identifier in a goto statement shall name a label located somewhere in the enclosing function.
A goto statement shall not jump from outside the scope of an identifier having a variably modified type to inside the scope of that identifier.
As a global aray is not located in an enclosed function, this is not allowed!
Upvotes: 0