0

Let's say I have the following struct and array of that struct:

struct Fileinfo {
  int ascii[128];  //space to store counts for each ASCII character.
  int lnlen;       //the longest line’s length
  int lnno;        //the longest line’s line number.
  char* filename;  //the file corresponding to the struct.
};

struct Analysis fileinfo_space[8]; //space for info about 8 files

I want to have a function that will add a new struct to this array. It must take a void pointer to the position where to store the struct as an argument

int addentry(void* storagespace){
    *(struct Fileinfo *)res = ??? //cast the pointer to struct pointer and put an empty struct there
    (struct Fileinfo *)res->lnlen = 1; //change the lnlen value of the struct to 1
}

My questions are:

  1. What goes in place of ??? I tried (Fileinfo){NULL,0,0,NULL} as per this Stackoverflow response. But I get `error: ‘Fileinfo’ undeclared (first use in this function)

  2. How do I create a void pointer to the array? Is (void *)fileinfo_space correct?

I am required to use void * as the argument for the function for this assignment. It's not up to me.

Community
  • 1
  • 1
quantumbutterfly
  • 1,815
  • 4
  • 23
  • 38
  • 2
    You have an array of the structs... unless you're trying to make the array bigger, the struct is already there (no need to "add" it). Just set its members to something meaningful. – Dmitri Sep 12 '16 at 20:49
  • 1
    @quantumbutterfly: What do you mean? That's a normal array dereference and a `struct` member access. If you have not learn that, look into your C book. – too honest for this site Sep 12 '16 at 20:51
  • Let's say I have a void pointer to the struct array. How can I use that to set the lnlen value of the first struct to 1? – quantumbutterfly Sep 12 '16 at 20:51
  • Note: never use `void *` unless you really knwo what you are doing and you have good reasons not to use the actual type. – too honest for this site Sep 12 '16 at 20:52
  • I am required to use void * as the argument for the function for this assignment. It's not up to me – quantumbutterfly Sep 12 '16 at 20:54
  • 1
    You don't create the struct, it's already part of the array. You can copy another struct's values like so: `*(struct Fileinfo *)storagespace = otherstruct;` or access its members like so: `((struct Fileinfo *)storagespace)->lnlen = 1;`, or do `struct Fileinfo *tmp = storagespace;` and then just use eg. `tmp->lnlen = 1;`, etc. Btw, is `struct Analysis` a mistake, or is your array actually of a different type of struct? – Dmitri Sep 12 '16 at 20:54
  • 1
    You may also be able to get away with `*(struct Fileinfo *)storagespace = (struct Fileinfo){0};` if your compiler supports it. – Dmitri Sep 12 '16 at 21:02
  • @Dmitri Thanks! Inside the function, I removed the first line and added an asterisk in front of the second line and that fixed the problem. If you want the experience points for having an answer accepted, just post your comments and an answer and I'll accept it – quantumbutterfly Sep 12 '16 at 21:11

1 Answers1

1

Let's say you have some memory block passed as storagespace void pointer:

You have to define a constant to be able to initialize (unless you're using c++11), let's call it init. BTW your assignment value is wrong: first member is an array of int. You cannot pass NULL to it. Just zero-fill it like show below.

Then cast your void pointer into a pointer on your struct, then initialize by copying the init struct, modify at will...

int addentry(void* storagespace){
    static const struct Fileinfo init = {{0},0,0,NULL};
    struct Fileinfo *fi = (struct Fileinfo *)storagespace;
    *fi = init; //cast the pointer to struct pointer and put an empty struct there
    fi->lnlen = 1; //change the lnlen value of the struct to 1
}
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219