0

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;

 }
4
  • This code is missing something, a } 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. Commented Jan 13, 2018 at 22:09
  • servers is not a variable, it's a struct. server isn't a variable, it's a typedef alias. You need to, at the absolute least, declare a proper servers array using either stack allocation or malloc. Commented Jan 13, 2018 at 22:12
  • My suggestion is that you remove all of the code in the initialize function, 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 the initialize function. If you use gcc or clang to compile, use the -Wall option and clean up all of the warnings. Commented Jan 13, 2018 at 22:30
  • You implemented it, so it's unclear what your problem is. Is it perhaps that it doesn't compile? Now, some advise: You have a struct servers, a typedef server and local variables called servers. Don't do that, you will get confused yourself. It's not very creative, but call the structure server_info and call the typedef the same. Then, using servers for the array won't be confused with the type. Commented Jan 13, 2018 at 22:42

1 Answer 1

2

Your program almost worked. There was a missing bracket to end the for loop in the void initialize(SERVER *s)

Usage of the same name for variable and the name of the structure is confusing. It is better to avoid it.

The serious problem in the print function was preventing you from seeing the proper results. You used formatting %d twice and supplied only one variable. I guess your intention was to print the serv[2] array. It can be easily done in the for loop.

This is modified program. I changed the initialization function so you can see that elements are properly initialized.

#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 *s);

  void initialize(SERVER *s)
  {
       int i=0,j=0;

       for(i=0; i<2; i++) {                  //i=0; i=1  
           s[i].id = i;                      //  0,   1 
           s[i].num_in_Q = i*i + 1;          //  1,   2  
           s[i].server_status = i+i + 2;     //  2,   4 
      } // the bracket was missing
}

 int main()
 {
    int i;
    SERVER serv[2];

    initialize(serv);

    for(i=0; i<2; i++) {
        printf("server[%d].id = %d\n", i, serv[i].id);
        printf("server[%d].num_in_Q = %d\n", i, serv[i].num_in_Q);
        printf("server[%d].server_status = %d\n\n", i, serv[i].server_status);
    }

    return 0;
}

OUTPUT:

server[0].id = 0
server[0].num_in_Q = 1
server[0].server_status = 2

server[1].id = 1
server[1].num_in_Q = 2
server[1].server_status = 4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.