#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;
}
Asked
Active
Viewed 82 times
-4

Peter Miehle
- 5,984
- 2
- 38
- 55

emmie
- 1
-
3what is the problem? – Peter Miehle Apr 08 '15 at 11:05
-
1What is your question?? – Karthikeyan.R.S Apr 08 '15 at 11:06
-
1I think you've failed to state an actual question. – David Hoelzer Apr 08 '15 at 11:07
-
have you prooven, that over is bigger than 0 and of reasonable amount? – Peter Miehle Apr 08 '15 at 11:08
-
1`for(j = 1; j<= ptr1[i]; j++)` might wrong. E.g when `i >= score` – BLUEPIXY Apr 08 '15 at 11:11
-
i am geting segmentation fault error – emmie Apr 08 '15 at 11:11
-
1where do you get the segfault? (use a debugger or some more debug-printf-statements – Peter Miehle Apr 08 '15 at 11:12
-
@emmie do you want jagged 2D array? – BLUEPIXY Apr 08 '15 at 11:20
3 Answers
4
You are using
ptr1=(int*)malloc(score*sizeof(int));
inside your for loop. That causes memory leak. You should free the memory.
You also have
printf("%d%d ", i, ptr[i]);
But ptr[i]
has not been assigned any value, so it just gives garbage value. The same problem occurs in
for(j = 1; j<= ptr1[i]; j++)
So you need to assign some value to them before using them like this.

Arun A S
- 6,421
- 4
- 29
- 43
-
-
-
3Don't type cast return value of malloc. http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – niyasc Apr 08 '15 at 11:11
-
0
- Casting the result of malloc doesn't make any sense, it is pointless and potentially bad practice.
printf("%d%d ", i, ptr[i]);
. You print the value of an uninitialized memory cell. This is undefined behavior and might in theory cause the program to crash on some platforms. If you need the memory allocated to be initialized to zero, you should be using calloc() instead.ptr1=(int*)malloc(score*sizeof(int)); for(j = 1; j<= ptr1[i]; j++)
This code makes no sense whatsoever and will crash the program. You use ptr1 as if it was an initialied array of integers, while it is actually an uninitialized, single integer.
0
#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;
}

BLUEPIXY
- 39,699
- 7
- 33
- 70
-
-
@emmie this work fine to me. I might have been mistaken because it does not well known. What did you input? [DEMO](http://ideone.com/oaW5Yd) – BLUEPIXY Apr 08 '15 at 11:50