简体   繁体   中英

2D array pointer in C

I have the function and main

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>

setArray(double *thearray){
    *thearray[0][0] = 2.0;
    *thearray[0][1] = 2.0;
    *thearray[1][0] = 2.0;
    *thearray[1][1] = 2.0;
}

void main(){
    double myarray[2][2];
    setArray(&myarray);
}

I cannot specify the size of array on setArray function because I don't know what will it be. I need to fill up the array in this specific fucction but I can't. get errors:

test.c: In function ‘setArray’:
test.c:8:13: error: subscripted value is neither array nor pointer nor vector
test.c:9:13: error: subscripted value is neither array nor pointer nor vector
test.c:10:13: error: subscripted value is neither array nor pointer nor vector
test.c:11:13: error: subscripted value is neither array nor pointer nor vector
test.c: In function ‘main’:
test.c:16:1: warning: passing argument 1 of ‘setArray’ from incompatible pointer type [enabled by default]
test.c:7:1: note: expected ‘double *’ but argument is of type ‘double (*)[2][2]’

You can use a VLA:

void setArray(int m, int n, double arr[m][n])
{
    for (int r = 0; r < m; ++r)
        for (int c = 0; c < n; ++c)
             arr[r][c] = 2.0;
}

int main()
{
    double myarray[2][2];
    setArray(2, 2, myarray);
}

VLAs are supported in C99, and optional in C11. If your compiler does not support VLAs, then you cannot fulfil your requirements. However, you could pass the array as a 1-D array and find the right elements using arithmetic:

void setArray(int num_rows, int num_cols, double *arr)
{
#define ARR_ACCESS(arr, x, y) ((arr)[(x) * num_cols + (y)])
    for (int r = 0; r < num_rows; ++r)
        for (int c = 0; c < num_cols; ++c)
             ARR_ACCESS(arr, r, c) = 2.0;
#undef ARR_ACCESS
}

int main()
{
    double myarray[2][2];
    setArray(2, 2, (double *)&myarray);
}

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>

void setArray(double **thearray){
    thearray[0][0] = 2.0;
    thearray[0][1] = 2.0;
    thearray[1][0] = 2.0;
    thearray[1][1] = 2.0;
}

void main(){
    int i;
    double **myarray = (double**) malloc(2 * sizeof(double*));
    for(i = 0; i < 2; ++i)
        myarray[i] = (double*) malloc(2 * sizeof(double));
    setArray(myarray);
}

First, your setarray should accept a 2D array, not a poniter. If you know the width of the array, you can define it like this:

void setArray(double (*thearray)[2]) //2D array decays into a pointer to an array

then just call:

setArray(myarray)

An array decays to a pointer only once, so a 2D array wouldn't decay to a pointer to pointer. if the width isn't fixed, use a pointer of pointer instead:

void setArray(double **thearray)
{
    ...
}

setArray((double **)myarray) //explicitly convert.

A 2D array has a double pointer ( ** ). When you are sending the array as an argument, you do not need to add the ampersand as the array without its brackets is an address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM