0
#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 by Nick Evan: n/a

2
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by mailsteam
0

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 by Nick Evan: n/a

0

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 by Nick Evan: n/a

0

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

0

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 by Nick Evan: n/a

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.