c - Initializing a pointer to an array of integers. -
I have the following problem: I need to start a Stretch Number that represents a number, for that It requires its value, the number of separators and the dividers themselves. To store the divisor I need to use an indicator in an array of integers, and this is where the problem starts.
type-type straight {int value; Int divisor_amount; Int (* divider) []; } Number;
Now before I do not know how many divisors posseses my number I can not give the length of the array.
In the main function, I assign one value to each of these fields. This is not a problem for the first two variables.
Number six; Six. Value = 6; Six.divisor_amount = 3;
Now for the pointer I do not know how to actually start it. During class, it was said to do this:
Six. Divisors [0] = 1; Six divisors [1] = 2; Six divisors [2] = 3;
But then I remove this error from the compiler:
[Error] Invalid use of array with unspecified limitations
So I thought I needed to allocate some space and I tried:
six.divisors = malloc (six.divisor_amount * sizeof (int));
But this only gives me another error:
[Warning] Assignment from inconsistent pointer type [enabled by default]
< / Blockquote>
int (* divisors) [];
Incorrectly this is an indicator for the array of int
.
Usage
int * divisors;
Your malloc
allocation should work with it.
Comments
Post a Comment