gpt4 book ai didi

c - 请举例帮助我更好地理解 C 中这些不同的类型声明?

转载 作者:行者123 更新时间:2023-11-30 21:02:29 24 4
gpt4 key购买 nike

我总结了一些常见的 C 类型声明,现在我想知道一些可靠的例子,例如如何初始化它们以及如何使用它们。

一些示例已经给出,但还有一些仍在等待好的示例。

int p[3]; int *p[3]; int p[][3]; int (*p)[3]; int *(p[3]); int(*p[8])[5];

char p[3];字符 *p[3];字符p[][3];字符 (*p)[3];字符 *(p[3]); char(*p[8])[5];

int **p; char **p;

int p[3]; 
//p is array of 3 int.

示例:

int contestants[] = {1, 2, 3};

-

char p[3];
//p is array of 3 characters.

示例:

char cards[] = "JQK";

-

 int *p[3];
//p is array of 3 pointers to int.

示例:

int x = 1;
int y = 2;
int z = 3;
int *a1[3] = {&x, &y, &z}; //array of pointer to int
printf (" &x = %p, a1[0] = %p\n" , &x, a1[0]);
printf ("%d %d %d\n" , *a1[0], *a1[1], *a1[2]); //1 2 3

-

int p[][3];
//p is array of array of 3 int.

示例:

int data1[2][3] = { {1,2,3}, {4,5,6}};

-

char *p[3];
//p is array of 3 pointers to char.

示例:

char *juices[] = { "dragonfruit", "waterberry", "sharonfruit", "uglifruit",
"rumberry", "kiwifruit", "mulberry", "strawberry", "blueberry",
"blackberry", "starfruit" };
char *names_for_dog[] = {"Bowser", "Bonza", "Snodgrass"};

-

char p[][3];
//p is array of array of 3 char.

示例:

char tracks[][80] = { "I left my heart in Harvard Med School",
"Newark, Newark - a wonderful town", "Dancing with a Dork",
"From here to maternity", "The girl from Iwo Jima", };

-

int (*p)[3];
//p is pointer to array of 3 ints.

示例:

int data1[2][3] = { {1,2,3}, {4,5,6}};
int (*a2)[3] = data1;//pointer to int[3]
printf("%d %d\n" , a2[0][1], a2[1][2]);//2 6

-

char (*p)[3]; 
//p is pointer to array of 3 char.

示例:**********我需要一个很好的例子********************

-

int *(p[3]); is equivalent to int *p[3]; 
//p is array of 3 pointers to int.

-

char *(p[3]); is equivalent to char *p[3];
//p is array of 3 pointers to char.

-

int (*p[8])[5]; 
//p is array of 8 pointers to array of 5 ints.

示例:

int data2[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
int (*a3[3])[4] = { &data2[0], &data2[1], &data2[2] };//array of pointer to int[4]
printf("%d %d %d\n" , (*a3[0])[2], (*a3[1])[0], (*a3[2])[3]);//3 5 12

-

char (*p[8])[5]; 
//p is array of 8 pointers to array of 5 char.

示例:**********我需要一个很好的例子********************

-

int **p;
//p is pointer to pointer to int

示例:**********我需要一个很好的例子********************

-

char **p;
//p is pointer to pointer to char

示例:**********我需要一个很好的例子********************

最佳答案

#include <stdio.h>

int main (void) {
int x = 1;
int y = 2;
int z = 3;
int *a1[3] = {&x, &y, &z};//array of pointer to int
printf("&x = %p, a1[0] = %p\n", &x, a1[0]);
printf("%d %d %d\n", *a1[0], *a1[1], *a1[2]);//1 2 3

int data1[2][3] = { {1,2,3}, {4,5,6}};
int (*a2)[3] = data1;//pointer to int[3]
printf("%d %d\n", a2[0][1], a2[1][2]);//2 6

int data2[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
int (*a3[3])[4] = { &data2[0], &data2[1], &data2[2] };//array of pointer to int[4]
printf("%d %d %d\n", (*a3[0])[2], (*a3[1])[0], (*a3[2])[3]);//3 5 12
return 0;
}

关于c - 请举例帮助我更好地理解 C 中这些不同的类型声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com