Thread: returning arrays from function using pointer_segmentation fault

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    Smile returning 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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your indentation suggests that your for loop on i has more than one statement in it; however, the code does not agree. Use braces.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    The display loop is incorrect btw you need to free the array.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    Smile solved... thanks

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int **array();
     main()
    {
      int **result=array(),i,j;
    
    
    
      //display result
      for(i=0;i<2;i++)                             //CORRECTED HERE. one extra printf was here
    	  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+2;
    
    	    
    	
    
    
    
    	//returning pointer
    	return array;
    printf("hi");
    
    }

    thank you. apologize for my negligence. this site is very active, like it...

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    Smile passing array to function using pointers

    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 comes.. help please
    Last edited by vineeshvs; 01-14-2011 at 10:42 PM.

  6. #6
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    are you kidding? I thought you fixed it in post #4. Still need to free the arrays.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If by "segmentation fault comes" you mean it doesn't compile, then yes, I see where things went wrong. Two mistakes that I can point out include the way function() and main() were both written. For function(), since the prototype says

    void function(int **x);

    then the definition should also say

    void function(int **x)

    For main(), the standard expects one of the following to be main's signature in C++

    int main()
    int main(int argc, char *argv[])

    or some variation thereof using the const keyword. But the main thing is that main() returns int.

    As for an actual segmentation fault, I don't see one.

  8. #8
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    her'es your problem
    Code:
     if(x[i]=NULL) // use "==" to compare.
    That's is assignment. You just made all your array NULL. That's why you can't access x[i][j], thus segfault
    "All that we see or seem
    Is but a dream within a dream." - Poe

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku Brute Force Algorithm Help (recursion?)
    By ridilla in forum C Programming
    Replies: 22
    Last Post: 05-07-2010, 03:31 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  5. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 07:05 AM

Tags for this Thread