Search code examples
cfunctionpointersstructinitialization

initialize struct from function call


Feel like im taking crazy pills just trying to do literally the simplest stuff I can imagine in C. Any help would be extremely appreciated. why does this work?

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

but this throws away the initialized struct?

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
'''
int POINTERS_PER_INODE = 5;

struct Inode {
   int  valid;/* 0 == invalid, 1 == valid*/
   int size;
   int Blocks [5];
};

int InodeToString(char * InodeString, struct Inode iNode){
  char * blockBuffer;
  sprintf(InodeString, "%d", iNode.valid);
  int i;
  for (i = 0; i < POINTERS_PER_INODE; i++){
    blockBuffer = malloc(8);
    sprintf(blockBuffer, "%d", iNode.Blocks[i]); //no valid pointers yet
    strcat(InodeString,blockBuffer);
    free(blockBuffer);
  }
  return 0;
}

int initializeInode(struct Inode iNode){
  int i;
  for (i = 0; i < POINTERS_PER_INODE; i++){
    iNode.Blocks[i] = -1; //no valid pointers yet
  }
  iNode.valid = 0; //initialized as invalid inode
  return 0;
}

int main() {
  struct Inode iNode1;
  initializeInode(iNode1);

  char * InodeString;
  InodeString = malloc(20);
  InodeToString(InodeString, iNode1);
  printf("%s", InodeString);

  free(InodeString);
  iNode1.valid = 1;
  InodeString = malloc(20);
  InodeToString(InodeString, iNode1);
  printf("%s", InodeString);

  return 0;
}

This is test code btw, so the includes probably dont make sense. stack overflow says I dont have enough details so I guess I have to keep typing sentences. Let me know if theres any details that would make this more clear. its for a basic super simplified file system simulation project. it seemed in a previous version when I initialized the inode outside of the function, I was able to pass the string into the string function, assign it values, not use it as the return value and still end up on the other side of the function with an updated string.


Solution

  • As is normal in C, arguments to a function are passed by value. The object called iNode in initializeInode is local to that function, and changes to it have no effect on any other object in the program. If you want a function to modify an object that's local to the caller, you have to pass a pointer to it, and dereference that pointer to get at the caller's object.

    So what you probably want is:

    int initializeInode(struct Inode *iNode){
      int i;
      for (i = 0; i < POINTERS_PER_INODE; i++){
        iNode->Blocks[i] = -1; //no valid pointers yet
      }
      iNode->valid = 0; //initialized as invalid inode
      return 0;
    }
    
    int main() {
      struct Inode iNode1;
      initializeInode(&iNode1);
      // ...
    }