Video Link: https://youtu.be/JYHpD9huNR4
Tutorial Link: https://www.programiz.com/c-programming/c-pointer-functions
#include <stdio.h>
void findValue(int* num) {
*num = 39;
}
int main() {
int number = 21;
findValue(&number);
printf("Number: %d", number);
return 0;
}
Output
Number: 39
#include <stdio.h>
void findSquare(int* number) {
int square = *number * *number;
*number = square;
}
int main() {
int number = 21;
findSquare(&number);
printf("Square is %d", number);
return 0;
}
Output
Square is 441
#include <stdio.h>
int* findSquare(int* number) {
int square = *number * *number;
*number = square;
return number;
}
int main() {
int number = 21;
int* result = findSquare(&number);
printf("Result is %d", *result);
return 0;
}
Output
Result is 441
#include <stdio.h>
int* addNumbers(int* num1, int* num2, int* sum) {
*sum = *num1 + *num2;
return sum;
}
int main() {
int number1 = 32;
int number2 = 18;
int sum;
int* result = addNumbers(&number1, &number2, &sum);
printf("Sum is %d", *result);
return 0;
}
Output
Sum is 50
Create a program to find the multiplication of two numbers using a function and pointers.
Here's how your program should work:
-
Create a function that accepts three pointers.
-
Inside the function multiply values represented by two pointers and assign the result to the address of the third pointer.
-
Inside the main function, create three variables, two variables with values 13 and 9 and the third variable to store their product.
-
Call the function with addresses of the 3 variables as arguments.
-
Store the returned value inside a pointer and print the value pointed by the returned address.
Solution
#include <stdio.h>
int* multiplication (int* a, int* b, int* c) {
*c = *a * *b;
return c;
}
int main() {
int num1 = 13;
int num2 = 9;
int multiple;
int* result = multiplication(&num1, &num2, &multiple);
printf("The multiplication is: %d", *result);
return 0;
}
Output
The multiplication is: 117
Q. What type of value does the following function return?
Options:
- Integer value
- Address of integer variable
- Double value
- Address of double variable
Answer: 4