//静态数组 int arr[5]; //动态数组 int *arr = (int *)malloc(5 * sizeof(int)); //访问数组 int arr[] = {1, 2, 3, 4, 5}; printf("size=%ld\n", sizeof(arr));//??? for (int i = 0; i < 5; i++)//??? { printf("Item=%d\tItem=%d\n", arr[i], *(arr + i)); } //实际串长和实际内存空间 char str[N]="abc\0abc"; printf("%d\n",strlen(str)); printf("%d\n",sizeof(str)); //3 //10
char chs[12] = "hi,there"; printf("%d\n", sizeof(chs) / sizeof(chs[0])); printf("%d\n", sizeof(chs) / sizeof(*chs)); printf("%d\n", sizeof(chs) / sizeof(*(chs + 0)));
//方案1 int a[M][N] = {1, 2, 3, 7, 8, 9};OK //方案2 int a[M][N] = {{1, 2, 3}, {7, 8, 9}};
#include <stdio.h> #define M 2 #define N 3 int main(void) { //int a[M][N] = {1, 2, 3, 7, 8, 9};OK int a[M][N] = {{1, 2, 3}, {7, 8, 9}}; int i, j; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { printf("%d-%p\t", a[i][j],&(a[i][j])); } printf("\n"); } printf("\n"); }
char str[N] = {'a', 'b', 'c'}; char chs[N] = "abc"; gets(str); puts(str); scanf("%s", chs); printf("%s", chs); str="helloworld"//告警
char chs[] = "hi,there"; puts(chs); //告警:数组名chs是常量 //chs="hello" strcpy(chs, "hello"); puts(chs);
char *ch = "hi,there"; puts(ch); //字符指针指向一个串常量,不能被修改,只能重新指向别处 // strcpy(ch, "hello"); ch="hello"; puts(ch);