-2

I am new to c and programming and not comfortable with dynamic memory allocation. However, it seems like for my purposes this is the only way:

I need to return a 2D array of the size, defined in runtime from an external function into main-function:

void read(double (*data)[6], int size)
{
//do something and generate size and array[size*6]
for(i=0;i<size;i++){
    for(j=0;j<6;j++) 
        data[i][j]=array[i*6+j];}

}

I call this function in main:

int main()
{
    int size;
    double *data= (double*) malloc(10000 *sizeof(double));
    read(fname, data, size);//should supply me with a pointer to memory where data was stored

    //I should be able to access the stored data somehow:
    for (i = 0; i < size; ++i){
        for(j=0;j<6;j++) 
            printf("%lf ", data[i][j]);printf("\n");
    }
}

There is probably a whole bunch of mistakes in this code. I was trying to deliver the idea of what I want to do. Is there an implementation, or this is just nonsense? Thanks in advance!

Additional information: function read reads data from file and formats it into an array of double. I really don't want to put it into main, I'd rather keep it as a library.

MsTais
  • 189
  • 2
  • 9
  • Please fix the formatting of your code. Multiple for-loops on the same line is really ugly and hard to follow – Christian Gibbons Apr 02 '18 at 16:57
  • `read` takes a `double *[6]`, you pass in a `double *`. What is `array`? – Christian Gibbons Apr 02 '18 at 17:11
  • @Christian Gibbons Array is 1D double array of numbers I read off of the .dat file. I rearrange them into 2D array of the form data[size][6], where size depends on how many lines of data are stored in .dat – MsTais Apr 02 '18 at 17:31
  • 1
    Your function doesn't return anything and there is no 2D array anywhere either. Please clarify your question and show appropriate code. – too honest for this site Apr 02 '18 at 17:54
  • 1
    @Olaf "Your function doesn't return anything." Good question! From the presented code, my understanding is that `returning` does not mean returning a *new* array. I means updating array `data` (passed to the function) with the information obtained inside function `read`. The array `data` is created in the `main`. – sg7 Apr 02 '18 at 18:37
  • See [this answer](https://stackoverflow.com/a/47235897/841108) to a very similar question – Basile Starynkevitch Apr 02 '18 at 18:45
  • @sg7: That's the point of the "unclear" close reason. OP should at least use _basic terminology to make clear what he wants. As it looks, that's another one in urgent need of reading a good textbook. However I read the question, it should cover all he needs. – too honest for this site Apr 02 '18 at 18:54
  • 2
    Standard sidenote (the 124316165740321th): don't cast the result of `malloc` & friends or `void *` in general. – too honest for this site Apr 02 '18 at 18:58

1 Answers1

1

1) If you know the size of of your array you may want to be more precise with the allocation.

2) Remember to check if your allocation was successful.

3) When you do not needs your array free the allocated memory

The program may look like:

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

void read(double (*data)[6], int size, double *array)
{
 //do something and generate size and array[size*6]
 int i,j;
  for(i=0;i<size;i++){
    for(j=0;j<6;j++) 
        data[i][j]=array[i*6+j];
    }
}

int main(void) {

    int size = 6;
    int i,j;

    double arr[] = {
        1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 0.1, 0.1, 0.2,
        1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 0.1, 0.1, 0.2,
        1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 0.1, 0.1, 0.2,
        1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 0.1, 0.1, 0.2,
    };

    double (*data)[size] = malloc(sizeof(double[size][size]));
    if (data==NULL)
        return -1;

    read(data, size, arr);

    for (i = 0; i < size; ++i){
        for(j=0;j<6;j++) 
            printf("%lf ", data[i][j]);
        printf("\n");
    }

    free(data);
    return 0;
}

Test:

1.000000 2.000000 3.000000 4.000000 5.000000 6.000000                                                                                     
7.000000 8.000000 9.000000 0.100000 0.100000 0.200000                                                                                     
1.000000 2.000000 3.000000 4.000000 5.000000 6.000000                                                                                     
7.000000 8.000000 9.000000 0.100000 0.100000 0.200000                                                                                     
1.000000 2.000000 3.000000 4.000000 5.000000 6.000000                                                                                     
7.000000 8.000000 9.000000 0.100000 0.100000 0.200000 
sg7
  • 6,108
  • 2
  • 32
  • 40
  • Thanks! I do not know the size of my array though. It is defined in runtime inside of the function "read", and depends on the .dat I am trying to read. – MsTais Apr 02 '18 at 17:49
  • @MsTais Ok, then you have two choices: a) allocate the maximum possible size which you expect (as you do now) or b) read data to the dynamically allocated `arr` first (if needed resize the array as you go) and then create array `data`. – sg7 Apr 02 '18 at 17:55
  • I have just tried your way and just defined a big array which won't be overflown. It produces segmentations fault. I am looking into that. Nothing is wrong, except I only get access to the data when I call read(). Till that time I don't know what my data is like. – MsTais Apr 02 '18 at 18:01
  • 2
    I do not mind to be DV if I make a mistake helping others. But, it would be nice to know what I did wrong for the future. Also, for the sake of MsTais it would be helpful for her to know what is a potential problem in this answer. – sg7 Apr 02 '18 at 18:25
  • 1
    It was my bug, not related to your approach. It worked, thanks! – MsTais Apr 02 '18 at 20:57
  • At least somebody is not being a snob and trying to help... Thanks a lot! – MsTais Apr 02 '18 at 20:58