How to return arrays from function using pointer_segmentation fault?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vineesh vs
    New Member
    • Jan 2011
    • 10

    How to return arrays from function using pointer_segmentation fault?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int **array();
     main()
    {
      int **result=array(),i,j;
    
    
    
      //display result
    for(i=0;i<2;i++)
      printf("\n");
    	  for(j=0;j<2;j++)
    	    printf("%d \t",result[i][j]);
    
    }
    
    
    
    
    //function
    int **array()
    {
      int nrows=2,ncolumns=2,i,j;
    
    
      // printf("hi");
    
      //memory allocation
      int **array;
    	array = malloc(nrows * sizeof(int *));
    	if(array == NULL)
    		{
    		printf("out of memory\n");
    		return 0;
    		}
    
    	//printf("hi");
    	for(i = 0; i < nrows; i++)
    	  	{
    	  	array[i] = malloc(ncolumns * sizeof(int));
    			if(array[i] == NULL)
    			{
    			printf("out of memory\n");
    			return 0;
    			}
    		}
    
     
    
    
    	//create array
    	for(i=0;i<2;i++)
    	  for(j=0;j<2;j++)
    	    array[i][j]=i+j;
    
    
    
    
    	//returning pointer
    	return array;
    printf("hi");
    
    }

    it compiled successfully. but shows segmentation fault on running. please help to find error
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    your problem is in
    Code:
      //display result
    for(i=0;i<2;i++)
      printf("\n");
          for(j=0;j<2;j++)
            printf("%d \t",result[i][j]);
    the first for() statement prints two newlines and exits with i=2
    you then execute the second for() statement and attempt to print result[2][0]
    try putting {} so the first for() loop executes the second for() loop
    Code:
    for(i=0;i<2;i++)
    {
      printf("\n");
          for(j=0;j<2;j++)
            printf("%d \t",result[i][j]);
    }

    Comment

    • vineesh vs
      New Member
      • Jan 2011
      • 10

      #3
      in the line 32 why dont we use




      Code:
      array =(int**) malloc(nrows * sizeof(int *));
      instead of
      Code:
      array = malloc(nrows * sizeof(int *));
      is this because the compiler take the int datatype as default

      Comment

      • vineesh vs
        New Member
        • Jan 2011
        • 10

        #4
        Code:
        #include<stdio.h>
        #include<stdlib.h>
        
        
        void function(int **x);
        
        
        main()
        {
          int nrows=2,ncolumns=2,i,j;
        
        
          //memory allocation for x
          int **x=malloc(nrows*sizeof(int*));
          if(x==NULL)
            {
              printf("out of memory\n");
              return 0;
            }
          for(i=0;i<nrows;i++)
            {
              x[i]=malloc(ncolumns*sizeof(int));
              if(x[i]=NULL)
        	{
        	  printf("out of memory\n");
        	  return 0;
        	}
            }
        
        
          printf("code passed me");//checking
        
        
          //define x
          for(i=0;i<2;i++)	  
            for(j=0;j<2;j++)	    
              x[i][j]=i+j+2;
         
        
        
        
        
          //call function
          function(x); 
        
        }
        
        
        
        
        
        
        
        //function_definition
        function(int **x)
        
        {
          int nrows=2,ncolumns=2,i,j,y[2][2];
        
        		  
          for(i=0;i<2;i++)
            for(j=0;j<2;j++)
              y[i][j]=x[i][j]+1;
        
        
        
          //display y
          for(i=0;i<2;i++)
            for(j=0;j<2;j++)
              printf("%d",y[i][j]);		 
        }

        segmentation fault... help please...

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          have a look at Method 4 in the following tutorial on pointers

          Comment

          • vineesh vs
            New Member
            • Jan 2011
            • 10

            #6
            thanks.... also in 23rd line i forgot a =

            Comment

            Working...