C, use int variable as array size inside structure [duplicate] - c

This question already has answers here:
Can we have a struct element of type Variable length array? [duplicate]
(5 answers)
Closed 2 years ago.
I would like to define array size inside a structure by using a parameter of this structure. Does C permit to do something like this ?
struct queue {
int head;
int top;
int size;
struct action action[size];
};

No you can't. Since action is not a dynamic variable, the compiler needs to know at compile time how much space it needs for action. size was not even initialized. Anyway, you could see this just by trying to compile.

The size is not known at the time of defining the struct. Therefore it is impossible for the compiler to understand how large the result will be. Typically, you would first allocate memory for the struct, and have a struct action *action; member. After initializing the struct, you use instance->action = calloc(instance->size, sizeof *instance->action) to allocate memory for the array.

Related

Using struct field in another field of the same field in C [duplicate]

This question already has answers here:
Array of variable length in a structure
(3 answers)
Closed 1 year ago.
I am begginer in C programming. Can you tell me why I can't use one field in struct in another field?
struct example{
int length;
int array[length];
}
Can you tell me what's the scope struct has? I know that is just struct tag not real struct variable,but please explain to me.
length is not a variable, it's a structure member. It can only be used along with a variable that contains that structure type, e.g.
struct example myvar;
myvar.length = 10;
Since the size of the array member has to be known when the variable is declared, it's not possible for it to be dependent on the value of another member of the same structure.
If the size of the array varies, you can use a flexible array member. See What's the need of array with zero elements?

How to return char *arr[] with variable length in C? [duplicate]

This question already has answers here:
Returning an array using C
(8 answers)
Closed 2 years ago.
I want to refactor a function so that I can use it for arrays of different lengths and return that newly created array so that other functions can access it. I can't make the array static since you can't have static arrays with dynamic length. I also can't use a global struct because that needs to take the length of the array and that has to be hardcoded I think.
So the question is whether it is even possible to do something like this:
char* splitElementsArr(FILE* file){
int length = countBlankLines(file);
char *arr[length] // or maybe use malloc here
...Some operations to fill array
return arr;
No, you can't. You are returning a pointer to local data that were dynamically allocated on the stack. As soon as you return, those data no longer exist.
Solution : use malloc().

Dynamically allocate Matrix using typedef [duplicate]

This question already has answers here:
How do I correctly set up, access, and free a multidimensional array in C?
(5 answers)
Closed 6 years ago.
I already have an idea on how to malloc a matrix if it were an int**. But using a typedef, I think have an idea but I'm not so sure.
typedef int LabeledAdjMatrix[SIZE][SIZE];
Do I do it like this?
APSP = (APSPMatrix*)malloc(sizeof(APSPMatrix));
But when I access it I'm gonna have to use *APSP[0][0] and I have no idea how to use this in memset/memcpy.
Is there a proper way of doing this? Both in dynamically allocating and in accessing.
My advice would be to not use array typedefs, they make the code harder to read as it is less apparent when array-pointer decay is or isn't happening.
If you want to allocate a contiguous array you can write:
int (*APSP)[SIZE] = malloc( sizeof(int[SIZE][SIZE]) );
and then access it as APSP[0][0].
Your post talks about "malloc as if it were int **", by which I assume you mean you want separate allocations for each rows... but then you would write int **APSP and write a loop to allocate each row, it is really nothing to do with [SIZE][SIZE].

Getting the size of a malloced area [duplicate]

This question already has answers here:
Determine size of dynamically allocated memory in C
(15 answers)
How to get the size of dynamically (using malloc or calloc )allocated memory? [duplicate]
(2 answers)
Closed 9 years ago.
Is there a way to know from a pointer the size with which malloc() was called?
For example, if I have:
typedef struct entry entry_t;
struct entry
{
int val;
};
entry_t *entryt_p = (entry_t *)malloc(10 * sizeof(entry_t));
Is there a way I can extract from entryt_p with which size malloc() was called?
There isn't a portable way specified by the language. Some versions of malloc may offer an extension to do it. In general, it's up to your program to keep track.
There is no way for the user to access this information. Malloc returns a pointer to the address of the memory that was allocated.
It is up to the program to keep track of how much was allocated.
If you really don't want to keep track of the size consider using a sentinel value in the last position.

Declaring a zero sizes array [duplicate]

This question already has answers here:
zero length arrays [duplicate]
(3 answers)
Closed 9 years ago.
I tried declaring an array a of size 0:
int a[0];
My VC++ 6 compiler throws an error of not being able to create an array of zero size.
If I try the same of declaring inside a structure, I do not get any errors.
struct st
{
int a[0];
}
The code gets compiled and linked without any errors. Can somebody help me understand how the compiler reacts in the above two cases. Thanks.
The struct is a special case. It is a common pattern to declare an empty array as the last member of a struct, where the struct is actually part of a larger block of memory of variable length. See Empty arrays in structs for more explanation.
Some compilers support the extension of using zero-sized arrays as the last element of the struct, to indicate your intent to allocate there an array whose size you don't know yet. Then you can use that struct member (the zero-sized array) to access the elements of that array.
Note that is not an standard feature from C89, and C99 offers an alternative solution:
struct st
{
int a[];
}

Resources