分段失败 #包括 #包括 内部主(空) { 积分i,j; int*ptr,*ptr1; int-over; printf(“输入超过的数量”); 扫描频率(“%d”及以上); ptr=(int*)malloc(超过*sizeof(int)); //进行迭代,外循环,逐行读取。。。 对于(i=0;i
您正在使用的分段失败 #包括 #包括 内部主(空) { 积分i,j; int*ptr,*ptr1; int-over; printf(“输入超过的数量”); 扫描频率(“%d”及以上); ptr=(int*)malloc(超过*sizeof(int)); //进行迭代,外循环,逐行读取。。。 对于(i=0;i,c,C,您正在使用的 #include <stdio.h> #include <stdlib.h> int main(void) { int score,i,j; int *ptr, *ptr1; int over; printf("Enter the number of over"); scanf("%d",&over); ptr=(int*)malloc(over*sizeof(int)); // do the
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int score,i,j;
int *ptr, *ptr1;
int over;
printf("Enter the number of over");
scanf("%d",&over);
ptr=(int*)malloc(over*sizeof(int));
// do the iteration, outer for loop, read row by row...
for(i=0; i <= (over-1); i++)
{
printf("%d%d ", i, ptr[i]);
// inner for loop, for every row, read column by column and print the bar...
printf("Enter the number of run per over");
scanf("%d",&score);
ptr1=(int*)malloc(score*sizeof(int));
for(j = 1; j<= ptr1[i]; j++)
// print the 'bar', and repeat...
printf("|");
// go to new line for new row, and repeats...
printf("\n");
}
return 0;
}
在for循环中。这会导致内存泄漏。您应该释放内存
你也有
ptr1=(int*)malloc(score*sizeof(int));
但是ptr[i]
没有分配任何值,所以它只给出垃圾值
printf("%d%d ", i, ptr[i]);
(j=1;j- 铸造malloc的结果没有任何意义,它是
。打印未初始化内存单元的值。这是未定义的行为,理论上可能会导致程序在某些平台上崩溃。如果需要将分配的内存初始化为零,则应改为使用calloc()printf(“%d%d”,i,ptr[i]);
ptr1=(int*)malloc(score*sizeof(int));对于(j=1;j
#包括 #包括 内部主(空){ 整数**分数; 整数超过,得分; int i,j; printf(“输入超过:”)的数量; 扫描频率(“%d”及以上); 分数=(整数**)malloc(大于*平方英尺(整数*); 对于(i=0;i
对于(j=1;j)问题是什么?你的问题是什么?我认为你没有陈述一个实际的问题。
你从哪里得到segfault?(使用调试器或其他调试printf语句)我可以提出的任何建议,而不是这两个lines@emmie你的意图是什么如何处理线条?不要键入malloc的cast返回值。@Peter我想动态打印每个比赛结束分段的板球得分Fault@emmie这项工作对我来说很好。我可能弄错了,因为它并不广为人知。你输入了什么?for(j=1;j=score
for(j = 1; j<= ptr1[i]; j++)
#include <stdio.h> #include <stdlib.h> int main(void){ int **scores; int over, score; int i, j; printf("Enter the number of over : "); scanf("%d", &over); scores = (int**)malloc(over*sizeof(int*)); for(i = 0; i < over; i++){ printf("%d ", i + 1); printf("Enter the number of run per over : "); scanf("%d", &score); scores[i] = (int*)malloc((score+1) * sizeof(int));// +1 for number of columns scores[i][0] = score; for(j = 1; j <= score; j++){ printf("%d Enter the score : ", j); scanf("%d", &scores[i][j]); } } for(i = 0; i < over; i++){ for(j = 1; j <= scores[i][0]; j++){ printf("|%d", scores[i][j]); } printf("|\n"); } //deallocate for(i = 0; i < over; i++) free(scores[i]); free(scores); return 0; }