使用typedef注意事项
- typedef语句不能创造新的类型,只能为已有的类型重新命名,也就是说原来的类型名仍然可以使用。
- typedef语句只能用来定义类型名,而不能用来定义变量。
- 举个例子 typedef int COUNTER;COUNTER i,n;与int i,n等价。
相关文章
- #include <> typedef struct Node { int index; struct Node *next; }JosephuNode; int Josephu(int n, int m) { int i, j; JosephuNode *head, *tail; head = tail = (JosephuNode *)malloc(sizeof(JosephuNode)); for (i = 1; i < n; ++i) { tail->index = i; tail->next = (JosephuNode *)malloc(sizeof(JosephuNode)); tail = tail->next; } tail->index = i; tail->next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号\n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; }">设编号为1,2,… n的n个人围坐一圈,约定编号为k(1数组实现: #include <> #include <> int Josephu(int n, int m) { int flag, i, j = 0; int *arr = (int *)malloc(n * sizeof(int)); for (i = 0; i < n; ++i) arr[i] = 1; for (i = 1; i < n; ++i) { flag = 0; while (flag < m) { if (j == n) j = 0; if (arr[j]) ++flag; ++j; } arr[j - 1] = 0; printf("第%4d个出局的人是:%4d号\n", i, j); } free(arr); return j; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; } 链表实现: #include <> #include <> typedef struct Node { int index; struct Node *next; }JosephuNode; int Josephu(int n, int m) { int i, j; JosephuNode *head, *tail; head = tail = (JosephuNode *)malloc(sizeof(JosephuNode)); for (i = 1; i < n; ++i) { tail->index = i; tail->next = (JosephuNode *)malloc(sizeof(JosephuNode)); tail = tail->next; } tail->index = i; tail->next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号\n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; }
- typedef typename的用法
- c语言结构体定义和使用typedef,结构体定义typedef struct 用法详解
- ST官方库函数之GPIO复位函数void GPIO_DeInit(GPIO_TypeDef* GPIOx) 的理解
- typedef关键字
- typedef
- 用typedef来定义数组
- 使用typedef语句定义数组类型
- typedef 与指针、多维数组
- c语言_struct SeqList 与 typedef struct用法