Search
 
SCRIPT & CODE EXAMPLE
 

C

passing two dimensional array to function in c

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

#define ROWS 3
#define COLS 2

void fun1(int **, int, int);

int main()
{
  int array_2D[ROWS][COLS] = { {1, 2}, {3, 4}, {5, 6} };
  int n = ROWS;
  int m = COLS;

  fun1(array_2D, n, m);

  return EXIT_SUCCESS;
}

void fun1(int **a, int n, int m)
{
  int i, j;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      printf("array[%d][%d]=%d
", i, j, a[i][j]);
    }
  }
}
Comment

passing two dimensional array to function in c

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

#define ROWS 3
#define COLS 2

void fun1(int (*)[COLS], int);

int main()
{
  int array_2D[ROWS][COLS] = { {1, 2}, {3, 4}, {5, 6} };
  int rows = ROWS;

  /* works here because array_2d is still in scope and still an array */
  printf("MAIN: %zu
",sizeof(array_2D)/sizeof(array_2D[0]));

  fun1(array_2D, rows);

  return EXIT_SUCCESS;
}

void fun1(int (*a)[COLS], int rows)
{
  int i, j;
  int n, m;

  n = rows;
  /* Works, because that information is passed (as "COLS").
     It is also redundant because that value is known at compile time (in "COLS"). */
  m = (int) (sizeof(a[0])/sizeof(a[0][0]));
 
  /* Does not work here because the "decay" in "pointer decay" is meant
     literally--information is lost. */
  printf("FUN1: %zu
",sizeof(a)/sizeof(a[0]));

  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      printf("array[%d][%d]=%d
", i, j, a[i][j]);
    }
  }
}
Comment

c pass two dimensional array to function

#include <stdio.h>
const int M = 3;
const int N = 3;
 
void print(int arr[M][N])
{
    int i, j;
    for (i = 0; i < M; i++)
      for (j = 0; j < N; j++)
        printf("%d ", arr[i][j]);
}
 
int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    print(arr);
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: c check if file was created 
C :: C do...while loop 
C :: calculate max of three numbers using ternary operator in c 
C :: helloworld c 
C :: difference between int main() and int main(void) 
C :: compile opencv program 
C :: c get string 
C :: filing in c 
C :: default password raspberry pi 
Dart :: dart remove last character from string 
Dart :: flutter transparent appbar 
Dart :: flutter keyboard overflow when opens 
Dart :: flutter width infinity 
Dart :: how to get the name of the day in flutter 
Dart :: flutter snackbar circular shape rounded circle 
Dart :: flutter return empty widget 
Dart :: dart absolute value 
Dart :: get random color in flutter 
Dart :: not empty string check dart 
Dart :: flutter snackbar width 
Dart :: leading image flutter 
Dart :: sort list descending from spesific maps key in dart 
Dart :: flutter tooltip height 
Dart :: dart convert int to string 
Dart :: switch case dart 
Dart :: how to launch url in flutter web 
Dart :: Flutter Popup Menu Button Example Tutorial 
Dart :: dart custom exception 
Dart :: Flutter Dart - Difference Two DateTime 
Dart :: dart while break 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =