As for the presentation code, I want to pass a two-dimensional array x as an argument to the print_t ();function. What should I do? I could do it in one dimension, but I don't know what to do when it comes to a two-dimensional array.
#include "stdio.h"
void print_ (int ** t)
{
printf ("% d \ n", t [0]);
}
int main ()
{
int x1 [3] = {1,2,3};
int x [3] [3] {
{1,2,3},
{4,5,6},
{7,8,9},
};
print_ (x);
return 0;
}
-
Answer # 1
-
Answer # 2
* The tag is C ++, but since the code was in C language, the answer is based on C language.
The print_ function can only receive the start address of the x array.
From the print_ function, the number of elements in the array is unknown.
Therefore, it is necessary to explicitly specify the number of elements on the function side.When specifying with an argument ①
void print_ (int t [3] [3]) { // x [2] [2] printf ("% d \ n", t [2] [2]); }
When specifying with an argument ②
The number of lines can be omitted.void print_ (int t [] [3]) { // x [2] [2] printf ("% d \ n", t [2] [2]); }
When finding the element position by calculation.
void print_ (int * t) { // x [2] [2] printf ("% d \ n", t [2 * 3 + 2]); }
In either case, it is a good idea to define the number of elements with a macro.
#define ROWS 3 #define COLS 3 void print_ (int t [ROWS] [COLS]) { // x [2] [2] printf ("% d \ n", t [2] [2]); } void print_ (int * t) { // x [2] [2] printf ("% d \ n", t [2 * COLS + 2]); } int main () { int x [ROWS] [COLS] { {1,2,3}, {4,5,6}, {7,8,9}, }; print_ (x); return 0; }
Related articles
- how to pass a string as a function argument in c ++
- c ++ - please tell me how to pass the function argument to another file
- when i try to use a c ++ template type argument in a function, i get an error if the type is different
- i don't know how to pass an array of c ++ objects to a function
- c ++ - about argument of member function
- i want to create a 3d array with c ++ and convert it to an image
- about deleting c ++ structure array
- c ++ - behavior of fork () function c language
- python - when none is assigned to the formal argument of the function definition
- laravel - if you put an argument in withoutglobalscopes (), "withoutglobalscopes () must be of the type array or null, stri
- php - create a function that has a dummy argument of array $arr and passes an array array(1, 3, 5, 7,7, 9) containing numerical
- c ++ - how to resolve variadic template function overloads with no arguments
- array - -1 is output by the ubound function
- xcode - mutable and immutable when an array is used as an argument of a function with swift
- c ++ - i want to get the number of elements of the array taken as the argument of the function
- python 3x - i want to create a function that returns a list with the same name as the argument
- c ++ - i want to create class b in class a and create an argument list in class b that takes a pointer of class a as an argument
- javascript - argument type of callback function passed to request event of httpserver
- array error in fortran function
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
When passing a two-dimensional array, the number of outer elements cannot be omitted.
Example rewritten with vectorYou need to do the following:
For C ++, it's better to use std :: vector instead of an array in C.