Hi all,
its me again.can i have a function that return a double array?

Take a look at http://www.daniweb.com/forums/thread5939.html

SUMMARY: Pass a pointer to the start as a parameter to the function, and then return this pointer when you're done.

EDIT: Mentioned link is belongs to C++ example but it is an answer of your question.

For example,

double *getValues();

Take a look at http://www.daniweb.com/forums/thread5939.html

EDIT: Mentioned link is belongs to C++ example but it is an answer of your question.

For example,

double *getValues();

ive tried that.but seems not work for double array which is only the first row is returned

I tried that and worked. Why don't you give it a try:

#include <stdio.h>
#define M 5

float* return_array(int size);

main()
{   int i;
     float *t;
     t=return_array(M);
     printf("the elements of t array are:\n");
     for (i=0; i<M; i++)
       printf("%f ",t[i]);
     printf("\n");
     system("pause");
      }
      
float* return_array(int size)
{
   int i;
    float *temp;
    temp=(float*)malloc(size);
     printf("give me 5 double numbers\n");
      for (i=0; i<size; i++)
       scanf("%f",&temp[i]); 
       return temp;
        }

This piece of code returns an array of 5 float numbers in main.

I tried that and worked. Why don't you give it a try:

#include <stdio.h>
#define M 5

float* return_array(int size);

main()
{   int i;
     float *t;
     t=return_array(M);
     printf("the elements of t array are:\n");
     for (i=0; i<M; i++)
       printf("%f ",t[i]);
     printf("\n");
     system("pause");
      }
      
float* return_array(int size)
{
   int i;
    float *temp;
    temp=(float*)malloc(size);
     printf("give me 5 double numbers\n");
      for (i=0; i<size; i++)
       scanf("%f",&temp[i]); 
       return temp;
        }

This piece of code returns an array of 5 float numbers in main.

thank,
but is this suitable for 2d array.can i return row number and item in array?seem like 2d array is=array[row][column]

Try this..Does it do your job:

#include <stdio.h>
#define M 3
#define N 3

float** return_array(int rows,int columns);

main()
{   int i,j;
     float **t;
     t=return_array(M,N);
     printf("the elements of t array are:\n");
     for (i=0; i<M; i++) {
      for (j=0; j<N; j++)
       printf("%f ",t[i][j]);
     printf("\n");
     }
     system("pause");
      }
      
float** return_array(int rows,int columns)
{
   int i,j;
    float** temp;
    temp=(float**)malloc(rows);
      
      for (i=0; i<rows;i++)
       temp[i]=(float*)malloc(columns); 
       
      printf("give me 9 float numbers to fill the 2d array\n");
      for (i=0; i<rows; i++)
       for(j=0; j<columns; j++)
        scanf("%f",&temp[i][j]);
       return temp;
        }

Now function return_array return a 3x3 array in main.

> Try this..Does it do your job:
Well it might, if you included stdlib.h as well.

Then you might be able to call malloc without having to resort to dangerous casting (it's REALLY dangerous without a prototype, and essentially useless with a prototype).

Why is it dangerous?
Well Dorothy, without a prototype, malloc would be implicitly declared as returning int. On machines where pointers and integers have different sizes, that's one nasty bug waiting to bite. Not that you would see that at compile time, the cast ensures a dose of "STFU" to the compiler.

Omitting all the optional braces might be OK, so long as your indentation is top-notch. Yours isn't, so it's more bugs in hiding.

Thank you Salem. You explained the issue quite thoroughly. So how could we solve this problem? Return a 2d double array in main?

Apparently not well enough....

Briefly
- remove the casts on your malloc calls
- include stdlib.h

my problem is,i have a string array.in example:
array[100[100]
array[0][]=No.3,Petaling street
array[1][]=No4,Petaling Street

i cant find what is wrong.this code juz give me the last data in address file.
example of address file:
1-No3,Petaling Street
2-No4,Petaling Street
3-No1,Wallstreet

#define M 100
#define N 100

char** address_array(int rows,int columns)
main (void)
{
char t=address_array(M,N);
printf ("%s",t);
return 0;
}
char** address_array(int rows,int columns)
{
   FILE *in_address;
 // open constraint file and read data
   if ((in_address = fopen(address_file, "r+"))
       == NULL)
    {
      fprintf(stderr, "Cannot open address file.\n");
      exit(0);
    }

      int i,j;
      char** temp;
      char *p=NULL;
      temp=(char**)malloc(rows);

      for (i=0; i<rows;i++)
      temp[i]=(char*)malloc(columns);

      for (i=0; i<rows; i++)
       {
         for(j=0; j<columns; j++)
            {
                 fgets(temp[i], 100, in_address);
          //    if ((p = strchr(temp[i], '\n'))) //check endline character
           //    {
             //      *p='\0';    //assign 0 to endline character
             //   }
            }
       }
  fclose (in_address);
  return temp;

}

> char t=address_array(M,N);
You're returning a char**, so try
char **t=address_array(M,N);

> printf ("%s",t);
Try (with the first fix)
printf ("%s",t[0]);

> for(j=0; j<columns; j++)
OK, if you're reading a char at a time. But since you're reading whole LINES, then the outer loop will suffice.

REMOVE THE MALLOC CASTS!

> char t=address_array(M,N);
You're returning a char**, so try
char **t=address_array(M,N);

> printf ("%s",t);
Try (with the first fix)
printf ("%s",t[0]);

> for(j=0; j<columns; j++)
OK, if you're reading a char at a time. But since you're reading whole LINES, then the outer loop will suffice.

REMOVE THE MALLOC CASTS!

im sorry,what is malloc casts?

> temp=(char**)malloc(rows);
The red is a cast expression, and is unnecessary.

And it is also HORRIFICALLY wrong on the size as well - damn, didn't spot that one earlier.

It should be temp= malloc ( rows * sizeof *rows ); you always have to scale the number of elements you want with the size of each of the elements.

commented: Good point. Excellent advice. +15

im sorry,what is malloc casts?

this is the example of malloc cast isnt it?

double *p;
p = (double *)malloc ( n * sizeof ( double ) );

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.