Is zero size array invalid in C struct? [duplicate] - c

This question already has answers here:
Array of size 0 at the end of struct [duplicate]
(5 answers)
What is the purpose of a zero length array in a struct? [duplicate]
(2 answers)
Closed 3 years ago.
I have compiled followin gprogram using GCC compiler on Ubuntu platform.
I am wondering why the following program is working fine in C?
Is it undefined behaviour?
#include <stdio.h>
struct str
{
char arr[0];
};
int main()
{
struct str s;
s.arr[0]=1; // I think it is invalid in C
printf("%d\n", s.arr[0]);
return 0;
}
Output:
1
and when I print printf("%zu\n", sizeof(s));. it is print 0.

Related

Why is segmentation fault given? C [duplicate]

This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 1 year ago.
I've tried writing this code and the two printf statements appear to give segmentation fault.
why isn't the output 5? what went wrong? and how to edit this code in a way such that it prints 5?
#include <stdio.h>
int* fun(int x){
x=5;
return &x;
}
int main() {
int x = 3;
int* p = fun(x);
printf("%d",*(fun(x)));
printf("%d",*p);
return 0;
}

Size is 4, but it must be 400 [duplicate]

This question already has answers here:
How can I get the size of an array from a pointer in C?
(16 answers)
Find malloc() array length in C? [duplicate]
(4 answers)
Closed 2 years ago.
Following program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *array;
array = malloc(sizeof(int)*100);
for(int i=0; i<sizeof(array); i++) {
printf("%d\n",i);
}
free(array);
}
It display 0-3. But I expected 0-399. I thought the size will be calculated through 4*100.

How C allows argument definition after Close baracket( ")" )? [duplicate]

This question already has answers here:
strange function definition in Scilab<->C interface
(4 answers)
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 3 years ago.
How Does C Allows declaring type of argument after Closed Bracket like below Code?. The below Code is actually compiling and Executing without any error.
How does this generally Work?
#include <stdio.h>
void print_int(num)
int num;
{
printf("\n\n\nNumber : %d\n\n",num);
}
int main(argc,argv)
int argc;
char** argv;
{
print_int(2);
return 0;
}

Pass pointer to a literal direct to a function [duplicate]

This question already has answers here:
Pointer to literal value
(12 answers)
Closed 4 years ago.
Given the following piece of C code:
void calc(int *value)
{
// do something with value
}
int main(void)
{
int i;
i = 10;
calc(&i);
}
Is it possible to get rid of setting up i and pass directly 10 to function calc? If yes, how can this be done?
Example of what I have in mind (which doesn't work):
calc( (int *) 10);
Nope, it's impossible to have a pointer to a constant in C.
UPD: however it seems that there is a trick using a struct compound literals syntax (thanks to #antti-haapala for the correction). Try this:
calc(&(int){10});

Declaration of character pointer as global and local variable when using pipe() varies [duplicate]

This question already has answers here:
Printf printing garbage after read() call. The offset is always printed as 0
(3 answers)
Why is my simple C program displaying garbage to stdout?
(5 answers)
Closed 4 years ago.
#include<stdio.h>
#include<unistd.h>
char *msg1="HELLLO",*msg2="NONONO";//global declaration prints without any garbage value
int main()
{
/*char *msg1="HELLLO",*msg2="NONONO";"global declaration prints with garbage value */
char buf[6];
int file[2],i;
if(pipe(file) < 0)
printf("\nyou are out");
write(file[1],msg1,6);
write(file[1],msg2,6);
for(i=1;i<=2;i++){
read(file[0],buf,6);
printf("\n%s",buf);}
return 0;
}
Output:
As Global Variable:
HELLLO
NONONO
As local variable:
HELLLO▒▒▒
NONONO▒▒▒

Resources