鱼C论坛

 找回密码
 立即注册
查看: 1619|回复: 2

[已解决]运行空白,求助

[复制链接]
发表于 2023-2-23 21:36:07 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdarg.h>
  4. #define TRUE 1
  5. #define ERROR -1
  6. //串的堆分配储存表示
  7. typedef struct {
  8.         char *ch;//若是非空字符串,则按串长分配存储区,否则ch为NULL
  9.         int length;//串长度
  10. } HString;
  11. HString StrAssign(HString Str, char* chars) {

  12.         //生成一个其值等于串常量的chars的串T
  13.         if (Str.ch) {
  14.                 //释放T的空间
  15.                 free(Str.ch);
  16.         }
  17.         int len = 0;
  18.         printf_s("chars=%s\n", chars);
  19.         while (1) {
  20.                 ++len;
  21.                 if (chars[len] == 0) {
  22.                         //TODO
  23.                         break;
  24.                 }
  25.         }
  26.         if (!len) {
  27.                 //长度为0为ch赋NULL值
  28.                 Str.ch = NULL;
  29.                 Str.length = 0;
  30.         } else {
  31.                 if (!(Str.ch = (char*)malloc(len * sizeof(char)))) {
  32.                         //空间不足退出
  33.                         exit(ERROR);
  34.                 }
  35.                 for (int i = 0; i <= len - 1; i++) {
  36.                         //TODO
  37.                         Str.ch[i] = chars[i];
  38.                 }
  39.                 Str.length = len;
  40.         }
  41.         return Str;
  42. }
  43. int StrLength(HString S) {
  44.         return S.length;
  45. }
  46. int StrCompares(HString S, HString T) {
  47. //        若S>T,则返回值>0;若S<T,则返回值=0,若S<T,则返回值<0
  48.         printf_s("123\n");
  49.         for (int i = 0; i <= S.length && i < T.length; ++i) {
  50.                 //TODO
  51.                 if (S.ch[i] != T.ch[i]) {
  52.                         //TODO
  53.                         return S.ch[i] - T.ch[i];
  54.                 }
  55.                 printf_s("i的值为%d\n", i);
  56.         }
  57.         return S.length - T.length;
  58. }
  59. int main(void) {
  60.         HString T, S, M;
  61.         char* Tchars = "Hello World!";
  62.         char* Schars = "Hello C!";
  63.         char* message = "";
  64.         T = StrAssign(M, Tchars);
  65.         printf_s("字符串为%s\n", Tchars);
  66.         printf_s("字符串长度为%d\n", T.length);
  67.         S = StrAssign(M, Schars);
  68.         printf_s("字符串为%s\n", Schars);
  69. //        删除该行运行正常
  70.         message = StrCompares(S, T) == 0 ? "S字符串大于T字符串" : "S字符串大于或小于T字符串";
  71.         printf_s("%s\n", message);
复制代码
最佳答案
2023-2-23 21:48:19
本帖最后由 ExiaGN001 于 2023-2-23 21:56 编辑

有些情况下,用char*存包括汉字的两字节字符,是会炸的。
返回值:0xC0000374
代表着堆出错了
因为message指向的地方只有1字节,存不了那么大的数据。
将message有关部分删去并用原三目运算符替代message就可以了

发上来的代码有个致命的语法bug:
main函数没封口。这是初学者犯得最多的问题之一


改完的代码:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdarg.h>
  4. #define TRUE 1
  5. #define ERROR -1
  6. //串的堆分配储存表示
  7. typedef struct {
  8.         char *ch;//若是非空字符串,则按串长分配存储区,否则ch为NULL
  9.         int length;//串长度
  10. } HString;
  11. HString StrAssign(HString Str, char* chars) {

  12.         //生成一个其值等于串常量的chars的串T
  13.         if (Str.ch) {
  14.                 //释放T的空间
  15.                 free(Str.ch);
  16.         }
  17.         int len = 0;
  18.         printf_s("chars=%s\n", chars);
  19.         while (1) {
  20.                 ++len;
  21.                 if (chars[len] == 0) {
  22.                         //TODO
  23.                         break;
  24.                 }
  25.         }
  26.         if (!len) {
  27.                 //长度为0为ch赋NULL值
  28.                 Str.ch = NULL;
  29.                 Str.length = 0;
  30.         } else {
  31.                 if (!(Str.ch = (char*)malloc(len * sizeof(char)))) {
  32.                         //空间不足退出
  33.                         exit(ERROR);
  34.                 }
  35.                 for (int i = 0; i <= len - 1; i++) {
  36.                         //TODO
  37.                         Str.ch[i] = chars[i];
  38.                 }
  39.                 Str.length = len;
  40.         }
  41.         return Str;
  42. }
  43. int StrLength(HString S) {
  44.         return S.length;
  45. }
  46. int StrCompares(HString S, HString T) {
  47. //        若S>T,则返回值>0;若S<T,则返回值=0,若S<T,则返回值<0
  48.         printf_s("123\n");
  49.         for (int i = 0; i <= S.length && i < T.length; ++i) {
  50.                 //TODO
  51.                 if (S.ch[i] != T.ch[i]) {
  52.                         //TODO
  53.                         return S.ch[i] - T.ch[i];
  54.                 }
  55.                 printf_s("i的值为%d\n", i);
  56.         }
  57.         return S.length - T.length;
  58. }
  59. int main(void) {
  60.         HString T, S, M;
  61.         char* Tchars = "Hello World!";
  62.         char* Schars = "Hello C!";
  63.         
  64.         T = StrAssign(M, Tchars);
  65.         printf_s("字符串为%s\n", Tchars);
  66.         printf_s("字符串长度为%d\n", T.length);
  67.         S = StrAssign(M, Schars);
  68.         printf_s("字符串为%s\n", Schars);
  69.         printf_s("%s\n", StrCompares(S, T) == 0 ? "S字符串大于T字符串" : "S字符串大于或小于T字符串");
  70. }
复制代码

在我这边的运行结果:(以供校验)
  1. chars=Hello World!
  2. 字符串为Hello World!
  3. 字符串长度为12
  4. chars=Hello C!
  5. 字符串为Hello C!
  6. 123
  7. i的值为0
  8. i的值为1
  9. i的值为2
  10. i的值为3
  11. i的值为4
  12. i的值为5
  13. S字符串大于或小于T字符串
复制代码


有用请设置最佳谢谢
另:个签打错了:
  1. 古今多少事,都付笑谈中
复制代码
这是两个分句
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-2-23 21:48:19 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ExiaGN001 于 2023-2-23 21:56 编辑

有些情况下,用char*存包括汉字的两字节字符,是会炸的。
返回值:0xC0000374
代表着堆出错了
因为message指向的地方只有1字节,存不了那么大的数据。
将message有关部分删去并用原三目运算符替代message就可以了

发上来的代码有个致命的语法bug:
main函数没封口。这是初学者犯得最多的问题之一


改完的代码:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdarg.h>
  4. #define TRUE 1
  5. #define ERROR -1
  6. //串的堆分配储存表示
  7. typedef struct {
  8.         char *ch;//若是非空字符串,则按串长分配存储区,否则ch为NULL
  9.         int length;//串长度
  10. } HString;
  11. HString StrAssign(HString Str, char* chars) {

  12.         //生成一个其值等于串常量的chars的串T
  13.         if (Str.ch) {
  14.                 //释放T的空间
  15.                 free(Str.ch);
  16.         }
  17.         int len = 0;
  18.         printf_s("chars=%s\n", chars);
  19.         while (1) {
  20.                 ++len;
  21.                 if (chars[len] == 0) {
  22.                         //TODO
  23.                         break;
  24.                 }
  25.         }
  26.         if (!len) {
  27.                 //长度为0为ch赋NULL值
  28.                 Str.ch = NULL;
  29.                 Str.length = 0;
  30.         } else {
  31.                 if (!(Str.ch = (char*)malloc(len * sizeof(char)))) {
  32.                         //空间不足退出
  33.                         exit(ERROR);
  34.                 }
  35.                 for (int i = 0; i <= len - 1; i++) {
  36.                         //TODO
  37.                         Str.ch[i] = chars[i];
  38.                 }
  39.                 Str.length = len;
  40.         }
  41.         return Str;
  42. }
  43. int StrLength(HString S) {
  44.         return S.length;
  45. }
  46. int StrCompares(HString S, HString T) {
  47. //        若S>T,则返回值>0;若S<T,则返回值=0,若S<T,则返回值<0
  48.         printf_s("123\n");
  49.         for (int i = 0; i <= S.length && i < T.length; ++i) {
  50.                 //TODO
  51.                 if (S.ch[i] != T.ch[i]) {
  52.                         //TODO
  53.                         return S.ch[i] - T.ch[i];
  54.                 }
  55.                 printf_s("i的值为%d\n", i);
  56.         }
  57.         return S.length - T.length;
  58. }
  59. int main(void) {
  60.         HString T, S, M;
  61.         char* Tchars = "Hello World!";
  62.         char* Schars = "Hello C!";
  63.         
  64.         T = StrAssign(M, Tchars);
  65.         printf_s("字符串为%s\n", Tchars);
  66.         printf_s("字符串长度为%d\n", T.length);
  67.         S = StrAssign(M, Schars);
  68.         printf_s("字符串为%s\n", Schars);
  69.         printf_s("%s\n", StrCompares(S, T) == 0 ? "S字符串大于T字符串" : "S字符串大于或小于T字符串");
  70. }
复制代码

在我这边的运行结果:(以供校验)
  1. chars=Hello World!
  2. 字符串为Hello World!
  3. 字符串长度为12
  4. chars=Hello C!
  5. 字符串为Hello C!
  6. 123
  7. i的值为0
  8. i的值为1
  9. i的值为2
  10. i的值为3
  11. i的值为4
  12. i的值为5
  13. S字符串大于或小于T字符串
复制代码


有用请设置最佳谢谢
另:个签打错了:
  1. 古今多少事,都付笑谈中
复制代码
这是两个分句
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-2-23 23:04:49 | 显示全部楼层
ExiaGN001 发表于 2023-2-23 21:48
有些情况下,用char*存包括汉字的两字节字符,是会炸的。
返回值:0xC0000374
代表着堆出错了

成功运行,个签已改,感谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 05:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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