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

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().

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 do I send an array as function argument? [duplicate]

This question already has answers here:
How to pass a constant array literal to a function that takes a pointer without using a variable C/C++?
(10 answers)
Closed 5 years ago.
So , i'm trying to make a simple program in C using arrays.
int odd(int v1[],int n) {
int v2[n];
int i;
for (i=0;i<n;i++) {
if (v1[i]%2==0) {
v2[i]=v1[i];
}
else {
v2[i]=v1[i]*2;
}
}
for (i=0;i<n;i++) {
printf("Array %d",v2[i]);
}
return 0;
}
int main() {
odd({1,2,3,4,5},5);
return 0;
}
I get an error in the main function ('Expected expression') and i don't know how to correct.
An expression like
{1,2,3,4,5}
is not an array, on it's own. It's a brace-enclosed list, which is primarily used for initialization purpose.
If you don't want to define a separate array variable, you need to make use of compound literal. Something like
odd((int[]){1,2,3,4,5},5);
will do the job.

C: Are arrays that are passed into a function creating a copy of the array or is it modifying the original array [duplicate]

This question already has answers here:
Passing an array as an argument to a function in C
(11 answers)
Closed 6 years ago.
When I pass in an array as a parameter in a function in C, does it create a copy of the array or does it actually make changes to the original array? I have a small program here that translates a number into its binary form. For example, if the number is 15, the binary form would be 1111.
void convertToBinary(int base10, int result, char *binaryResult){
int binaryVals[8] = {1,2,4,8,16,32,64,128};
if(base10 == 0){
printf("Binary Result %s", binaryResult);
}
else{
int max = 0;
for(int i = 0; i < 8; i++){
if(binaryVals[i] <= base10){
binaryResult[i] = '0';
}
else{
max = binaryVals[i-1];
binaryResult[i-1] = '1';
result = base10-max;
printf("Result %d", result);
break;
//convertToBinary(result,0, binaryResult);
}
}
}
}
int main(void){
char binaryResult[8];
convertToBinary(15,0,binaryResult);
}
The recursion part is failing me. I am not sure why. I suspect that it is because it is creating a copy of the array each time it runs the recursion. But I am not sure how to fix that. It would be great if someone could help, thanks
Arrays passed into functions always modify the original array. The function declarations:
void func( int* array);
void func( int array[]);
Are equivalent, because when you call these functions with an array argument the array decays into a pointer for both. Because you're passing a pointer, you're actually passing the array by reference, and the original array is modified.

Static array in C [duplicate]

This question already has answers here:
Strange C code - dynamic arrays?
(4 answers)
Closed 8 years ago.
I always knew that it was not possible to build a dynamic array in C without using malloc and free, so why is this code compiling and running correctly?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
printf("Insert a number: ");
scanf("%d", &a);
int array[a];
int i;
for(i=0; i<a; i++)
{
array[i] = rand();
}
for(i=0; i<a; i++)
{
printf("%d\t", array[i]);
}
puts("");
return 0;
}
I understand that this is not a really dynamic array since there is no way to change the size of "array" after it has been declared, nor it can be freed callling free() but still I always thought that the size of static array must be known at compile time which is clearly not the case here..
What you are using is variable length array. Which is supported by C99 and latter. But note that VLA has automatic storage duration unlike dynamic memory allocated by malloc family functions.
Also note that compile time allocation is not equivalent to static array. static array and static allocation are different.

Resources