Pointers and Functions

0 lynellmacnamara November 13, 2020

In last tutorials, WQe have learn about pointer. In this tutorials, We will learn pointers and functions.

A pointer can be passed to a function as an argument. Passing a pointer means pasing address of a variable instead of the value of the variable. As address is passed in this case, this mechanism is also known as call by address or call by reference

W#hen pointer is passed to a function while functiuon calling, the formal argument of the function must be compatible with the passing pointer i.e. if integer pointer is being passed, the formal argument in function must be pointer of the type integer and so forth. As Addressof variable is passd in this mexhanism, if value in the passed address is changed within function definition, the valu of actual variable also changed.

Lets look an example to make clear concepts on pointer and functions.

// C++ program to illustrate Array Name as Pointers in C++ 
#include <stdio.h>

void addMarks(int *m) 
	*m = *m + 10;
//Driver program 
int main() 
	int marks;
  	printf("Enter actual marks ");
  	scanf("%d", &marks);
  	printf("The final marks is %d", marks);
  	return 0;

The output of the above program is:

Enter actual marks 10
The final marks is 20