-4

please explain exactly why do i use pointer on non array variable such as int i know pointer variable store the address of specified variable but why do we need address of just single variable, what is the advantage of doing this?

int a = 10;

int *ptr_a = &a;

Thanks!

UAK
  • 19
  • 4

2 Answers2

0

Although I guess the code you're looking at should give you an answer ... the typical reason this is done is for passing it to a function that should be able to modify this variable.

if you just call foo(a), the function foo() will receive the value of the variable as an argument, so it has it's own copy. The same thing happens with a pointer, but the pointer will of course still point to your original variable, so the function could modify it by dereferencing the pointer (*a = 42;).

0

By using pointer we achieve call by reference mechanism please compile below snippet by removing the pointer and note the output.

#include <stdio.h>
void swap(int *a,int *b);
int main(){
  int num1=5,num2=10;
  swap(&num1,&num2);  /* address of num1 and num2 is passed to swap function */
  printf("Number1 = %d\n",num1);
  printf("Number2 = %d",num2);
  return 0;
}

void swap(int *a,int *b){ /* pointer a and b points to address of num1 and num2 respectively */
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}

Explanation: The address of memory location num1 and num2 are passed to function and the pointers *a and *b accept those values. So, the pointer a and b points to address of num1 and num2 respectively. When, the value of pointer are changed, the value in memory location also changed correspondingly. Hence, change made to *a and *b was reflected in num1 and num2 in main function.

This technique is known as call by reference in C programming.

Roushan
  • 4,074
  • 3
  • 21
  • 38
  • 1
    *This technique is known as call by reference in C programming.* No it is not. C does not support call by reference. This is passing an address by value. – David Heffernan Oct 18 '15 at 07:45
  • @DavidHeffernan but in this link http://www.javatpoint.com/call-by-value-and-call-by-reference-in-c clearly mention call by reference – Roushan Oct 18 '15 at 10:03
  • C does not have call by reference, only call by value, a very well known fact – David Heffernan Oct 18 '15 at 10:26
  • @DavidHeffernan ok ,but can u provide me some resources where i can read about that – Roushan Oct 18 '15 at 10:28
  • Just do a Web search for these terms – David Heffernan Oct 18 '15 at 10:30
  • @DavidHeffernan the link above shared found while search the web but here they mentioned it call by reference – Roushan Oct 18 '15 at 10:31
  • It's a way to simulate call by reference. It's an address passed by value, formally. – David Heffernan Oct 18 '15 at 11:26
  • Oh my ... this discussion could easily be more productive by getting to the point: "This technique allows you to have call by reference *semantics* in C". Yes, C itself is call by value *only*, passing a pointer (by value, of course) is the way to get around it. –  Oct 18 '15 at 11:39