problem with passing address

hi
i have written this code but compiler says you should initialize a why and how??

[#include <stdio.h>
#include <stdlib.h>


void b(int**);


int main()
{
int i;
int **a;


b(a);

a[3][5]=45;

printf("%d\n",a[3][5]);

}

void b(int **a )
{

int i;
a=(int **)malloc(7*sizeof(int *));

for(i=0;i<7;i++)
a[i]=(int *)malloc(9*sizeof(int));



}
[/code]
Last edited on
a doesn't point to anything.
now how can i resolve the problem
when i write this program in this shape it works correctly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdlib.h>

 



int main()
{
	int i;
	
	int **a;


	a=(int **)malloc(7*sizeof(int *));

	for(i=0;i<7;i++)
		a[i]=(int *)malloc(9*sizeof(int));
	
	a[3][5]=453;
	
	printf("%d\n",a[3][5]);
	

}
You can't pass uninitialized variables to a function. In your second example, you're not passing it to a function. You're directly assigning to the variable. I'll write you an easy example to explain what I mean.

1
2
3
4
5
6
7
8
9
10
11
12
int ReturnIt(int x) //this is passing it by value, the value is garbage
{
	return x; //returns garbage
}

int main()
{
	int i; //i is unitialized (aka there's nothing but garbage inside of it)

	ReturnIt(i); //passing i (garbage) to the function

}


EDIT

I take that back. You can pass the address of an uninitialized variable or reference to it.
Last edited on
Topic archived. No new replies allowed.