0

here is what i have in done so far

struct test_case {
  int n;
  int *test[];
};


struct test_case *test_case_struct = (struct test_case *)malloc(
      sizeof(struct test_struct) + 100 * sizeof(int));

I need to allocate n pointers in the "test" pointer array. As far as i know i need to allocate space to the structure and then some more for the pointer array, but when i try to compile this, i get the error invalid use of sizeof operator for to incomplete type struct test_struct

if someone could please inform me how i can take the value of n as a user input and have int *test [n] made possible.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

3 Answers3

0

Currently you are using flexible array(aka zero length array). Which can be allocated as below.

struct test_case *test_case_struct =
   malloc(sizeof (*test_case_struct) + 100 * sizeof (int *));

Note missing * for int and typo sizeof(struct test_struct) in your code.


Alternatively you can use pointer to pointer as below.

struct test_case {
  int n;
  int **test;
};


struct test_case *test_case_struct = malloc(
      sizeof(*test_case_struct));
test_case_struct->test = malloc(100 * sizeof(int *)); // Allocates 100 pointers
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
0

You need to change

  sizeof(struct test_struct)

to

   sizeof(struct test_case)

as test_struct is not the correct structure type.

In a better way, you can also use the already-declared variable name, like

struct test_case *test_case_struct = malloc(
         sizeof (*test_case_struct) + n * sizeof(int*));

That said, you need to allocate memory worth of int *s, not ints, for the flexible member.

Also, below is a snippet which shows the count is taken as user input

int main(void)
{
    int n = 0;
    puts("Enter the count of pointers");
    if (scanf("%d", &n) != 1) {
        puts("Got a problem in the input");
        exit (-1);
    }
    struct test_case *test_case_struct = malloc( sizeof(struct test_case) + n * sizeof(int*));
    printf("Hello, world!\n");
    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • my intention is to have a pointer array that can point to **n** elements. where **n** is a user input. both **n** and pointer array being inside the structure. sorry for the typo in the snippet there – Shiva Kumar Nov 21 '18 at 09:07
  • @ShivaKumar So, you are saying, you don't need an array of pointers to `int`s, rather an array of `int`s, the count of the `int`s would be stored in `n`, right? – Sourav Ghosh Nov 21 '18 at 09:19
  • nope. if i were to have a pointer array int *test[n], n would be a user input (this alone is not challenging for me, i gave done it before), the complexity is where i need to put the pointer array **int *test[ ]** inside a structure, and also its size **int n** inside a structure. the **use case** i am needing this for is i have the user inputting different square matrices of different dimensions (**int n**). each pointer in the **test[0], test[1] ... test[n]** is pointing to an **int array** which inturn have **n** elements in it. i hope i have made the picture clear enough. – Shiva Kumar Nov 22 '18 at 05:23
0

Don't repeat type names. You already stumbled over your own code twice because you did that. You made the mistake of typing the wrong struct tag and confusing int* for int.

A more hardy allocation would look like this

struct test_case *test_case_struct =
  malloc(sizeof (*test_case_struct) + sizeof (test_case_struct->test[0]) * 100);

This here will allocate the size of whatever test_case_struct points at, plus 100 more of whatever test_case_struct->test[0] should be. Now you can play with the structure definition without breaking this call to malloc. And if you do perform a breaking change (like renaming test), you'll be notified by your compiler promptly.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458