function with array input/output 
Author Message
 function with array input/output

float *GetState(float smatrix[2][2])
{
     float newstate[2] = {0.0,0.0};
...
     return newstate;

Quote:
}

void main()
{
  float m[2][2] = {0.0,0.0,0.0,0.0};
  float out[2] = {0.0,0.0};
...
  out = GetState(m);

Quote:
}

The purpose of the upper code is to define a function which takes in a
two
dimentional array, and returns an array. I know that in C a function
cannot return an array, but only a pointer to an array. What I can do
in the function
definition of GetState(), so that it can return the pointer of the
array of newstate? Can I receive the pointer returned by the function
GetrState() with an array, or what shall I do in a correct way?

Thanks for your attention.
Jian
--



Fri, 28 May 2004 12:26:29 GMT  
 function with array input/output

Quote:
> float *GetState(float smatrix[2][2])
> {
>      float newstate[2] = {0.0,0.0};
> ...
>      return newstate;
> }

newstate is a local array and returning its address is a recipe for
disaster.

depending on what you need, you could declare newstate to be a static
array, i.e.

float *GetState(float smatrix[2][2])
{
   static float newstate[2] = {0.0,0.0};
   return newstate;

Quote:
}

which would make returning the address OK. be aware, though, newstate would
only be initialized once, so if the GetState function modifies newstate in
subsequent calls, the callers would get the new values in newstate.

Quote:
> void main()

void is not a valid return type for main.

Quote:
> {
>   float m[2][2] = {0.0,0.0,0.0,0.0};
>   float out[2] = {0.0,0.0};
> ...
>   out = GetState(m);
> }

you could also try the following:

#include <stdio.h>
#include <stdlib.h>

float *GetState(float smatrix[2][2])
{
        float *newstate = malloc(2*sizeof(float));
        if( newstate != NULL )
        {
                newstate[0] = 0.0;
                newstate[1] = 0.0;
        }

        return newstate;

Quote:
}

int main(void)
{
  float m[2][2] = {0.0,0.0,0.0,0.0};
  float *out;

  out = GetState(m);
  if( out != NULL )
  {
          /* do something with out */
  }
  else
  {
          /* handle error */
  }

  free(out);

  return 0;

Quote:
}

you might want to consult the faq available at

http://www.faqs.org/faqs/C-faq/faq/

hth.

Sinan.
--
--------------------------------
A. Sinan Unur
http://www.unur.com/
--



Fri, 28 May 2004 15:24:11 GMT  
 function with array input/output

writes

Quote:
>float *GetState(float smatrix[2][2])
>{
>     float newstate[2] = {0.0,0.0};
>...
>     return newstate;
>}

>void main()
>{
>  float m[2][2] = {0.0,0.0,0.0,0.0};
>  float out[2] = {0.0,0.0};
>...
>  out = GetState(m);
>}

There are various ways of dealing with your problem. One is to wrap all
your arrays into structs because those are capable of being assigned.
another is something like:
float* GetState(float dest[], float source [][2],  int row,)
// Note that this only works for arrays in which all elements
// are contiguous
   int index;
  for(index=0; index<2; ++index){
      dest[index] = source[row][index];
  }
  return source;

Quote:
}

Note that you can only use the return if your array is being handled by
a pointer (which is not the case in your example.

Now you can write:

int main() {
  float m[2][2] = {0.0,0.0,0.0,0.0};
  float out[2] = {0.0,0.0};
...
   GetState(out, m, 0));

Quote:
}

--
Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.
--



Sun, 30 May 2004 10:13:49 GMT  
 function with array input/output

Quote:
> float *GetState(float smatrix[2][2])
> {
>      float newstate[2] = {0.0,0.0};
> ...
>      return newstate;
> }

> void main()
> {
>   float m[2][2] = {0.0,0.0,0.0,0.0};
>   float out[2] = {0.0,0.0};
> ...
>   out = GetState(m);
> }

> The purpose of the upper code is to define a function which takes in a
> two
> dimentional array, and returns an array. I know that in C a function
> cannot return an array, but only a pointer to an array. What I can do
> in the function
> definition of GetState(), so that it can return the pointer of the
> array of newstate? Can I receive the pointer returned by the function
> GetrState() with an array, or what shall I do in a correct way?

It's much better to define the argument and return types of GetState as
structs.  This allows the compiler to check the array sizes and allows you
to return an array type:

struct result
  {
  float v[2];
  };

struct arg
  {
  float v[2][2];
  };

struct result GetState( const struct arg* smatrix)
  {
  struct result newstate = { 1.0F, 0.0F};
  ...
  return newstate;
  }

int main( void)
  {
  struct arg m = {0.0F,0.0F,0.0F,0.0F};
  struct result out;
  ...
  out = GetState( &m);

  return 0;
  }

C permits a function to return a struct which is typically allocated by the
compiler on the caller's stack.  This approach is not efficient if the
arrays are large as the structs will be copied.  With large arrays it would
be better to change the definition of GetState and pass it pointers to both
the input and output arrays.

NB unless space is at a premium floating point variables are best declared
as double since C requires the conversion of floats to doubles during
arithmetic operations.

- Lawrence Rust
--



Sun, 30 May 2004 10:13:53 GMT  
 function with array input/output

Quote:

> float *GetState(float smatrix[2][2])
> {
>      float newstate[2] = {0.0,0.0};
> ...
>      return newstate;

Very bad idea.  The "newstate" ceases to exist as soon as this routine
exits.  I.e. as soon as the function calling this one tries to do
anything useful with the result of tha GetState() call (besides
comparing it to NULL), it causes undefined behaviour.

  > }

Quote:
> void main()

Another very bad idea. main() returns int. Enough said.

Quote:
> The purpose of the upper code is to define a function which takes in
> a two dimentional array, and returns an array. I know that in C a
> function cannot return an array, but only a pointer to an
> array. What I can do in the function definition of GetState(), so
> that it can return the pointer of the array of newstate?

As long as newstate is a local automatic variable of GetState():
nothing.  That has to change.

Either allocate newstate on the heap (with malloc()), or make it
static, or pass 'newstate' as a pointer into GetState.  No need to
return it then, either.  Which of these you choose is largely a matter
of taste.
--

Even if all the snow were burnt, ashes would remain.
--



Sun, 30 May 2004 10:13:54 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. input, output, input/output parameters?????

2. how to input/output selfdefined struct type array?

3. Input/output in C function called by Fortran Program

4. How to input an array into a function

5. Unable to sync input and output streams

6. Hashing long input strings into short output strings

7. 2 questions on data input and output

8. File input output

9. Redirecting input and output of a program

10. How to Directly Read Piped-Output as Input in C

11. Data Input/Output - New Code- disregard last message

12. Data Input/Output

 

 
Powered by phpBB® Forum Software