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

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.

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

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

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.

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

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▒▒▒

Adding elements to an array through a function call [duplicate]

This question already has answers here:
Access array beyond the limit in C and C++ [duplicate]
(7 answers)
Closed 6 years ago.
Can someone explain to me why this code works:
#include <stdio.h>
void set_array(int array[3]);
int main()
{
int a[3] = {1, 2, 3};
set_array(a);
for (int i = 0; i < 4; i++)
{
printf("%d\n", a[i]);
}
}
void set_array(int array[3])
{
array[3] = 4;
}
How is it possible that I can add an element to an array through a function call? Can someone explain to me what's happening behind the curtains here?
Thanks in advance.
You can't, you need to allocate the array using malloc() and then use realloc().

Resources