#include<stdio.h>
void myalloc(int **b)
{
 *b=malloc(sizeof(int));
 **b=10;
 printf("**b=%d\n",**b);
}

main()
{
int *a;
 myalloc(&a);// is this convention is correct?
printf("*a=%d\n",*a);
}

i have not initialised *a. can i pass the &a(address of a pointer) without initialising it. Please look at my small program and help me out. is it a proper way of pass by reference in case of pointers?

Edited 6 Years Ago by Nick Evan: n/a

A few things:

main() is an int function (allthough :davis: might disagree right WaltP?) not a void. Please read this . int *a = NULL; would be a way to initialise your pointer. *b=malloc(sizeof(int)); When you've allocated memory, you should free it when your done with it.

Edited 6 Years Ago by Nick Evan: n/a

A few things:

main() is an int function (allthough :davis: might disagree right WaltP?) not a void. Please read this . int *a = NULL; would be a way to initialise your pointer. *b=malloc(sizeof(int)); When you've allocated memory, you should free it when your done with it.

if i dont initialise to NULL will it work. because i have tried it. i dont know is it working correctly.

Edited 6 Years Ago by Nick Evan: n/a

if i dont initialise to NULL. will it work? because i have tried it. i dont knw whether it is working correct.

if i dont initialise to NULL. will it work? because i have tried it.

Well yes...But why not just add "= NULL"?

i dont knw whether it is working correct.

What do you expect it to do? If it does that it is working correct.
From the looks of it, I would say your program will output:

**b=10
*a=10

Edited 6 Years Ago by Nick Evan: n/a

thanx for the reply.. i wil initialise.. just wanted to make sure that it works without initializing. so pointer will always have an address before initialising rite..

This article has been dead for over six months. Start a new discussion instead.