Also wenn ich verstehe Sie Frage richtig sind, möchten Sie 1 zu jedem Element in Ihrem Array arr[5][5] = { {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5} }
hinzufügen?
So:
#include <stdio.h>
#include <stdlib.h>
#define ROWS 2
#define COLS 5
void print_array(int arr[][COLS], int nrows);
int
main(void) {
int row, col;
int arr[ROWS][COLS] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLS; col++) {
arr[row][col] += 1;
}
}
print_array(arr, ROWS);
return 0;
}
void
print_array(int arr[][COLS], int nrows) {
int row, col;
for (row = 0; row < nrows; row++) {
for (col = 0; col < COLS; col++) {
printf("%d ", arr[row][col]);
}
printf("\n");
}
}
Oder wenn Sie wirklich sizeof()
verwenden möchten, können Sie etwas tun:
#include <stdio.h>
#include <stdlib.h>
#define ROWS 2
#define COLS 5
int
main(void) {
int row, col;
int arr[ROWS][COLS] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
int row_size, col_size;
row_size = sizeof(arr)/sizeof(*arr);
col_size = sizeof(*arr)/sizeof(**arr);
for (row = 0; row < row_size; row++) {
for (col = 0; col < col_size; col++) {
arr[row][col] += 1;
}
}
for (row = 0; row < row_size; row++) {
for (col = 0; col < col_size; col++) {
printf("%d ", arr[row][col]);
}
printf("\n");
}
return 0;
}
Könnten Sie bitte ein Beispiel zu geben ist zu benutzen? – kkmonlee
@AndrewLi Das ist, was ich anfänglich versuchte, aber 'arr.length' existiert nicht in C. Ich versuchte' sizeof (arr) ', aber ich konnte kein richtiges Array bekommen. – kkmonlee