0 0 votes closed with the note: Error what is wrong with this code? It shows segmentation fault #include<stdio.h> #include<stdlib.h> void main() { int i,j,count=1; int **a=(int**)malloc(3*sizeof(int*)); for(i=0; i<3; i++) { a[i]=(int*)malloc(4*sizeof(int)); } for(i=0; i<3; i++) { for(j=0; j<4; j++) { a[i][j]=count; count++; } } printf("\n"); for(i=0; i<3; i++) { for(j=0; j<4; j++) { printf("%d ",a[i][j]); } printf("\n"); } } Programming in C programming-in-c array array-of-pointers pointers + – aditi19 1.5k views comment Share Follow Print See all 6 Comments 6 6 Comments reply Show 3 previous comments Shaik Masthan commented Dec 22, 2018 reply Follow flag @akshat16 it is for implementing a[3][4], so it should be there 0 0 replyShare akshat16 commented Dec 22, 2018 reply Follow flag your code is right too, but I merely want to say it can be furthur optimised I mean second for loop is redundant try this code #include<stdio.h> #include<stdlib.h> void main() { int i,j,count=1; int **a=(int**)malloc(3*sizeof(int*)); for(i=0; i<3; i++) { a[i]=(int*)malloc(4*sizeof(int)); for(j=0; j<4; j++) { a[i][j]=count; count++; } } printf("\n"); for(i=0; i<3; i++) { for(j=0; j<4; j++) { printf("%d ",a[i][j]); } printf("\n"); } return 4; } 0 0 replyShare Shaik Masthan commented Dec 22, 2018 reply Follow flag ok... so your comment is on optimization. 0 0 replyShare Please log in or register to add a comment.
0 0 votes instead of writing of void main() plz write the int main(); Rackson answered Sep 13, 2018 Rackson comment Share Follow See 1 comment 1 1 comment reply manisha11 commented Sep 13, 2018 reply Follow flag That doesn't matter here. 0 0 replyShare Please log in or register to add a comment.