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.

8
  • 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. Commented Sep 12, 2016 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. Commented Sep 12, 2016 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? Commented Sep 12, 2016 at 20:51
  • 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? Commented Sep 12, 2016 at 20:54
  • 1
    You may also be able to get away with *(struct Fileinfo *)storagespace = (struct Fileinfo){0}; if your compiler supports it. Commented Sep 12, 2016 at 21:02

1 Answer 1

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
}
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.