#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

Line 11 to 16

//display result
for (i = 0; i < 2; i++) {
   printf("\n");
   for (j = 0; j < 2; j++) {
       printf("%d \t", result[i][j]);
   }
}

i ran the code, the above changes seem to work

#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");

}

thanks... very active site.. like it..

You need braces around the outer loop in main:

for(i=0;i<2;i++) {
    printf("\n");
    for(j=0;j<2;j++)
        printf("%d \t",result[i][j]);
}

Braces may only be omitted if the loop body consists of a single statement.

in the line 30 why dont we use

array =(int**) malloc(nrows * sizeof(int *));

instead of

array = malloc(nrows * sizeof(int *));

is this because the compiler take the int datatype as default

in the line 30 why dont we use

array =(int**) malloc(nrows * sizeof(int *));

instead of

array = malloc(nrows * sizeof(int *));

is this because the compiler take the int datatype as default

The cast is unnecessary because C allows an implicit conversion to and from void* with any other object pointer type. As long as malloc is properly declared (such as by including <stdlib.h>), all is well.

thanks..one more doubt
in some codes the library malloc.c is used. actually when is this library used???

#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... some help...

#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

in some codes the library malloc.c is used. actually when is this library used???

They're wrong. If you want to portably declare malloc, include <stdlib.h>. Any other header will lock you into the compiler.

>if(x=NULL)
This is a fatal typo. You're not comparing x to NULL, you're setting it to NULL. The condition then becomes equivalent to:

if (NULL != 0)

Which is pretty much guaranteed to be false, so your program thinks x is valid when it really isn't. You then later go on to dereference the pointer, and dereferencing a null pointer is a good way to get an access violation error at runtime.

ok. that answer was informative... thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.