I want to pass a float array from one function to another function
I want to make use of the float array type(here container)

void funct1()
{
float container[4]={0.1,0.2,0.3,0.4}

void funct2()         ----(1)
{return container;}


}

void funct3()
{

void funct2();

use container array;

}void funct1()
{
float container[4]={0.1,0.2,0.3,0.4}

void funct2()         ----(1)
{return container;}


}

void funct3()
{

void funct2();

use container array;

}

Here what would be the funct2() like

void funct2(container) ----(1)

void funct2(float container[]) ---(2)

use container array

Dani AI

Generated

asked how to get a float array from one function into another. The short answer: pass the array (which decays to a pointer) and also pass its length, and do not try to define a function inside another — nested functions are not standard C (as pointed out). 's example demonstrates passing an array to a printing function; the same pattern applies to any consumer function.

A typical, safe pattern (caller computes length and passes it) looks like this:

void use_array(const float arr[], size_t n);

int main(void) {
    float container[4] = {0.1f, 0.2f, 0.3f, 0.4f};
    use_array(container, sizeof container / sizeof container[0]);
}
void use_array(const float arr[], size_t n) {
    /* arr[0]..arr[n-1] */
}

Notes and alternatives:

  • In a parameter list float arr[] and float *arr are equivalent; the function receives a pointer (see Arrays in C).
  • You cannot return an array type by value. Options: have the caller provide a buffer (above), return a malloced pointer (caller must free; see malloc), or return a struct that contains an array (structs can be returned by value safely).
  • Never return a pointer to a local automatic array — it points to freed stack memory.
  • Declare function prototypes at file scope before calling them. Use const for read-only input arrays to document intent and help the compiler catch mistakes.

These patterns address the common pitfalls in the thread: correct parameter syntax, passing size, avoiding nested functions, and safe return strategies.

Recommended Answers

All 2 Replies

Thats one lot of mess, you should start indenting your code properly and perhaps what happened to your code tags. You will have to put your question properly in order to get more help. From what u have said i have come up with the sample code which will give you an idea on what need to be done

#include <stdio.h>
#define MAXSIZ    5

void PrintFloat(double []);

int main( void )
{
    double num[MAXSIZ] = { 12.23, 23.42, 45.66, 67.897, 786.86 };
    
    PrintFloat(num);
    
    getchar();
    return 0;
}

void PrintFloat(double num[])
{
     int i;
     
     for(i = 0; i < MAXSIZ; i++)
         printf("%.3f\t", num[i]);
}     

/* my output
12.230  23.420  45.660  67.897  786.860
*/

Dont define the function within the function. Its not a good programming practice, and perhaps u will have function scope problems which is just a mess.

ssharish

>Dont define the function within the function.
>Its not a good programming practice
Yep, it's not a good programming practice because nested functions are illegal in C. I wouldn't call a practice that completely fails to compile anything but bad. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.