Maker Pro
Maker Pro

Function and their types

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I am passing address of variable a

That is incorrect.
Code:
#include<stdio.h>
 
void function(int a);
int main(void)
{
 int z=10;
 printf(" Before calling z = %d \n",z);
 function(z);
 printf("After calling z=%d \n",z);
 return 0;
}
void function(int *a)
{
 a*=15;
}

Consider the example above.

During execution, what does a hold?
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
int z; // we are declaring variable name z is name of variable //
int z=10; // we are declaring variable and assigning value 10 to variable z

&z means the address of variable z

int *a // we are declaring pointer variable and name of pointer variable is a and * is operator //
int *a = 15 // we are declaring and assigning value 15 to pointer variable a //

int *a = &Z means we are assigning address of variable z to pointer variable a

Code:
#include<stdio.h>
 
void function(int a);
int main(void)
{
 int z=10;
 printf(" Before calling z = %d \n",z);
 function(z);
 printf("After calling z=%d \n",z);
 return 0;
}
void function(int *a)
{
 a*=15;
}

actual arguments is z=10; and formal arguments is a* = 15

When program start to execute. it store the value of variable z = 10 then when we call function we pass actual value 10 to function this is known as function call by value, we are passing the copy of actually argument to function

During execution, what does a hold?

I think during execution a will hold the actual value 10
 
Last edited:

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
actual arguments is z=10; and formal arguments is a* = 15

Aren't they the same thing? How can they be different?

I think during execution a will hold the actual value 10

No. It can't hold 10, because a is not an integer variable.

int *a = 15 // we are declaring and assigning value 15 to pointer variable a //

But we never do that, right?
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Aren't they the same thing?

int z = 10;
int * a = 15;

I think a is pointer variable and it store the address of variable 15
No. It can't hold 10, because a is not an integer variable

so pointer variable store only the address of another variable. a will hold the address of z during the execution right

But we never do that, right?
int *a = 15
we don't write this because compiler assign address to pointer variable right
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,738
Joined
Nov 17, 2011
Messages
13,738
I think a is pointer variable and it store the address of variable 15
There is no variable named 15.
a is a pointer to an address that stores an integer.
*a is the value stored at this adress. This value is 15.

so pointer variable store only the address of another variable
A pointer stores an address. This address need not necessarily be the address of a variable. It can also be the address of an I/O port, of a data structure or whatever.

int *a = 15
we don't write this because compiler assign address to pointer variable right
Syntactically this is o.k. You can do this, if you like and if you understand the correct use of pointers. But typically there is simply no use for using this declaration. If you need to declare an integer, use "int a=15".

Generally pointers should be used with lots of care since it is very easy to create havoc using wrong pointer assignments. Incorrectly using pointers can destroy memory content in unexpected locations leading to totally unexpected and unpredictable program behavior.
 
Top