Naming a variable with another variable in C

2019-07-20 03:47发布

I want to create a struct with 2 variables, such as

struct myStruct {
char charVar;
int intVar;
};

and I will name the structs as:

struct myStruct name1;
struct myStruct name2;

etc.

The problem is, I don't know how many variables will be entered, so there must be infinite nameX structures.

So, how can I name these structures with variables?

Thanks.

3条回答
The star\"
2楼-- · 2019-07-20 04:26

You should use an array and a pointer.

struct myStruct *p = NULL;
p = malloc(N * sizeof *p);  // where N is the number of entries.
int index = 1; /* or any other number - from 0 to N-1*/
p[index].member = x;

Then you can add elements to it by using realloc if you need to add additional entries.

查看更多
smile是对你的礼貌
3楼-- · 2019-07-20 04:29

So, how can I name these structures with variables?

I think every beginner starts out wanting to name everything. It's not surprising -- you learn about using variables to store data, so it seems natural that you'd always use variables. The answer, however, is that you don't always use variables for storing data. Very often, you store data in structures or objects that are created dynamically. It may help to read about dynamic allocation. The idea is that when you have a new piece of data to store, you ask for a piece of memory (using a library call like malloc or calloc). You refer to that piece of memory by its address, i.e. a pointer.

There are a number of ways to keep track of all the pieces of memory that you've obtained, and each one constitutes a data structure. For example, you could keep a number of pieces of data in a contiguous block of memory -- that's an array. See Devolus's answer for an example. Or you could have lots of little pieces of memory, with each one containing the address (again, a pointer) of the next one; that's a linked list. Mad Physicist's answer is a fine example of a linked list.

Each data structure has its own advantages and disadvantages -- for example, arrays allow fast access but are slow for inserting and deleting, while linked lists are relatively slow for access but are fast for inserting and deleting. Choosing the right data structure for the job at hand is an important part of programming.

It usually takes a little while to get comfortable with pointers, but it's well worth the effort as they open up a lot of possibilities for storing and manipulating data in your program. Enjoy the ride.

查看更多
孤傲高冷的网名
4楼-- · 2019-07-20 04:41

Redefine myStruct as

struct myStruct {
    char charVar;
    int intVar;
    struct myStruct *next;
};

Keep track of the last structure you have as well as the start of the list. When addding new elements, append them to the end of your linked list.

/* To initialize the list */
struct myStruct *start, *end;
start = malloc(sizeof(struct myStruct));
start->next = NULL;
end = start;

/* To add a new structure at the end */
end->next = malloc(sizeof(struct myStruct));
end = end->next;
end->next = NULL;

This example does not do any error checking. Here is how you would step along the list to print all the values in it:

struct myStruct *ptr;
for(ptr = start; ptr != NULL; ptr = ptr->next)
    printf("%d %s\n", ptr->intVar, ptr->charVar);

You not have to have a distinct name for each structure in a linked list (or any other kind of list, in general). You can assign any of the unnamed structures to the pointer ptr as you use them.

查看更多
登录 后发表回答