How to return an array in c - c

I know this question has been asked before but cannot find it in the same manner as I will describe it here:
Its all about returning an one-dimensional array in c-language. In java its very easy:
double[] myFunction() {
double[] v = new double[10];
return v;
}
I know that the implementation in c is not the same. But as an array element can be considered as a pointer to the first element in that array I thought one could do the following implementation:
double (*myFunction()) {
double v[10];
return v;
}
This compiles fine in gcc but when I make a call of the function I get a compilation error.
SO my Question - how does one return a one-dimensional vector in c-language?
Thanks

sample code
#include <stdio.h>
#include <stdlib.h>
double *myFunction1(){
return malloc(10*sizeof(double));
}
double (*myFunction2())[10]{
double (*p)[10];
p = malloc(sizeof(double[10]));
return p;
}
typedef struct d10 {
double vec[10];
} D10;
D10 myFunction3(){//Make little sense
D10 v = {{0}};
v.vec[9]=1.25;
return v;
}
int main(){
double *v1 = myFunction1();
double (*v2)[10] = myFunction2();
D10 v3= myFunction3();
//do something
printf("%lf\n", v3.vec[9]);
v1[0] = 3.14;
(*v2)[0] = 3.14 * 2;
free(v1);
free(v2);
return 0;
}

Corrected:
You can't pass an array and you can't return an array you only pass an address to it or return a pointer to it first element:
double *someFunction()
{
double *output;
p = (double *)malloc(size*sizeof(double)); // size should be defined
//... do some process with the content pointed to by output
return output; // return the address pointed to by output
}
Or pass a pointer to it first element and do some process on the content pointed to.
void someFunction(double *output, int size)
{
//... do some process with the content pointed to by output
}

For one, declaring v in the function makes the array live only in that function. Once the function returns, that array is popped off the stack, and is likely to be modified after successive function calls.
The proper C solution is to use malloc to allocate an amount of space that you can use as an array, and return the pointer to that array.
double * myFunc(){
double * v = malloc(10*size of(double));
return v;
}
In Java, it was safe to return an array because the garbage collector could detect that the array was still in use, after it was popped off the stack.

how does one return a one-dimensional vector in c-language?
Technically speaking, one doesn't. C functions cannot return array types.
The main problem you are running into is that, in the C code, v only exists for the lifetime of the function; once the function exits, v no longer exists, so any pointer you return will not be valid.
For this to work, you'll have to do something similar to what the Java code is doing; you'll need to allocate the memory for the array from the heap (i.e., allocate it dynamically):
double *myfunction()
{
double *v = malloc( sizeof *v * 10 );
return v;
}
Note that C does not clean up after you, so you'll have to explicitly free that memory when you're done with it, such as:
int main( void )
{
double *arr = myfunction();
/**
* do stuff with arr
*/
free( arr );
return 0;
}

Related

How to use double pointers (pointer to pointer) for an array of structures properly in standard C?

