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.

2
  • try using typedef when you create your structure then use the type name in your sizeof. Commented Nov 21, 2018 at 7:37
  • Side note, but consider not casting the result of malloc. Commented Nov 21, 2018 at 7:40

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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
@ShivaKumar So, you are saying, you don't need an array of pointers to ints, rather an array of ints, the count of the ints would be stored in n, right?
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.
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

3 Comments

And why flexible array is not suitable?
@SouravGhosh I just ass*u*me*d OP might not intended to use flexible array and accidentally declared structure like that.
@kiranBiradar The title suggests that an array of pointers in a struct is desired.

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.