c/c++语言开发共享字符串/分段错误

/* Program to calculate trip and plan flights */ #define TRIP 6 #define NAMEMAX 40 #define DEST 1 #include  #include  int main(void) { int i, trip_num, row, col; char travel_name[TRIP][DEST], dest_in[1]; printf("Please enter the number of trips:"); scanf("%d", &trip_num); while (trip_num > TRIP) { printf("Invalid number of trip. (Min of 3 trips and Max 6 trips).n"); /*input number of trips*/ printf("Please enter the number of trips:"); scanf("%d", &trip_num); if (trip_num < TRIP) printf("Valid trip number. Please proceed to enter destination code. n"); } for (i=0; i < trip_num ; i++) { printf("Please enter name of destination(Note: use _ to for spaces in  name):n"); scanf("%s", &dest_in); if (strlen(dest_in)  NAMEMAX) */ for (row = 0; row < trip_num; row++) { for (col=0; col < DEST; col++) printf("Trip#:%d travel_name:%s n", row+1, travel_name[row][col]); } return 0; } 

我试图让用户输入一个名称作为字符串并存储它如果名称是40个字符或更少,但它给我一个分段错误

    您的代码存在很多问题。

    trip_num未初始化。 不知道for循环执行了多少次:

    travel_name[i][0]=dest_in[100];

    可能是写过数组的末尾。 此外,该声明的目的是什么? 我并没有真正遵循它想要做的事情,所以我首先要弄清楚这一点。

    我想是因为 – > trip_numtrip_num

    通过使用调试器可以轻松解决这些类型的问题。 我是否可以建议学习gdb (或cgdb ),或者使用内置调试function(如Eclipse CDT)的完全IDE。

    需要了解更多c/c++开发分享字符串/分段错误,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!

      以上就是c/c++开发分享字符串/分段错误相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      www.ctvol.com true Article c/c++语言开发共享字符串/分段错误

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/980656.html

      (0)
      上一篇 2021年12月14日 上午4:50
      下一篇 2021年12月14日 上午4:53

      精彩推荐