I have an array of structures as a function parameter and the size of the array is dynamic. My coworker said that I'll have to use a double pointer since the values contained in the array of struct will be overwritten.
The parameter that will become a double pointer is the following :
xPIDConfig_t **pxPIDConfig
Here is what the structure looks like for the xPIDConfig_t :
typedef struct
{
ePIDType_t ePIDType;
/* Common fields for the different types of PID */
float fLowerSaturationLimit;
float fUpperSaturationLimit;
float fOldInput;
float fIError;
uint32_t ulDeltaTime;
eBool_t bSaturationEnable;
eBool_t bAntiWindupEnable;
eBool_t bNegativeErrorEmptyIError;
union
{
/* Parallel PID fields */
struct
{
float fProportionalGain;
float fIntegralGain;
float fDerivativeGain;
}xParallelPID;
/* Non-interactive PID fields */
struct
{
float fControllerGain;
uint32_t ulIntegralTime;
uint32_t ulDerivativeTime;
}xNonInteractivePID;
}xUniqueFields;
}xPIDConfig_t;
The size of the array of pxPIDConfig will vary.
But I am not sure how to malloc that double pointer or even how to use the function containing the double pointer.
I was just wondering if anyone had a good example of code of how to use a function with a double pointer array of variating size? and how to properly change the values contained in the array itself inside a function?
Right now this is how I change the values within the function :
pxPIDConfig->ePIDType = ePIDType;
pxPIDConfig->fOldInput = 0;
pxPIDConfig->fIError = 0;
pxPIDConfig->ulDeltaTime = ulDeltaTime;
pxPIDConfig->bSaturationEnable = bIsSaturationEnable;
pxPIDConfig->bAntiWindupEnable = bIsAntiWindupEnable;
pxPIDConfig->bNegativeErrorEmptyIError = bNegativeErrorEmptyIError;
when the pointer is double do I have to use double '->'? This is very confusing for me.
Thank you all for the help
/***************** EDIT ************************************
My function is working right now, but I got told I need to use memory allocation since the size of my arrays varies according to the number of loops I want to implement.
Here are the parameters of my function :
eError_t eControlCascadeInit( uint8_t ucNumberOfLoops, ePIDType_t *pePIDType, xPIDConfig_t **pxPIDConfig, float *pfLowerLimit, float *pfUpperLimit, uint32_t *pulDeltaTime, \
eBool_t *pbIsSaturationEnable, eBool_t *pbIsAntiWindupEnable, eBool_t *pbNegativeErrorEmptyIError, \
float *pfPGain, float *pfIGain, float *pfDGain, float *pfCGain, uint32_t *pulITime, uint32_t *pulDTime )
They're all arrays of size ucNumberOfLoops. All of them are read-only arrays, except for the pxPIDConfig one that is write-only. The function initializes all the xPIDConfig_t present in the array with the parameters passed to the function through array.
array[ 0 ] contains the parameters for the first PID controller being initialized.
array[ 1 ] contains the parameters for the second PID controller being initialized and so on...
It's like that for all the parameters in the function.
Hope it makes my question more clear?
Here you have an example of how to use double-pointer, to change the pointer in the function:
void allocate(xPIDConfig_t **array, size_t size)
{
*array = malloc(sizeof(**array) * size);
/* some examples how to access the struct members vi double pointer -*
(*array) -> ulDeltaTime = 100;
(**array).ulDeltaTime = 100;
(*(array + 5)) -> ulDeltaTime = 100;
array[5] -> ulDeltaTime = 100;
(*array[5]).ulDeltaTime = 100;
}
int main(void)
{
xPIDConfig_t *array;
allocate(&array, 100);
printf("%s\n", array ? "success" : "failure");
free(array);
}
You would only need a double pointer if the function reallocates the array to a different size. If the size isn't changing, you can just pass a pointer to (usually the first) element of the array, along with any size or index required by the function. For example:
extern void frobPidConfig(xPIDConfig_t *);
// 'frob' the xPIDConfig_t array elements from index a to b
void frobSomePidConfigs(xPIDConfig_t *pidconfigs, unsigned int a, unsigned int b)
{
unsigned int i;
for (i = a; i <= b; i++)
{
frobPidConfig(&pidConfigs[i]);
// Example of member access:
pidConfigs[i].ulDeltaTime = 42;
}
}
Example of calling code:
xPIDConfig_t *pidConfigs;
unsigned int n = 10; // or whatever...
pidConfigs = calloc(sizeof *pidConfigs, n);
if (!pidConfigs)
{
// Allocation error
exit(1);
}
/* ... */
frobSomePidConfigs(pidConfigs, 2, 5);
On the other hand, if the function needs to reallocate the array and initialize any new elements, it could be done using a double pointer like this:
extern void initPidConfig(xPIDConfig_t *);
void reallocPidConfigs(xPIDConfig_t **pidConfigs, unsigned int oldSize, unsigned int newSize)
{
unsigned int i;
// Reallocate to new size
xPIDConfig_t *realloced = realloc(*pidConfigs, sizeof **pidConfigs * newSize);
if (newSize && !realloced)
{
// allocation error
exit(EXIT_FAILURE);
}
*pidConfigs = realloced;
// Initialize any additional elements
for (i = oldSize; i < newSize; i++)
{
initPidConfig(*pidConfigs + i); // or: initPidConfig(&(*pidConfigs)[i]);
// Examples of member access:
(*pidConfigs)[i].bSaturationEnable = true;
(*pidConfigs + i)->bAntiWindupEnable = true;
}
}
Example of calling code:
xPIDConfig_t *pidConfigs = NULL;
// Note: realloc of the NULL pointer in *pidConfigs is OK.
reallocPidConfigs(&pidConfigs, 0, 10);
frobSomePidConfigs(pidConfigs, 2, 5);
Limited to addressing assumptions and questions regarding your title question:
"How to use double pointers (pointer to pointer) for an array of structures properly in standard C"
First, just because the function argument might have a double pointer (i.e. xPIDConfig_t **pxPIDConfig) does not mean that the variable need to be allocated memory with a double pointer, i.e. if the function eg is called like this: funcChangeParam(&pxPIDConfig); this often means that the object being passed needs to be changed in some way, requiring that the address of be passed, not the object itself. Also, if the object itself is a pointer, (such as a pointer to several instances of a struct object.) then the function used to pass the object for modification will be prototyped with arguments such as void funcChangeParam(xPIDConfig_t **pxPIDConfig); (Note the double pointer here.)
So with this function prototype Making the allocation of memory look like this:
void funcChangeParam(xPIDConfig_t **pxPIDConfig);
//allocate memory for multiple instances of struct
xPIDConfig_t *pxPIDConfig = malloc(countOfInstances * sizeof(*pxPIDConfig);
if(pxPIDConfig)
{
//use pxPIDConfig
funcChangeParam(&pxPIDConfig);pass pointer to multiple instances of struct
And references to the object members inside the calling function could use the following notation. Eg:
//in a loop or other construct where i is defined from 0 to countOfInstances - 1
(*pxPIDConfig)[i].ePIDType = ePIDType;//modification of assignment per your example
//etc.
//The following is a trivial example for illustration purposes.
//Code here uses a simplified struct, function
//prototype, and simple calling example, the concept
//of which easily translates to what you are
//asking about.
typedef struct {
int num;
}test_s;
void change(test_s **new);
int main(){
test_s *test = malloc(10*sizeof *test);
change(&test);
return 0;
}
void change(test_s **new)
{
for(int i=0;i<10;i++)
{
(*new)[i].num = (i+1)*3; //init all instances to some value
}
}

Warning: Return from incompatible pointer type

The code below is producing a compiler warning: return from incompatible pointer type. The type I'm returning seems to be the issue but I cant seem to fix this warning.
I have tried changing the type of hands to int *. Also have tried returning &hands.
int * dealDeck(int numPlayers, int numCards, int cardDeck[])
{
static int hands[MAX_PLAYERS][MAX_CARDS]={0};
int start = 0;
int end = numCards;
int player, hand, j;
int card;
for(player = 0; player < numPlayers; player++)
{
for(hand = start, j=0; hand < end; hand++,j++)
{
card = cardDeck[hand];
hands[player][j] = card;
}
start = end;
end += numCards;
}
return hands;
}
This function should return a pointer to the array "hands". This array is then passed to another function which will print out its elements.
The hands variable is not an int * this is a int **
So you need to return a int **
This is a 2d array.
First of all, you have declared return type of int *, which would mean, that you are trying to return an array, while you want to return a 2-dimensional array. The proper type for this would usually be int **, but that won't cut it here. You opted to go with static, fixed size array. That means, that you need to return pointer to some structures of size MAX_CARDS * sizeof(int) (and proper type, which is the real problem here). AFAIK, there is no way to specify that return type in C*.
There are many alternatives though. You could keep the static approach, if you specify only up to 1 size (static int *hands[MAX_PLAYERS] or static int **hands), but then you need to dynamically allocate the inner arrays.
The sane way to do it is usually "call by reference", where you define the array normally before calling the function and you pass it as a parameter to the function. The function then directly modifies the outside variables. While it will help massively, with the maintainability of your code, I was surprised to find out, that it doesn't get rid of the warning. That means, that the best solution is probably to dynamically allocate the array, before calling the function and then pass it as an argument to the function, so it can access it. This also solves the question of whether the array needs to be initialized, and whether = {0} is well readable way to do it (for multidimensional array) , since you'll have to initialize it "manually".
Example:
#include <stdio.h>
#include <stdlib.h>
#define PLAYERS 10
#define DECKS 20
void foo(int **bar)
{
bar[0][0] = 777;
printf("%d", bar[0][0]);
/*
* no point in returning the array you were already given
* but for the purposes of curiosity you could change the type from
* void to int ** and "return bar;"
*/
}
int main()
{
int **arr;
arr = malloc(sizeof(int *) * PLAYERS);
for (size_t d = 0; d < DECKS; d++) {
/* calloc() here if you need the zero initialization */
arr[d] = malloc(sizeof(int) * DECKS);
}
foo(arr);
return 0;
}
*some compilers call such type like int (*)[20], but that isn't valid C syntax

Array slicing in C

In Python, if I wanted to assign the vector x=(1,2) to the first two elements of y=(0,0,0,0), I would do something like y[:1] = x. Is there an equivalent in C to assign a double x[2]={1.,2.} to the first two elements of an available double y[4] = {0.,0.,0.,0.}? Or will I have to loop?
Just try this
#include <string.h>
#include <stdio.h>
int main()
{
double x[2] = {1., 2.};
double y[4] = {0., 0., 0., 0.};
memcpy(y, x, 2 * sizeof(*x));
/* ^ 2 elements of size -> sizeof(double) */
return 0;
}
instead of writing sizeof(double) which is ok, i did sizeof(*x) because if I change the type of x I wouldn't need to fix the memcpy, but i would also have to change the type of y in that case.
You could write a function that passes in the pointer or array, an offset, and a length. The function then allocates space to a new pointer with malloc() and does a memcpy() operation or loop-and-copy, although memcpy() is probably better. The new pointer is returned to the caller.
Yes, it is possible using the function memcpy. But you have to be careful about data types. You can also copy data of one type to another. Again as I said, you need to be careful. Otherwise it may generate garbage value.
And second option is looping as mentioned in question.
Try this. A bit more low-level than your python method but welcome to C where sooner or later everything is block of raw memory to be shunted around!
The macro arraycopy applies some size checking.
The function arraycopyunchecked is fairly raw.
The functions are overlap safe. Under the hood they use memcopy(,,) which may be slower but won't have surprise results if unexpected aliasing results in overlapping arrrays.
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //includes mempcy. I know! I know!
#define arraycopy(dest,doffset,source,soffset,count) arraycopy_((dest),(doffset),(source),(soffset),(count),sizeof(*(dest)),sizeof(*(source)))
int arraycopy_(void*const dest,const size_t doffset,const void*const source, const size_t soffset,const size_t count,const size_t size,const size_t sizecheck){
if(size!=sizecheck){
return 1;//Incompatible types.
}
memcopy(((char*)dest)+doffset*size,((const char*)source)+soffset*size,count*size);
return 0;
}
int arraycopyunchecked(void*const dest,const size_t doffset,const void*const source, const size_t soffset,const size_t count,const size_t size){
memcopy(((char*)dest)+doffset*size,((const char*)source)+soffset*size,count*size);
return 0;
}
int main(void) {
int errors=0;
double dest[]={0.0,0.0,0.0,0.0};
double source[]={1.0,2.0};
arraycopy(dest,0,source,0,2);
if(dest[0]!=1.0){
++errors;
}
if(dest[1]!=2.0){
++errors;
}
if(dest[2]!=0.0){
++errors;
}
arraycopy(dest,1,source,0,2);
if(dest[0]!=1.0){
++errors;
}
if(dest[1]!=1.0){
++errors;
}
if(dest[2]!=2.0){
++errors;
}
if(errors!=0){
printf("Errors - %d\n",errors);
}else{
printf("SUCCESS");
}
double dest1[]={0.0,0.0,0.0,0.0,0.0};
double source1[]={1.0,2.0,3.0};
arraycopy(dest1,2,source1,1,2);
if(dest[2]!=2.0){
++errors;
}
if(dest[3]!=3.0){
++errors;
}
return errors==0?EXIT_SUCCESS:EXIT_FAILURE;
}

Returning an Array from a Function in C

I'm just making a mess out of this. I have a function that is supposed to take a one-dimensional array, do some calculations with its values, and then return a similar array with the results of the calculation. I don't necessarily care whether it returns the same array (with new values) or if it creates a new array at a different memory location and returns that. Here is what I've got at the moment. There are errors all over this, but I don't know what I am doing wrong. Can anyone help?
double s = 10;
double b = 2.6666;
double r = 28;
double (*newVertex(double vtx[3] )) [] {
static double newVtx[3];
/* Coordinates */
double x = vtx[0];
double y = vtx[1];
double z = vtx[2];
double dt = 0.001;
double dx = s*(y-x);
double dy = x*(r-z)-y;
double dz = x*y - b*z;
newVtx[0] = x + dt*dx;
newVtx[1] = y + dt*dy;
newVtx[2] = z + dt*dz;
return &newVtx;
}
int main(int argc, char *argv[]) {
int i;
/* Arrays to hold the coordinates */
double thisPt[3] = {1, 1, 1};
double nextPt[3];
for (i=0;i<1000;i++) {
printf("%5d %8.3f %8.3f %8.3f\n", i, thisPt[0], thisPt[1], thisPt[2]);
nextPt = newVertex(&thisPt);
thisPt = nextPt;
}
return 0;
}
First of all, your function declaration looks unnecessarily complex to me.
If you're not planning to create a new array, then it should be something like:
void function_name(double *parameter) {
// code to change the parameter in place here
}
or, if you want to be explicit about the length of the array (see comments for additional info):
#define ARRAY_SIZE 3
void function_name(double parameter[ARRAY_SIZE]) {
// code to change the parameter in place here
}
If you're planning to create a new array, then you could do something like:
double * function_name(double *parameter) {
double *result = (double *)malloc(sizeof(double * number_of_elements));
// read parameter, write into result
return result;
}
The above snippet assumes the number_of_elements is fixed and known. If it is not, then you need to handle them as additional arguments.
Next, this is bad for several reasons:
double (*newVertex(double vtx[3] )) [] {
static double newVtx[3];
// update newVtx
return &newVtx;
}
The return statement returns the address of a local variable. In this particular case, the variable is static, so the variable won't be overwritten once the function exits. But does it really need to be static in the first place? And is it sufficient for it to be static? Think about code like this:
double *v1 = newVertex(old_vertex);
double *v2 = newVertex(old_vertex);
You may be tempted to think you can handle the two vertices individually, but they're pointing to the exact same spot in memory: the location of the static variable. It's much more common practice to allocate space for the array dynamically (malloc, calloc) and return a pointer to the allocated memory.
Here nextPt = newVertex(&thisPt);
just pass array name
newVertex(thisPt); //array name thispt==&thispt[0]
thisPt = nextPt; //illegal and remove this line
Your function
void newVertex(double *); //declaration
void newVertex(double *vtx) //defination
{
//donot return array
}
print after function call
newVertex(thisPt);
printf("%5d %8.3f %8.3f %8.3f\n", i, thisPt[0], thisPt[1], thisPt[2]);

understanding how to dynamically create an array of structure and access its elements

I need to pass the address of a pointer to a structure to a function, which inturn will dynamically allocate the memory for an array of structures and fill in the values.
Now from my calling method, once i return from the func1, i should be able to iterate through the array of structure and display the value of the structure variables.
Can someone explain how to pass the address of the pointer to the structure, also iterating through the array of structures created dynamically ?
my sample code looks like this:
struct test {
int a;
int b;
};
void func1(int *n,struct test **testobj)
{
n=5;
*testobj = (struct test*) malloc(n*sizeof(struct test));
for(i=0;i<n;i++)
{
(*testobj)[i].a=1;
(*testobj)[i].b=2;
}
}
int main()
{
struct test testobj;int n;
func1(&n,&testobj);
for(i=0;i<n;i++)
{
printf("%d %d",(*testobj)[i].a,*testobj)[i].b);
}
free(testobj);
}
In main() define a pointer to a test structure:
struct test *testPtr;
To take the address of that pointer use the & address-of operator:
&testPtr;
This returns the address of the pointer and has type struct test **
You can then pass this into your function func1, which does the correct allocation (although casting malloc() is generally considered bad practice - Do I cast the result of malloc?). Other than that func1() looks good... the line...
*testobj = malloc(n*sizeof(struct test));
... is correct. *testobj dereferences your double pointer that you got by doing &testPtr, and stores the address of the new memory in your pointer. You are also correct when you dereference your double-pointer using (*testobj)[i] because [] has higher precedence than * you needed to (as you've correctly done) surround the dereference with brackets to make sure that happens before you take the index.
Thus, when func1() returns the pointer testPtr should now point to the array of n test structures you allocated and can be accessed using testPtr[i].a etc.
EDIT: Your for loop should become
for(i=0;i<n;i++)
printf("%d %d", testobj[i].a, testobj[i].b);
Your original for loop should have given you compilation errors? In the original code testobj is not a pointer, therefore dereferencing it should not be possible.
So the summary answer is in main() declare testobj as a pointer and then access the array elements as testobj[n] :)
EDIT: As eric has pointed out, remove n=5; from func1(). I think you meant *n=5 perhaps as some kind of debugging step... You probably mean to use n as the input to the function to say how many objects you want in your structure array. Either initialise n or perhaps re-define func1() to be
void func1(int n,struct test **testobj) // n is no longer a poitner, just a number
create your array of pointers to structures in declaration step itself and simply pass it to the function
struct test *testobj[10];
func1(&n,testobj);
This passes the whole array of pointers to the function
It isn't entirely clear which version you're asking for, but one of these should cover it:
/* allocate some number of tests.
*
* out_n: out parameter with array count
* returns: an array of tests
*/
struct test* allocate_some_tests(int *out_n) {
int n = 5; /* hardcoded, random or otherwise unknown to caller */
*out_n = n
struct test *t = malloc(n * sizeof(*t));
while (n--) {
t[n].a = 1;
t[n].b = 2;
}
return t;
}
/* allocate a specific number of tests.
*
* n: in parameter with desired array count
* returns: an array of tests
*/
struct test* allocate_n_tests(int n) {
struct test *t = malloc(n * sizeof(*t));
while (n--) {
t[n].a = 1;
t[n].b = 2;
}
return t;
}
Note that you can just return the allocated array, you don't need a pointer-to-pointer here.
As for calling them, and iterating over the result:
void print_tests(struct test *t, int n) {
for (; n--; t++)
printf("{%d, %d}\n", t->a, t->b);
}
int main()
{
int count1; /* I don't know how many yet */
struct test *array1 = allocate_some_tests(&count1);
print_tests(array1, count1);
int count2 = 3; /* I choose the number */
struct test *array2 = allocate_n_tests(count2);
print_tests(array2, count2);
}
Your code appears pretty much ok to me.
only edit that should make it fine is--
in place of
struct test testobj;
put the following code
struct test *testobj;
and keep the remaining as it is..!
here's the working version of what's required, here the memory is allocated in the called function just as required
#include <stdlib.h>
#include <stdio.h>
struct tests {
int a;
int b;
};
void func1(int *n,struct tests **testobj)
{
int i;
*n=5;
*testobj = (struct tests*) malloc((*n)*sizeof(struct tests));
for(i=0;i<(*n);i++)
{
(*testobj)[i].a=1;
(*testobj)[i].b=2;
}
}
int main()
{
int i;
struct tests *testobj;int n;
func1(&n,&testobj);
for(i=0;i<(n);i++)
{
printf("%d %d",(testobj)[i].a,testobj[i].b);
}
free(testobj);
}

Resources