鱼C论坛

 找回密码
 立即注册
查看: 1726|回复: 1

C Primer Plus Problem

[复制链接]
发表于 2021-3-21 10:13:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1.     #include <stdio.h>
  2. #include <string.h>
  3. #define SIZE 100
  4. #define answer "Grant"
  5. char *s_gets(char * st, int n)
  6. {
  7.     int i =0;
  8.     char *ret_val;
  9.     ret_val = fgets(st,n,stdin);
  10.     if (ret_val)
  11.     {
  12.         while(st[i]!='\n' && st[i] != '\0')
  13.         i++;
  14.         if(st[i] == '\n')
  15.         st[i] = '\0';
  16.         else
  17.         while(getchar() != '\n')
  18.         continue;
  19.     }
  20.     return ret_val;
  21. }
  22. int main(void)
  23. {
  24.     char *try;
  25.     s_gets(try,100);
  26.     puts("Who is buried in Grant's dig?");
  27.     while(try != answer)
  28.     {
  29.         printf("No,that's wrong. Try aggin");
  30.         s_gets(try,SIZE);
  31.     }
  32.     puts("Right!DO a good job!");
  33.     return 0;
  34. }
复制代码
《C Primer Plus 》上的例子 说---------------------answer 和 try 都是指针,这我就不明白了?真的吗?求背后原理

谢谢你们啦!!!!!!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-21 10:40:06 | 显示全部楼层
try是个止震板毫无疑问
另一个实验一下就知道了,另外宏变量最好全部大写

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define SIZE 100
  4. #define answer "Grant"

  5. int main(void)
  6. {
  7.     printf("%s\n", answer+1);
  8.     /*
  9.     实际上,宏定义的字符串常量在预编译的时候把answer替换成字符串常量(“Grant”);
  10.         所以answer+1可以理解为“Grant”+1 即常量首地址+1,故输出“rant”.
  11.     */
  12.     printf("%c\n", answer[2]); // 当成数组来使用
  13.     return 0;
  14. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 22:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表