2

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.

infused
  • 24,000
  • 13
  • 68
  • 78
user3108849
  • 111
  • 3
  • 12
  • 11
    Look up *arrays*. – Blorgbeard Dec 23 '13 at 20:09
  • 3
    I want your machine if it has infinite memory. –  Dec 23 '13 at 20:11
  • lol I meant I don't know how many variables will be entered and I don't want to create those variables at the beginning. I want to create a struct when it is required by the user but I don't know how to name them – user3108849 Dec 23 '13 at 20:12
  • 2
    If the number of entries is infinite, use dynamic memory allocation, in other words linked list. Change the struct to contain a pointer to struct myStruct. – alvits Dec 23 '13 at 20:12

3 Answers3

4

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.

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • I get the error "conflicting types for 'p' "... and what did you mean with "index" ? – user3108849 Dec 23 '13 at 20:22
  • @Devolus. Personally, I would go with your answer because it is a better overall concept. I am not a big fan of fragmented linked lists, although they do have their uses. With memory running into the GBs, it is generally easier to preallocate. – Mad Physicist Dec 23 '13 at 20:28
  • You can always `realloc()` if you need more entries. –  Dec 23 '13 at 20:38
  • Oh okay I got it.. Thanks for the solution @Devolus and thanks for the important edit Arkadiy (I cannot tag two users :) ) – user3108849 Dec 23 '13 at 21:05
2

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • I got the idea how did you increase the number of structs.. I just couldn't understand how did you name the each struct. Could you mind helping me to understand that ? :) – user3108849 Dec 23 '13 at 20:28
  • @user3108849. I did not name any of the structs besides the first and last one in the list. You start at the first one, step along the `next` pointers until you hit a `NULL`. This way you do not need to know the names. I have added an example of how you could print all the values in your list to illustrate. – Mad Physicist Dec 23 '13 at 20:34
  • I think I will go with the one including naming.. Thank you very much for your attention :) – user3108849 Dec 23 '13 at 21:03
1

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.

Kevin
  • 53,822
  • 15
  • 101
  • 132
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • "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" Yeah, this is exactly my situation. As an experienced to a beginner, can you give me some advice here? Because I'm really confused here.. – user3108849 Dec 23 '13 at 20:43
  • I'm asked to build a kind of social network, the program will get the commands from a file and apply them. I thought of creating structs for each user and name them as user1, user2.. etc. Is it wrong? The command will say something like e.g. "make John Smith and Jane Doe frineds." and I will look for the structs of John and Jane and making them friends. Which method should I use? – user3108849 Dec 23 '13 at 20:47
  • Yes, using a variable for each user is the wrong way to go about it. In a real social network, each user would probably be represented as a record in a database table, and you'd probably have another table representing the relationships between users. But that's pretty advanced. I'd suggest learning about arrays and linked lists, as illustrated in the other answers. Get comfortable with dynamic memory allocation. Then draw a picture of how you want your program to represent data, with a box for each structure and an arrow for each pointer. – Caleb Dec 23 '13 at 21:05
  • My program will be a miniature of a social network, I don't know how many how many users will I add but I think giving names for each user will help me this time. Thank you very much for your detailed help. – user3108849 Dec 23 '13 at 21:08