This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 7 years ago.
I have this struct:
typedef struct {
GPIO_TypeDef* GPIO_Reg;
uint16_t GPIO_Pin;
uint16_t status;
} PinType;
Then if I declare this array:
PinType array[10];
The PinType elements in the array are initialized with some default values?
For example, if I write this:
printf("%d", array[1].status);
Should I see 0 as output? Or the initial value depends on the content of the memory before I declared the array?
This answer depends on the scope of the variable.
if array is global, then it will be auto-initialized.
if array is static, all the elements will be auto-initialized to 0.
if array is of automatic storage, it won't be initialized automatically.
The structure members are not initialised, unless the variable is static or global.
In fact, using an uninitialised member is undefined behaviour in C.
memsetting the array with zeros is idiomatic.
Related
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().
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.
This question already has answers here:
Are the members of a global structure initialized to zero by default in C?
(5 answers)
Closed 2 years ago.
I didn't find anything online about what happens when objects are created in C: like their value are initialized or they take garbage value.
#include <stdio.h>
struct temp
{
int a;
} s;
int main()
{
printf("%d", s.a);
}
OUTPUT is : 0.
So is 0 a garbage value?? Or is it an undefined behavior?
Since globals and static structures have static storage duration, the answer is yes - they are zero initialized (pointers in the structure will be set to the NULL pointer value, which is usually zero bits, but strictly speaking doesn't need to be).
You have used a global structure variable. Therefore initialised to default value, i.e., 0.
It depends on the place where the variable is declared. The structure variable s is declared as global. So it initialized to zero by default.
More in this link
This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 5 years ago.
#include<stdio.h>
void main(void)
{ char m,n;
printf("%d\n",m);//value of m
printf("%d",n);//value of n
}
in the above snippet value of m is always printed as 0 why?why it doesn't change even after multiple times compilation is it automatically assigned if we do not assign while the value of n always changes so why not both change randomly every time i compile?
Am i missing any concept?
Local variables like yours are automatic variables. They are allocated on stack memory and their value is garbage.
Global variables are implicitly have static storage class and have value 0 by default.
Since m is a local variable, its value needn't always be the same. It is indeterminate. This memory location can be given to other processes.
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[];
}