I'm trying to return an array of integers from a function, sort the numbers then pass everything back to main. I haven't allocated and freed memory in this piece of code. I was just trying to see if it would actually work. The compiler flags an error for the statement b=sort(a). It says that it is not assignable, which would make sense. The input integers are not pointers. Is there a way to declare an array of integers as pointers? such as :
int *a[5]={3,4}
#include <stdio.h>
#include <stdlib.h>
int *sort(int *input_array);
int *sort(int *input_array)
{
return input_array;
}
int main()
{
int a[5]={3,4};
int b[5];
b=sort(a);
return 0;
}
When you create an array, you cannot assign to the array itself (only to the elements). Besides, since when you pass an array, you're passing it by reference, sort() would modify the array, making it unneeded to return it.
What you're looking for is either of: sorting the original array, which would be like this:
void sort (int * array);
void sort (int * array) {
// do stuff on the array
}
int main (void) {
int a[5] = {1, 46, 52, -2, 33};
sort(a); // result is still in a
return 0;
}
Or creating a copy and sorting it, which would be like this:
#include <stdlib.h>
#include <string.h>
int * sort (int * array, unsigned size);
int * sort (int * array, unsigned size) {
int * copy = malloc(sizeof(int) * size);
memcpy(copy, array, size * sizeof(int));
// sort it somehow
return copy;
}
int main (void) {
int a[5] = {1, 46, 52, -2, 33};
int * b; // pointer because I need to assign to the pointer itself
b = sort(a, (sizeof a) / (sizeof *a)); // now result is in b, a is unchanged
// do something with b
free(b); // you have to
return 0;
}
You can't assign arrays, they're not "first class citizens" but instead behave much like pointers.
You need something like:
int a[] = { 3, 4 };
int *b;
b = sort(a, sizeof a / sizeof *a);
The sizeof expression is needed to compute the length of the array, the sort() function can't determine that from the bare pointer it gets passed.
UPDATE: The above assumes that you won't be changing the input array, but if you do then (as pointed out in a comment, thanks) the return value is of course not needed since the caller's a will have changed when the sort() call returns.
If you are passing array - a pointer of int, you don't need to return a changed array. The array that you passed will get changed.
As #unwind suggested, you should pass number of elements to the function also so that the function knows how many elements are there in the array.
You can't return an array of anything in C. You can only return a single instance of a single datatype.
That datatype can be a pointer to memory storing a sequential list of numbers (or anything else), but you lose all information about how long the result is, so you either need to know that, or you have to have another value as an output variable to tell you the length.
You can also return a custom datatype, such as a struct, that would contain both the list of data as well as the length. However, returning a large datastructure creates multiple shallow copies of the data structure, slowing down execution of your program as well as creating memory nightmares with leaks and multiple references.
Returning a pointer to a custom data structure, however, can work very well.
Related
I tried this:
int* test()
{
static int states[2]= {4,7};
return states;
}
And called it like this:
int* values = test();
But it only seems to return the first value - 4 - and not the whole array.
I tried to do it exactly as I saw in other examples so I'm confused as to why it doesn't work. I'm using the STM32cubeIDE to write and compile, if that makes a difference.
Normal arrays can't be returned from a function because their lifetime ends when the function returns, the behavior for accessing one of these local arrays outside the scope of the function is undefined.
In your particular case this is possible because your array has static storage duration.
In C a function can only return one element, so to return an array you must return a pointer which can contain the address of the first element of that array. And that's what you're doing in your code. You can access both values by correctly indexing the returned pointer i.e. values[0] and values[1].
Unfortunately this is not without its issues, the size of the array is not known by the caller and you can't safely index it because you don't know its bounds.
There are ways solve this which are not all that complicated once you get used to them. Most notably defining a global size for the array1, using a structure containing the size of the array and a pointer to the array itself2, or passing pointers to the size and/or the array as arguments of the function3.
1. Using a global variable that stores its size:
#define SIZE 2
int *test()
{
static int states[SIZE] = {4, 7};
return states; //returns a pointer to the first element of the array
}
int main()
{
int* values = test(); // values is now pointing to the array
for(size_t i = 0; i < SIZE; i++){
printf("%d ", values[i]); //indexing is similar to a local array
}
}
2. Using a struct to store both the size and a pointer to the array:
typedef struct{ //structure to hold the data
int *array;
size_t size;
} array_struct;
array_struct test()
{
static int states[2] = {4, 7};
array_struct array = {.array = states, .size = 2}; //assing pointer and size
return array; //return the structure
}
int main()
{
array_struct values = test(); //assing the structure to a local
for(size_t i = 0; i < values.size; i++){ //use the size passed
printf("%d ", values.array[i]);
}
}
Output:
4 7
Option 3 is laid out in Bathsheba's answer.
You get back a pointer to the first element of the array due to the decay of the array type to a pointer type.
You obtain the other elements by pointer arithmetic.
Unfortunately though all size information is lost so you don't know at the call site how many elements you have. One way round that would be to change the function to
void test(int** array, size_t* length)
with *array = states and *length = sizeof(states) / sizeof(states[0]) in the function body.
How do I correctly return an array from a function?
You don't because you cannot. Read the C11 standard n1570.
(since as return values, arrays are decayed to pointers)
In practice, you could use a flexible array member in your struct and return some malloc-ed pointer to that struct. You then need a documented convention about who will free that pointer.
Read about C dynamic memory allocation. For more details and example code, see this answer.
I tried something like :
typedef struct vec{
int sz;
int v[];
} ff;
int sum(struct vec z){
int o=0,i;
for(i=0;i<z.sz;i++)
o+=z.v[i];
return o;
}
int main(){
int test[]={10,1,2,3,4,5,6,7,8,9,10};
return sum((struct vec)test);
}
But this example code can't compile. How to send array by value (not ref throw pointer) to function?
You can't send an array by value in C. Every time you try to pass an array to a function, you'll pass the pointer to the first value.
To pass an array by value, you can cheat a little and encapsulate it in a struct, and send it by value (so it will be copied)... It's a dirty hack, as explained here, but well, if you really want that, it works.
Another way is to copy the array inside the function, with the pointer given.
In your example, you will need to specify the exact size of the array in the definition of struct vec, for example int v[10]. Also your initialization can be written better. Try this:
#define MAX_SIZE 50
struct vec {
int sz;
int v[MAX_SIZE];
};
int sum(struct vec z){
int i, o;
o = 0;
for(i=0; i<z.sz; i++) o += z.v[i];
return o;
}
int main(){
struct vec test = {10, {1,2,3,4,5,6,7,8,9,10}};
return sum(test);
}
While it's true that an array in C has a size, that size can only be accessed through the sizeof operator, and then only on the original array. You can't cast an array to a structure in the hopes that the compiler will fill in all fields.
In fact, it's actually impossible to pass an array to a function "by value". All arrays decays to pointers, so when you pass an array to a function that function receives a pointer. This decay to pointers is also why you can only use the sizeof operator on the original array, once it decays to a pointer the compiler have no idea that it's actually pointing to an array, so using the sizeof operator (even on something that points to an array) will return the size of the pointer and not what it points to.
In your case, it would be simpler to change your function to take two arguments, the pointer to the array and the number of entries in the array:
/* `array` is an array containing `elements` number of `int` elements. */
int sum(const int *array, const size_t elements)
{
...
}
int main(void)
{
...
printf("Sum = %d\n", sum(test, sizeof(test) / sizeof(test[0]));
}
Arrays are always passed by reference in C, you can't change that.
What you can do is make a copy of array and pass the new array in the function
As the others noted, the only way in C to pass an array by value is to use a struct with a fixed size array. But this is not a good idea, because the array size will be hardcoded into the struct definition.
However, you really should not try to pass arrays by value. Instead, what you should do is simply to declare your pointer parameter as const, like this:
int sum(size_t count, const int* array) {
While this does not pass the array by value, it clarifies the intent not to change the array, i. e. you can now treat sum() as if you were passing the array by value.
If your function does indeed not change the array (like your sum() function), using a const pointer comes for free, copying the array to pass it by value would just waste time.
If your function really wants to change a private copy, do so explicitely within the function. If the array won't get too large, you can simply allocate it on the stack, like so:
int doSomethingWithPrivateCopy(size_t count, const int* array) {
int mutableCopy[count];
memcpy(mutableCopy, array, sizeof(mutableCopy));
//Now do anything you like with the data in mutable copy.
}
This question already has answers here:
Why can't I assign arrays as &a = &b?
(2 answers)
Closed 9 years ago.
I wrote this piece of code
#include <stdio.h>
struct foo
{
int foo1[3];
};
int main(void)
{
int a[] = {1, 2, 3};
struct foo test;
test.foo1 = a;
printf("%d\n", test.foo1[0]);
return 0;
}
It gives compile error saying that it cannot convert int * to int[3].
I know that array names will decay into pointers in expressions, but is there a way of suppressing that since I do need an array here?
As the others said, there is not direct assignment operators that will copy an array. You have to use memcpy() instead
memcpy(test.foo1, a, sizeof(a));
This is one source of errors in C because the sizeof() needs to be large enough to copy all the data but not too large so as not to overwrite data at tests.foo1. The best practice, I would imagine, is to test that both arrays are the same size before doing the memcpy().
This is one of the basics of C, arrays cannot be assigned.
In C and in C++ there is no way to assign a value to an whole array. It is also not possible to assign an array the values of an other array (even if the dimension would match).
You cannot assign arrays in C. However, you can assign objects of user-defined type, even if those contain an array. So peraps rewrite it like this:
struct foo a = { { 1, 2, 3 } };
struct foo test;
test = a;
Or of course just initialize the object correctly immediately:
struct foo test = { { 1, 2, 3 } };
Arrays are not first class objects in C. You can't copy (assign), compare, pass, or return an array. You copy an array into another array element by element. You also compare two arrays element by element. You pass a pointer to a first element of the array and similarly return a pointer to the first element of a dynamically allocated array. Therefore test.foo1 = a; is wrong. You have two choices here.
#include <stdio.h>
struct foo {
int foo1[3];
};
int main(void) {
int a[] = {1, 2, 3};
struct foo test;
int len = *(&(test.foo1) + 1) - test.foo1; // length of the array test.foo1
int i = 0;
for(i = 0; i < len; i++)
test.foo1[i] = a[i]; // copy the array element-wise
printf("%d\n", test.foo1[0]);
return 0;
}
You can also directly copy all the bytes from the array a in main to the array test.foo1 using memcpy.
memcpy(test.foo1, a, sizeof a);
This copies all the bytes of the array a into the array test.foo1. Therefore the array test.foo1 must be large enough else it will lead to undefined behaviour or even segfault.
So... I have a dynamically allocated array on my main:
int main()
{
int *array;
int len;
array = (int *) malloc(len * sizeof(int));
...
return EXIT_SUCCESS;
}
I also wanna build a function that does something with this dynamically allocated array.
So far my function is:
void myFunction(int array[], ...)
{
array[position] = value;
}
If I declare it as:
void myFunction(int *array, ...);
Will I still be able to do:
array[position] = value;
Or I will have to do:
*array[position] = value;
...?
Also, if I am working with a dynamically allocated matrix, which one is the correct way to declare the function prototype:
void myFunction(int matrix[][], ...);
Or
void myFunction(int **matrix, ...);
...?
If I declare it as:
void myFunction(int *array, ...);
Will I still be able to do:
array[position] = value;
Yes - this is legal syntax.
Also, if I am working with a dynamically allocated matrix, which one
is correct to declare the function prototype:
void myFunction(int matrix[][], ...);
Or
void myFunction(int **matrix, ...);
...?
If you're working with more than one dimension, you'll have to declare the size of all but the first dimension in the function declaration, like so:
void myFunction(int matrix[][100], ...);
This syntax won't do what you think it does:
void myFunction(int **matrix, ...);
matrix[i][j] = ...
This declares a parameter named matrix that is a pointer to a pointer to int; attempting to dereference using matrix[i][j] will likely cause a segmentation fault.
This is one of the many difficulties of working with a multi-dimensional array in C.
Here is a helpful SO question addressing this topic:
Define a matrix and pass it to a function in C
Yes, please use array[position], even if the parameter type is int *array. The alternative you gave (*array[position]) is actually invalid in this case since the [] operator takes precedence over the * operator, making it equivalent to *(array[position]) which is trying to dereference the value of a[position], not it's address.
It gets a little more complicated for multi-dimensional arrays but you can do it:
int m = 10, n = 5;
int matrixOnStack[m][n];
matrixOnStack[0][0] = 0; // OK
matrixOnStack[m-1][n-1] = 0; // OK
// matrixOnStack[10][5] = 0; // Not OK. Compiler may not complain
// but nearby data structures might.
int (*matrixInHeap)[n] = malloc(sizeof(int[m][n]));
matrixInHeap[0][0] = 0; // OK
matrixInHeap[m-1][n-1] = 0; // OK
// matrixInHeap[10][5] = 0; // Not OK. coloring outside the lines again.
The way the matrixInHeap declaration should be interpreted is that the 'thing' pointed to by matrixInHeap is an array of n int values, so sizeof(*matrixInHeap) == n * sizeof(int), or the size of an entire row in the matrix. matrixInHeap[2][4] works because matrixInHeap[2] is advancing the address matrixInHeap by 2 * sizeof(*matrixInHeap), which skips two full rows of n integers, resulting in the address of the 3rd row, and then the final [4] selects the fifth element from the third row. (remember that array indices start at 0 and not 1)
You can use the same type when pointing to normal multidimensional c-arrays, (assuming you already know the size):
int (*matrixPointer)[n] = matrixOnStack || matrixInHeap;
Now lets say you want to have a function that takes one of these variably sized matrices as a parameter. When the variables were declared earlier the type had some information about the size (both dimensions in the stack example, and the last dimension n in the heap example). So the parameter type in the function definition is going to need that n value, which we can actually do, as long as we include it as a separate parameter, defining the function like this:
void fillWithZeros(int m, int n, int (*matrix)[n]) {
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
matrix[i][j] = 0;
}
If we don't need the m value inside the function, we could leave it out entirely, just as long as we keep n:
bool isZeroAtLocation(int n, int (*matrix)[n], int i, int j) {
return matrix[i][j] == 0;
}
And then we just include the size when calling the functions:
fillWithZeros(m, n, matrixPointer);
assert(isZeroAtLocation(n, matrixPointer, 0, 0));
It may feel a little like we're doing the compilers work for it, especially in cases where we don't use n inside the function body at all (or only as a parameter to similar functions), but at least it works.
One last point regarding readability: using malloc(sizeof(int[len])) is equivalent to malloc(len * sizeof(int)) (and anybody who tells you otherwise doesn't understand structure padding in c) but the first way of writing it makes it obvious to the reader that we are talking about an array. The same goes for malloc(sizeof(int[m][n])) and malloc(m * n * sizeof(int)).
Will I still be able to do:
array[position] = value;
Yes, because the index operator p[i] is 100% identical to *(ptr + i). You can in fact write 5[array] instead of array[5] and it will still work. In C arrays are actually just pointers. The only thing that makes an array definition different from a pointer is, that if you take a sizeof of a "true" array identifier, it gives you the actual storage size allocates, while taking the sizeof of a pointer will just give you the size of the pointer, which is usually the system's integer size (can be different though).
Also, if I am working with a dynamically allocated matrix, which one is the correct way to declare the function prototype: (…)
Neither of them because those are arrays of pointers to arrays, which can be non-contigous. For performance reasons you want matrices to be contiguous. So you just write
void foo(int matrix[])
and internally calculate the right offset, like
matrix[width*j + i]
Note that writing this using the bracket syntax looks weird. Also take note that if you take the sizeof of an pointer or an "array of unspecified length" function parameter you'll get the size of a pointer.
No, you'd just keep using array[position] = value.
In the end, there's no real difference whether you're declaring a parameter as int *something or int something[]. Both will work, because an array definition is just some hidden pointer math.
However, there's is one difference regarding how code can be understood:
int array[] always denotes an array (it might be just one element long though).
int *pointer however could be a pointer to a single integer or a whole array of integers.
As far as addressing/representation goes: pointer == array == &array[0]
If you're working with multiple dimensions, things are a little bit different, because C forces you declare the last dimension, if you're defining multidimensional arrays explicitly:
int **myStuff1; // valid
int *myStuff2[]; // valid
int myStuff3[][]; // invalid
int myStuff4[][5]; // valid
Below is a snippet from the book C Programming Just the FAQs. Isn't this wrong as Arrays can never be passed by value?
VIII.6: How can you pass an array to a function by value?
Answer: An array can be passed to a function by value by declaring in
the called function the array name
with square brackets ([ and ])
attached to the end. When calling the
function, simply pass the address of
the array (that is, the array’s name)
to the called function. For instance,
the following program passes the array
x[] to the function named
byval_func() by value:
The int[] parameter tells the
compiler that the byval_func()
function will take one argument—an
array of integers. When the
byval_func() function is called, you
pass the address of the array to
byval_func():
byval_func(x);
Because the array is being passed by
value, an exact copy of the array is
made and placed on the stack. The
called function then receives this
copy of the array and can print it.
Because the array passed to
byval_func() is a copy of the
original array, modifying the array
within the byval_func() function has
no effect on the original array.
Because the array is being passed by value, an exact copy of the array is made and placed on the stack.
This is incorrect: the array itself is not being copied, only a copy of the pointer to its address is passed to the callee (placed on the stack). (Regardless of whether you declare the parameter as int[] or int*, it decays into a pointer.) This allows you to modify the contents of the array from within the called function. Thus, this
Because the array passed to byval_func() is a copy of the original array, modifying the array within the byval_func() function has no effect on the original array.
is plain wrong (kudos to #Jonathan Leffler for his comment below). However, reassigning the pointer inside the function will not change the pointer to the original array outside the function.
Burn that book. If you want a real C FAQ that wasn't written by a beginner programmer, use this one: http://c-faq.com/aryptr/index.html.
Syntax-wise, strictly speaking you cannot pass an array by value in C.
void func (int* x); /* this is a pointer */
void func (int x[]); /* this is a pointer */
void func (int x[10]); /* this is a pointer */
However, for the record there is a dirty trick in C that does allow you to pass an array by value in C. Don't try this at home! Because despite this trick, there is still never a reason to pass an array by value.
typedef struct
{
int my_array[10];
} Array_by_val;
void func (Array_by_val x);
Isn't this wrong as arrays can never be passed by value?
Exactly. You cannot pass an array by value in C.
I took a look at the quoted part of the book and the source of this confusion or mistake is pretty fast found.
The author did not know about that *i is equivalent to i[] when provided as a parameter to a function. The latter form was invented to explicitly illustrate the reader of the code, that i points to an array, which is a great source of confusion, as well-shown by this question.
What I think is funny, that the author of the particular part of the book or at least one of the other parts (because the book has 5 authors in total) or one of the 7 proofreaders did not mentioned at least the sentence:
"When the byval_func() function is called, you pass the address of the array to byval_func():"
With at least that, they should had noticed that there is a conflict.
Since you passing an address, it is only an address. There is nothing magically happen which turns an address into a whole new array.
But back to the question itself:
You can not pass an array as it is by value in C, as you already seem to know yourself. But you can do three (there might be more, but that is my acutal status of it) things, which might be an alternative depending on the unique case, so let´s start.
Encapsulate an array in a structure (as mentioned by other answers):
#include <stdio.h>
struct a_s {
int a[20];
};
void foo (struct a_s a)
{
size_t length = sizeof a.a / sizeof *a.a;
for(size_t i = 0; i < length; i++)
{
printf("%d\n",a.a[i]);
}
}
int main()
{
struct a_s array;
size_t length = sizeof array.a / sizeof *array.a;
for(size_t i = 0; i < length; i++)
{
array.a[i] = 15;
}
foo(array);
}
Pass by pointer but also add a parameter for determine the size of the array. In the called function there is made a new array with that size information and assigned with the values from the array in the caller:
#include <stdio.h>
void foo (int *array, size_t length)
{
int b[length];
for(size_t i = 0; i < length; i++)
{
b[i] = array[i];
printf("%d\n",b[i]);
}
}
int main()
{
int a[10] = {0,1,2,3,4,5,6,7,8,9};
foo(a,(sizeof a / sizeof *a));
}
Avoid to define local arrays and just use one array with global scope:
#include <stdio.h>
int a[10];
size_t length = sizeof a / sizeof *a;
void foo (void)
{
for(size_t i = 0; i < length; i++)
{
printf("%d\n",a[i]);
}
}
int main()
{
for(size_t i = 0; i < length; i++)
{
a[i] = 25;
}
foo();
}
In C and C++ it is NOT possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address. In practice this has almost the same effect and it is a much faster and more efficient operation.
To be safe, you can pass the array size or put const qualifier before the pointer to make sure the callee won't change it.
Yuo can work it around by wrapping the array into the struct
#include <stdint.h>
#include <stdio.h>
struct wrap
{
int x[1000];
};
struct wrap foo(struct wrap x)
{
struct wrap y;
for(int index = 0; index < 1000; index ++)
y.x[index] = x.x[index] * x.x[index];
return y;
}
int main ()
{
struct wrap y;
for(int index = 0; index < 1000; index ++)
y.x[index] = rand();
y = foo(y);
for(int index = 0; index < 1000; index ++)
{
printf("%d %s", y.x[index], !(index % 30) ? "\n" : "");
}
}
#include<stdio.h>
void fun(int a[],int n);
int main()
{
int a[5]={1,2,3,4,5};
fun(a,5);
}
void fun(int a[],int n)
{
int i;
for(i=0;i<=n-1;i++)
printf("value=%d\n",a[i]);
}
By this method we can pass the array by value, but actually the array is accessing through the its base address which actually copying in the stack.