-1

现在的问题是关于 typedef 和 c 样式字符串。这是代码。

 #include <stdio.h>
 #define C "BTP300", "BTD310", "BTI320", "BTS330"
 typedef char* Course;// Line 1
 #define CMP(a,b,c,d) ((a[(c)])d(b[(c)]))

 int main(void) {
     Course reference, test[] = {C}; //Line 2
     sort(test,4,2);
     reference=test[0];
     difference(test, reference, 4);
     return 0;
 }


    void sort(Course a[], int n, int k) {
         int i, j;
         Course temp;
         for (i = n - 1; i > 0; i--) {
             for (j = 0; j < i; j++) {
                 if (CMP(a[j],a[j+1],k,>)) {
                     temp = a[j];
                     a[j] = a[j+1];
                     a[j+1] = temp;
                 }
             }
         }
     }

 void difference(Course a[], Course reference, int n) {
     int i;
     for (i = 0; i < n; i++)
         printf("%d ", ABS(a[i] - reference));
     printf("\n");
 }

这就是我所理解的: -Course 是 char 类型指针的别名。-reference 是 char 类型的指针。

我不知道或需要知道代码背后的理论是: 1- 什么是 test[] ??? 我认为它是一个 char 类型的指针数组,换句话说 test[1] 包含 char*="btd310" 。(我不知道它的推理。)

2-我如何访问“btd310”中的“d”?3- 使用 typedef int* 如何创建二维 int 数组?我一直在寻找类似的东西

typedef int* arr;
void main(){
arr foo[]={{1,2},{3,4}}
}

显然上面的代码对我不起作用,所以它将以哪种方式工作,即我将获得 2d int 数组。

提前谢谢大家。顺便说一句,可能有更好的其他方法可以做到这一点,但我必须以这种方式学习。

上面代码中的两个宏是如何工作的?

4

2 回答 2

1

1-什么是测试[] ??? 我认为它是一个 char 类型的指针数组,换句话说 test[1] 包含 char*="btd310" 。

test 是 Course 类型的数组。

2-我如何访问“btd310”中的“d”?

测试[1][2]

3- 使用 typedef int* 如何创建二维 int 数组?

为什么不这样做:

int foo[][2]={{1,2},{3,4}}
于 2013-10-27T08:32:33.623 回答
0
  1. test是一个数组Course,即一个char指针数组。课程的类型将写为char *()[]. 至于该数组的内容,预处理器将简单地替换"BTP300", "BTD310", "BTI320", "BTS330"C因此 test 的初始化读取test[] = {"BTP300", "BTD310", "BTI320", "BTS330"}

  2. 你得到了'D'with test[1][2]。请注意,C 在任何地方都区分大小写,因此'D''d'.

  3. char*和的初始化中使用字符串char ()[]有点特殊,所以最好不要考虑。要声明 的二维数组int,您只需使用int foo[2][2] = {{1, 2}, {3, 4}};如果必须使用 typedef,您可以使用typedef int arr[2];为 2 个整数的一维数组定义数组类型,然后您可以制作二维数组的一维数组2 个整数,带arr foo[2] = {{1, 2}, {3, 4}};.

于 2013-10-27T08:35:12.687 回答