0

I need to make a function which can accept one-dimensional arrays and two-dimensional arrays. I also need to do that with single pointer as argument of this function.

If someone will ask, I solved my problem with double pointer as argument on function matrix, but now I have to do this in other way. I was thinking about passing array of pointers and in my function assign every pointer (of row) to temporary one-dimensional array. I'm not familiar with C language, so maybe I did some stupid mistake, but I believe tmpTab = &tab[i]; this line of code should work, but it doesn't, because I get junk on output.

void matrix(double* tab, int n, int l) {
    int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
    int i, j, isCol = 1, isRow = 1;
    double *tmpTab;

    if (l >= n || l < 0) { isRow = 0; }
    if (l >= n || l < 0) { isCol = 0; }
    for (i = 0; i < n; i++) {
        tmpTab = &tab[i];
        for (j = 0; j < n; j++) {
            printf("\n%i: %lf", j, tab[j]);
            if (tab[j] < S || tab[j] > E) count1++;
            if (isRow && i == l && tab[j] >= S && tab[j] <= E) count2++;
            if (isCol && j == l && tab[j] >= S && tab[j] <= E) count3++;
            if (i == j && tab[j] >= S && tab[j] <= E) count4++;
            if (i > j && tab[j] >= S && tab[j] <= E) count5++;
            if (i + j == n - 1 && tab[j] >= S && tab[j] <= E) count6++;
        }
    }
}

Function to allocate my array:

double **allocate_array(int n) {
    double **tab;
    tab = (double **)malloc(sizeof(double) * n);
    for (int i = 0; i < n; i++) {
        tab[i] = (double *)malloc(sizeof(double) * n);
    }
    return tab;
}

Main:

int main()
{
    double **tab;
    tab = read_array("dane.txt", &N);
    print_array(tab, N);
    matrix((double *)tab, N, 2);
}

Read from file works properly, print_array also. This is output of my program for research.

3.30 2.30 2.10 3.30 3.40                                                                                                3.30 2.30 2.10 3.30 3.40                                                                                                3.30 2.30 2.10 3.30 3.40                                                                                                3.30 2.30 2.10 3.30 3.40                                                                                                3.60 2.60 2.60 3.60 3.50                                                                                                                                                                                                                        0: 0.000000                                                                                                             1: 0.000000                                                                                                             2: -6277435991103237715486889255272272650483169294881725067238215516160.000000                                          3: -6277438562204192487878988888393020692503707483087375482269988814848.000000                                          4: -6277438562204192487878988888393020692503707483087375482269988814848.000000                                          0: 0.000000                                                                                                             1: 0.000000                                                                                                             2: -6277435991103237715486889255272272650483169294881725067238215516160.000000                                          3: -6277438562204192487878988888393020692503707483087375482269988814848.000000                                          4: -6277438562204192487878988888393020692503707483087375482269988814848.000000                                          0: 0.000000                                                                                                             1: 0.000000                                                                                                             2: -6277435991103237715486889255272272650483169294881725067238215516160.000000                                          3: -6277438562204192487878988888393020692503707483087375482269988814848.000000                                          4: -6277438562204192487878988888393020692503707483087375482269988814848.000000                                          0: 0.000000                                                                                                             1: 0.000000                                                                                                             2: -6277435991103237715486889255272272650483169294881725067238215516160.000000                                          3: -6277438562204192487878988888393020692503707483087375482269988814848.000000                                          4: -6277438562204192487878988888393020692503707483087375482269988814848.000000                                          0: 0.000000                                                                                                             1: 0.000000                                                                                                             2: -6277435991103237715486889255272272650483169294881725067238215516160.000000                                          3: -6277438562204192487878988888393020692503707483087375482269988814848.000000                                          4: -6277438562204192487878988888393020692503707483087375482269988814848.000000 
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Eddy
  • 593
  • 8
  • 22
  • 1
    A pointer to a pointer is ***not*** the same as an array of arrays (a "multi-dimensional" array). See e.g. [this old answer of mine](https://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456) for an explanation about why. – Some programmer dude May 27 '19 at 06:41
  • 1
    By the way, `tab = (double **)malloc(sizeof(double) * n)` is wrong for two reasons: First of all you don't allocate the size of pointers; Secondly in C you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude May 27 '19 at 06:42
  • @Someprogrammerdude If I'm not wrong I pass this to function: ``` | matrix[0] | matrix[1] | matrix[2] | ... | ``` and I should access to their addresses by ```matrix[i]```, becouse it points me memory where my row is stored. – Eddy May 27 '19 at 06:45
  • 1
    The problem is this: `matrix((double *)tab, N, 2)`. `tab` is a pointer to a pointer, you have allocated it as an array of pointers not an array of values. It can't be used as an array of arrays (what e.g. `double tab[X]Y];` would be) or an array of values. – Some programmer dude May 27 '19 at 06:51
  • I'm also very unclear about what problem you're trying to solve? What is the `matrix` function supposed to do? Why can't it take a `double **tab` argument? – Some programmer dude May 27 '19 at 06:55
  • @Someprogrammerdude Then I will have another problem, becouse I need dynamic allocated "array" – Eddy May 27 '19 at 06:56
  • @Someprogrammerdude Matrix function count some things and print it to console, but for clear I cut most of the lines of ```printf``` functions. ```double **tab``` could be argument and I did it for the first time, but my professor told me that I should do this in another approach... – Eddy May 27 '19 at 07:00
  • @NorbertIncze I will try the 3rd option, becouse maybe it will solve my problem – Eddy May 27 '19 at 07:05
  • You have a `\n` in your only `printf`. Are you sure that the output is 1 very long line? – mch May 27 '19 at 07:17

0 Answers0