-1

I'm building a server for a project and I need to store a bunch of values in a ordered way. I've been searching for hours and I haven't figured out how.

I built a struct as follows:

struct WHITEBOARD{
    int line;
    char type;
    int bytes;
    char string[1024];  
} *Server;

Then in my main function I want to dynamically allocate memory to create an array of structs WHITEBOARD to a size of [argv[1]] (eventually). I would like to use calloc and in my research I have found the following:

void main()
{
    struct whiteboard (*Server) = (struct whiteboard*) calloc(10, sizeof(*Server));
    (*Server).line = 2;
    printf("Test: %d\n",(*Server).line);
}

This works but I can't seem to find out how to turn Server into an array of structs so that I can reference (*Server)[1].line and assign to this heap bound variable from a function. Which I intend to do as follows.

char* doThing(struct whiteboard Server)
{
    (*Server)[1].line = 4;
    return;
}

And be able to printf the newly bound variable from main.

This might be a dumb question, but any help would be awesome. Thanks!

2 Answers2

0
struct WHITEBOARD{
    int line;
    char type;
    int bytes;
    char string[1024];  
} *Server;

You have a variable (a pointer to struct WHITEBOARD) called Server at global scope, thus, you don't need to redeclare-it inside main nor inside your function parameters, also note that you are misusing the dereference operator (*), to access the element 1 in (*Server)[1].line = 4; just use Server[1].line = 4;

void doThing(void) /* Changed, you promise to return a pointer to char but you return nothing */
{
    Server[1].line = 4;
}

int main(void) /* void main is not a valid signature */
{
    Server = calloc(10, sizeof(*Server)); /* Don't cast calloc */

    Server[1].line = 2;
    doThing();
    printf("Test: %d\n", Server[1].line);
    free(Server);
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0

Simply get rid of all the obscure syntax you have invented, don't "guess the syntax" when you are not sure of how to do something.

  • Keep struct declaration and variable declarations separate.
  • Don't use global variables.
  • Don't use parenthesis when there is no obvious need for it.
  • Don't dereference a pointer with both * and [] operators in the same expression.
  • Don't cast the result of calloc.
  • Don't write functions that return a type and then return nothing.
  • The signature of main() on a hosted system is int main (void).
  • Always free() allocated memory.

Example:

#include <stdio.h>
#include <stdlib.h>

typedef struct 
{
  int line;
  char type;
  int bytes;
  char string[1024];  
} whiteboard_t;


void do_thing (whiteboard_t* server)
{
  server[1].line = 4;
}

int main (void) 
{
  int n = 10;
  whiteboard_t* server = calloc(n, sizeof(whiteboard_t));

  server[0].line = 2;
  printf("Test: %d\n",server[0].line);

  do_thing(server);
  printf("Test: %d\n",server[1].line);

  free(server);
  return 0;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • The `_t` type suffix is reserved by POSIX and it seems that OP wants an array of `10` elements, not an array of 10 * 10 elements, use `calloc(n, sizeof(whiteboard_t));` instead of `calloc(n, sizeof(whiteboard_t[n]));` – David Ranieri Mar 14 '17 at 08:19
  • @KeineLust The question is about C not POSIX. That POSIX bans `_t` postfix is simply a design flaw in the POSIX standard, which I couldn't care less about. The C standard actively encourages the use of `_t` for typedefs. File a defect report to POSIX if you care. The typo has been fixed, thanks. – Lundin Mar 14 '17 at 12:12