Allocating a 2d Array

Joshua

My aim is to allocate a 2d array with only using 1 line for efficiency. Since my prof is expecting it to be efficient. the code gives me an error saying that it can't convert from void* to int.

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

#define NUMOFCOL 4

int **addtwoarr(int (*A)[NUMOFCOL], int (*B)[NUMOFCOL]);

int main(void){
    int firstarr[4][4]={{1,1,1,1},
                        {1,1,1,1},
                        {1,1,1,1},
                        {1,1,1,1}},
        secondarr[4][4]={{1,1,1,1},
                        {1,1,1,1},
                        {1,1,1,1},
                        {1,1,1,1}}, **receiver;
    receiver = addtwoarr(firstarr, secondarr);
    printf("%d", receiver[3][3]);
}

int **addtwoarr(int (*A)[NUMOFCOL], int (*B)[NUMOFCOL]){
    int col, row, **arr;
    (*arr)[NUMOFCOL] = malloc(NUMOFCOL * sizeof(*arr)); /*this line in particular gives the error */
    for(row=0; row<NUMOFCOL; row++){
        for(col=0;col<NUMOFCOL; arr[row][col]=A[row][col]+B[row][col], col++){}
    }
    return arr;
}

The allocation happens in the addtwoarr function which is where the error occurs.

WhozCraig

I seriously don't recommend this, as there are a ton of assumptions in your code about top-end sizing. But if you really want to do it, the rather cryptic syntax for returning pointers to fixed length arrays in C looks something like this:

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

#define NUMOFCOL 4

int (*addtwoarr(int A[][NUMOFCOL], int B[][NUMOFCOL]))[NUMOFCOL];

int main(void)
{
    int firstarr[][NUMOFCOL] = {
        {1,1,1,1},
        {1,1,1,1},
        {1,1,1,1},
        {1,1,1,1}},

    secondarr[][NUMOFCOL] = {
        {1,1,1,1},
        {1,1,1,1},
        {1,1,1,1},
        {1,1,1,1}};

    int (*receiver)[NUMOFCOL] =  addtwoarr(firstarr, secondarr);
    printf("%d\n", receiver[3][3]);
    free(receiver);
}

int (*addtwoarr(int A[][NUMOFCOL], int B[][NUMOFCOL]))[NUMOFCOL]
{
    int col, row;
    int (*arr)[NUMOFCOL] = malloc(NUMOFCOL * sizeof(*arr));
    for(row=0; row<NUMOFCOL; row++){
        for(col=0;col<NUMOFCOL; arr[row][col]=A[row][col]+B[row][col], col++);
    }
    return arr;
}

Output

2

Best of luck.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically allocating a 2D array in C

From Dev

Dynamically allocating a 2D string array

From Dev

Allocating 2D array dynamically

From Dev

Allocating 2D array with pointer to fixed-size array

From Dev

c: issues when allocating 2d char array dynamically?

From Dev

C segmentation fault when dynamically allocating 2d array

From Dev

Allocating a 2D contiguous array within a function

From Dev

Allocating an 2D array in C in heap instead of stack

From Dev

dynamically allocating my 2d array in c

From Dev

Memory management in allocating 2-D array

From Dev

Memory management in allocating 2-D array

From Dev

Overwriting nodeid when allocating nodeid++ in 2D array while moving

From Dev

Allocating space for a 2D string array before the text file is read

From Dev

Allocating memory for a 2-dimensional array that is in a structure

From Dev

Allocating a 2-dimensional structure array with malloc

From Dev

Allocating 2 dimension array of a class Java

From Java

Why does allocating a single 2D array take longer than a loop allocating multiple 1D arrays of the same total size and shape?

From Dev

Why does allocating a single 2D array take longer than a loop allocating multiple 1D arrays of the same total size and shape?

From Dev

Dynamically Allocating 2D Arrays in C

From Dev

Declaring and allocating to a 2D char pointer

From Dev

Dynamically allocating array explain

From Dev

Allocating array of structures with malloc

From Dev

allocating memory to an array of string

From Dev

Allocating a char pointer array

From Dev

Dynamically Allocating Array With Datafile

From Dev

Allocating space for an array

From Dev

Allocating array of structures in function

From Dev

Dynamically allocating an array of a struct

From Dev

Dynamically allocating 1D array of structs : Two Methods

Related Related

  1. 1

    Dynamically allocating a 2D array in C

  2. 2

    Dynamically allocating a 2D string array

  3. 3

    Allocating 2D array dynamically

  4. 4

    Allocating 2D array with pointer to fixed-size array

  5. 5

    c: issues when allocating 2d char array dynamically?

  6. 6

    C segmentation fault when dynamically allocating 2d array

  7. 7

    Allocating a 2D contiguous array within a function

  8. 8

    Allocating an 2D array in C in heap instead of stack

  9. 9

    dynamically allocating my 2d array in c

  10. 10

    Memory management in allocating 2-D array

  11. 11

    Memory management in allocating 2-D array

  12. 12

    Overwriting nodeid when allocating nodeid++ in 2D array while moving

  13. 13

    Allocating space for a 2D string array before the text file is read

  14. 14

    Allocating memory for a 2-dimensional array that is in a structure

  15. 15

    Allocating a 2-dimensional structure array with malloc

  16. 16

    Allocating 2 dimension array of a class Java

  17. 17

    Why does allocating a single 2D array take longer than a loop allocating multiple 1D arrays of the same total size and shape?

  18. 18

    Why does allocating a single 2D array take longer than a loop allocating multiple 1D arrays of the same total size and shape?

  19. 19

    Dynamically Allocating 2D Arrays in C

  20. 20

    Declaring and allocating to a 2D char pointer

  21. 21

    Dynamically allocating array explain

  22. 22

    Allocating array of structures with malloc

  23. 23

    allocating memory to an array of string

  24. 24

    Allocating a char pointer array

  25. 25

    Dynamically Allocating Array With Datafile

  26. 26

    Allocating space for an array

  27. 27

    Allocating array of structures in function

  28. 28

    Dynamically allocating an array of a struct

  29. 29

    Dynamically allocating 1D array of structs : Two Methods

HotTag

Archive