forked from getiot/linux-c
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define ROWS 3 // 二维数组的行数 | ||
#define COLS 4 // 二维数组的列数 | ||
|
||
int main() | ||
{ | ||
int arr[ROWS * COLS]; // 一维数组 | ||
int (*arr2d)[COLS]; // 二维数组指针 | ||
|
||
arr2d = (int (*)[COLS]) arr; // 将一维数组的地址赋给二维数组指针 | ||
|
||
// 通过指针方式访问二维数组的元素,并进行赋值 | ||
for (int i = 0; i < ROWS; i++) { | ||
for (int j = 0; j < COLS; j++) { | ||
arr2d[i][j] = i * COLS + j; | ||
} | ||
} | ||
|
||
// 打印二维数组的元素 | ||
for (int i = 0; i < ROWS; i++) { | ||
for (int j = 0; j < COLS; j++) { | ||
printf("%d\t", arr2d[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
all: | ||
gcc access_array.c -o access_array | ||
gcc dynamic_array.c -o dynamic_array | ||
CC := gcc | ||
CFLAGS := -std=gnu17 -Wall -Wextra | ||
SRCS := $(wildcard *.c) | ||
EXES := $(SRCS:.c=) | ||
|
||
all: $(EXES) | ||
|
||
%: %.c | ||
$(CC) $(CFLAGS) -o $@ $^ | ||
|
||
clean: | ||
rm access_array dynamic_array | ||
rm -f $(EXES) | ||
|
||
.PHONY: all clean |