874 views
0 0 votes
Can we pass pointers in the arguments of a function? If yes then do we pass the value the pointer holds or the address of the pointer?

2 Answers

0 0 votes
Yes we can pass pointers as an argument of a function

Pointers itself contains address of any data item value.

For example int a = 10;   int *b =&a;

here 'b' contains address of 'a' and at address of 'a' contains the value = 10.

if we simply pass ' &b ' it contains address and if we pass ' *b ' then it contains value at the address stored in 'b'
• edited by
0 0 votes

We of course can - otherwise printf function can never work because the first argument to it is a "pointer to char". And when we pass a pointer, its value is being passed - but the value being a pointer is expected to be an address of some other object. 

char *s = "hello";
printf(s);
char a[2] = {'a', '\0'};
printf(a);
char b = '\0';
printf(&b);
int d = 9;
printf("%d", d); // here address of the string literal "%d" is passed to printf

 

Position:
Show:

Related questions

8 8 votes
1 1 answer
266
266 views
GO Classes asked Jun 19
266 views
What is the output of the following code?#include <stdio.h void fun(int a[], int *p) { a = a + 5; *(p + 2) = *(p + 2) + 10; p++; *p = *p + 1; } int main() { int arr[] =...
6 6 votes
1 1 answer
217
217 views
GO Classes asked Jun 19
217 views
What is the output of the following code?#include <stdio.h int main() { int arr[] = {10, 20, 30}; int *p = arr; printf("%d %d", *p, *(arr + 2)); return 0; }$\texttt{10 20...
8 8 votes
1 1 answer
225
225 views
GO Classes asked Jun 6
225 views
What is the output of the following code?#include <stdio.h void change(int *p, int *q) { *p = *p + 4; *q = *p + *q; p = q; *p = *p - 3; } int main() { int a = 6, b = 10; ...
8 8 votes
1 1 answer
231
231 views
GO Classes asked Jun 6
231 views
What is the output of the following code?#include <stdio.h int main() { int a = 8, b = 12; int *p = &a; int *q = &b; *p = *p + *q; q = p; *q = *q - 5; printf("%d %d", a, ...