#25 C Pointers and Functions | C Programming For Beginners
Summary
TLDRIn this video, the instructor explores the use of pointers with functions in C programming. The tutorial covers passing pointer variables as function arguments, modifying values at memory addresses, and returning pointer values from functions. Examples include finding the square of a number and summing two integers using pointers. Additionally, viewers are assigned a task to create a multiplication function using pointers. The video emphasizes hands-on learning, offering resources like quizzes, challenges, and a GitHub repository for further practice. Engagement is encouraged through likes, comments, and subscriptions.
Takeaways
- 📌 Pointers in C can be used to pass arguments to functions, allowing the function to modify the value of a variable in the calling scope.
- 🔢 A function can accept a pointer as an argument and change the value it points to, demonstrating how pointers can affect data outside their scope.
- 💡 The script provides an example of a function that calculates the square of a number using a pointer to modify the original variable's value.
- 👨🏫 The tutorial explains how to use pointers to find the square of a number, emphasizing the use of pointer syntax within functions.
- 🎯 It showcases a function that returns a pointer to an integer, illustrating how functions can return addresses in addition to values.
- 🛠️ The script includes a practical example where a function adds two numbers using pointers and returns the sum through a pointer.
- 📚 The video encourages viewers to practice programming with quizzes and challenges through the introduction of a learning platform called 'Programmies Pro'.
- 🔄 The tutorial demonstrates the concept of returning a pointer from a function and how to use the returned pointer to access the result.
- 📈 The script motivates engagement by asking viewers to like, comment, and subscribe to support free content creation.
- 📝 A programming task is provided at the end of the script to reinforce learning, involving creating a function that multiplies two numbers using pointers.
- 🎉 The video concludes with a question to engage viewers, asking them to comment with the return type of a certain function.
Q & A
What is the purpose of using pointers with functions in C programming?
-Pointers allow functions to directly modify the values of variables by passing their memory addresses, enabling more efficient memory use and manipulation of data.
In the example where a pointer is passed to a function, how is the value of a variable changed?
-The address of the variable is passed to the function, and inside the function, the value at the memory address is changed using the pointer. This results in the original variable's value being modified outside the function.
What does the `find_square` function do in the video example?
-The `find_square` function calculates the square of a number by accepting a pointer to an integer, multiplying the value pointed by the pointer with itself, and then updating the original value with the square.
How can a function return a pointer in C?
-A function can return a pointer by changing its return type to a pointer type (e.g., `int*` for an integer pointer) and using the `return` statement to return the pointer. The returned pointer can then be used to access or modify the data stored at that memory address.
Why is it necessary to use an asterisk (*) before a pointer variable inside a function?
-The asterisk (*) dereferences the pointer, which means it accesses the value stored at the memory address the pointer points to. Without dereferencing, the function would only access the memory address, not the actual data.
What is the difference between passing a regular variable and a pointer variable to a function in C?
-When passing a regular variable, the function receives a copy of the variable's value, so changes made inside the function don't affect the original variable. When passing a pointer, the function receives the memory address, allowing it to modify the original variable directly.
In the last example, how does the function add two numbers using pointers?
-The function `add_number` accepts three pointers: one for each number and one for storing the sum. It computes the sum by dereferencing the first two pointers, adds their values, and stores the result in the location pointed by the third pointer.
How is the result of a function that returns a pointer printed in the video example?
-The result is printed by dereferencing the pointer returned by the function, using an asterisk (*) before the pointer variable in the `printf` statement.
What are the advantages of using pointers when returning values from functions?
-Using pointers to return values allows functions to return more than one value and modify variables directly, enabling efficient memory use and bypassing the need to return copies of large data structures.
What task is given to viewers at the end of the video?
-Viewers are asked to create a program that multiplies two numbers using a function and pointers. The function should accept three pointers, multiply the values pointed to by the first two, and store the result in the location pointed to by the third pointer.
Outlines
📘 Introduction to Pointers and Functions in C
The video continues a series on C programming, specifically focusing on pointers and their use with functions. It begins with a review of a function that calculates the square of a number by passing a regular variable. Then, the concept of passing a pointer to a function is introduced. An example function `find_value` is shown, which accepts a pointer, changes the value of the variable it points to, and the effect of this change is demonstrated by updating the value of a variable in the `main` function.
🧮 Computing Square Using Pointers
In this section, the script discusses another example, where the square of a number is computed using a pointer instead of a regular variable. The function `find_square` accepts a pointer as a parameter and modifies the value stored at the memory address of the number. The multiplication is performed using the dereferencing operator (`*`), and the square is assigned back to the value pointed by the pointer. The example demonstrates how pointers can be used to manipulate data efficiently within functions.
📈 Returning a Pointer from a Function
Here, the concept of returning a pointer from a function is explored. The `find_square` function is modified to return the pointer to the variable that stores the square of a number. The script explains how to handle this return value by storing it in a pointer variable and then using it to print the squared result. This illustrates how functions in C can return memory addresses and how to work with them in the `main` function.
➕ Adding Two Numbers Using Pointers
This section introduces an example of a function that adds two numbers using pointers and returns the sum. The function `add_numbers` takes three pointers as arguments: two for the numbers to be added and one to store the result. Inside the function, the values pointed to by the first two pointers are added, and the result is stored in the memory address of the third pointer. The returned pointer holds the address of the sum, which is then printed in the `main` function.
📝 Programming Task: Multiplying Numbers with Pointers
In this final segment, the viewer is given a programming task to practice using pointers with functions. The task involves creating a function that multiplies two numbers using three pointer arguments: two for the input numbers and one to store the product. The program should call the function with the addresses of three variables and print the result. A GitHub repository is mentioned as a resource for the solution, and the video ends with a quiz to reinforce the lesson.
Mindmap
Keywords
💡Pointers
💡Function
💡Memory Address
💡Pass by Reference
💡Dereferencing
💡Return Pointer
💡Integer Pointer
💡Pointer Arithmetic
💡Main Function
💡Print Statement
Highlights
Introduction to using pointers and functions together in C programming.
Explanation of a function that calculates the square of a number using regular variables.
Introduction to passing a pointer as an argument to a function in C.
Example of modifying a variable's value using a pointer inside a function.
Demonstration of how passing a pointer to a function can change the original variable's value.
Example of computing the square of a number using pointers instead of regular variables.
Explanation of using pointer dereferencing to perform operations on the value stored at the pointer’s address.
Example of returning a pointer from a function and how to store it in a pointer variable.
Explanation of modifying the print statement to show the result from a pointer variable.
Demonstration of how a function can return a pointer and how to work with it in the main function.
Example of a function that computes the sum of two numbers using pointers.
Introduction to writing a function that accepts multiple pointers to perform operations.
Explanation of passing addresses to a function and how pointer arithmetic is used within it.
Programming task for viewers: Multiply two numbers using a function and pointers.
Promotion of 'Programmy Pro' for more interactive learning, quizzes, and real-world projects in programming.
Transcripts
hey guys welcome back to this series on
c programming in the last few videos we
have been learning about pointers in c
we'll continue that in this video as
well today we will learn to use pointers
and function together this is going to
be an interesting video so let's get
started
firstly let's revise the working of the
function with the help of an example
here i have created a function that
calculates the square of a number the
function takes single parameter and it
then computes the square of the number
and then returns the square here we are
passing a regular variable to the
function
in c we can also pass a pointer variable
as an argument to a function let's see
an example in which i'll create a
function that accepts a pointer as an
argument and change the value pointed by
the pointer
so i'll create a function void
find
value here this function takes pointer
as an argument
and
inside the function
asterisk num is equals to 39.
here the non-parameter is a pointer
variable and we have changed the value
pointed by this pointer now let's create
a number variable inside the main so
i'll create here int
number
and i'll assign value 21 to this
and i'll call the function by passing
the address of the number so i'll call
the function find
value
bracket
ampersand
number
since our function accepts a pointer
variable which stores the memory address
we are passing the address of the number
variable
now let me print the value of the number
so i'll use print statement
printf
inside quotation number
percent d
comma
number
let's run this code
as you can see the value of the number
is changed to 39. this happens because
when we pass the address of a number
variable
the pointer variable inside the function
is assigned with the address of the
number
so when we change the value at the
memory address the value of the number
is also changed
now let's see another example in this
example we'll find the square of a
number like before but this time we'll
use a pointer variable instead of a
regular variable here i have the code
from our earlier program let's remove
this function
now i'll create a find square function
void
find square
and this function accepts a pointer
variable as its parameter
so
int
asterix
number
inside the function i'll find the square
of the number so int
square is equals to
asterisk number
multiplied with
number
this syntax might look confusing for you
here asterix number is the value pointed
by the number pointer and we are
multiplying the value with itself
now the square variable stores the
square of the number now let me assign
this to the value pointed by the number
pointer so
asterix number
is equals to
square
now inside the main function i'll change
this find value to find square
so find square
i'll also modify this print statement
i'll write here
square
is now i'll run this code
as you can see we have computed the
square of the number
by the way do you want to take your
programming journey to the next level
watching tutorials is fine but it has
one limitation it's hard to learn and
practice together at the same time lucky
for you we recently launched programmies
pro that provides tutorials along with
quizzes and challenges which will help
you practice and test your learning in
real time also the course includes
projects to give you an experience of
how programming works in the real world
sign up now visit this link which is
also available in the video description
below
just like regular variable we can also
return a pointer variable from a
function let me show you an example here
we have our code from our earlier
program now suppose i want to return
pointer from the function not just
compute the square so first i'll change
the return type of the function from
void
to
integer pointer that is int asterisk
now inside the function i'll add a
return statement that returns a pointer
so return
number
this statement returns the address
pointed by number
now to store the return value i'll
assign this function call to a variable
so int asterisk
result
is equals to this function call
since the return value is an address we
have created a pointer to store the
result now let's use this result
variable to print the square value
i'll modify this print statement here
i'll remove this square is
and i'll write here
result is
percent d
and i'll remove this number and i'll
write here
asterix
result
now let's run this program
as you can see the return address stores
the square of the number
okay guys we need your support to keep
these types of content free for all
users youtube really likes engagement on
the video so leave a comment below press
that like button and hit subscribe if
you haven't already let's keep the
engagement score higher so that more
people can discover and enjoy these
courses
let's see one more example to make it
more clear for you so i'll create a
function that adds to number and returns
the sum let me go back to my code editor
i'll remove these all code from here
and i'll remove these statements from
here
so first i'll create a function that
accepts three pointer so i'll create a
function here
that is int
asterisk add
number
int
asterisk
num1
comma int
asterisk
num2 comma int
asterix sum
inside the function we will compute the
sum of the number pointed by num1 and
num2 and store the result to the address
pointed by the sum
so
we'll do
asterix
sum is equals to
asterisk num1
plus
asterisk
num tube
then i'll return the sum
so return
sum
now inside the main function i'll create
two variables int number 1
and i'll assign value 32
and another variable in number 2
and i'll assign value 18 to this and a
sum variable to store the sum let me
call the function by passing the
addresses of three variables
so i'll call the function add numbers
parenthesis ampersand number 1
comma
ampersand number 2
comma
ampersand sum
since the function returns the address
i'll assign this function call to a
pointer so int
asterisk result is equals to this
function
finally i'll print this using printf
statement
so print f bracket inside quotation
sum
is percent d
comma
asterisk result
now let me run this program
as you can see we have computed the sum
of two numbers using the function and
pointers
now to revise what you have learned in
this program here is a programming task
for you create a program to find the
multiplication of two numbers using a
function and a pointer here is how your
programs would work create a function
that assets 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 the value 13 and nine
and third variable to store their
product call the function with addresses
of the three variables as argument store
the return value inside a pointer and
print the value pointed by the returned
address you will find the answer to this
question in our github repository and
also if you want to revise this concept
all these programs will be there as well
i'll put the link in the video
description below now that we have
reached the end of this video it's time
for programming squeeze what type of
value does the following function return
comment your answer below see you in the
next video happy programming
[Music]
تصفح المزيد من مقاطع الفيديو ذات الصلة
C_74 Pointers in C- part 4 | Pointer to Pointer (Double Pointer)
you will never ask about pointers again after watching this video
C_73 Pointers in C- part 3 | Pointer Assignment
C_75 Pointers in C-part 5 | Pointer Arithmetic (Addition) with program
Understanding and Using Function Pointers in C
C_71 Pointers in C - part 1| Introduction to pointers in C | C Programming Tutorials
5.0 / 5 (0 votes)