I am trying to access items in an array of structs and change the valus in function. I create a array of struct and i try too pass in function initialize where i change the valus. But i do not know how to pass the changes of function in main.
The code is:
#include <stdio.h>
#include <stdlib.h>
#define Q_LIMT 100
typedef struct servers
{
int id;
int num_in_Q;
int server_status;
}server;
void initialize(server *servers);
int main()
{
server servers[2];
initialize(servers);
printf("server[%d].id = %d\n",servers[0].id);
printf("server[%d].num_in_Q = %d\n",servers[0].num_in_Q);
printf("server[%d].server_status = %d\n",servers[0].server_status);
return 0;
}
void initialize(server *servers)
{
int i=0,j=0;
for(i=0; i<2; i++) {
servers[i].id = i;
servers[i].num_in_Q = 0;
servers[i].server_status = 0;
}
}at the very least. Is this the complete code? Remember you don't need to declare method signatures in advance if you're declaring the function later in the file, you can just move that definition prior to its first use.serversis not a variable, it's a struct.serverisn't a variable, it's atypedefalias. You need to, at the absolute least, declare a properserversarray using either stack allocation ormalloc.initializefunction, and then get your code to compile without any warnings or errors. You have so many problems in your code that you aren't even close to being able to talk about what should be in theinitializefunction. If you usegccorclangto compile, use the-Walloption and clean up all of the warnings.servers, a typedefserverand local variables calledservers. Don't do that, you will get confused yourself. It's not very creative, but call the structureserver_infoand call the typedef the same. Then, usingserversfor the array won't be confused with the type.