#25 C Pointers and Functions | C Programming For Beginners

Programiz
13 Apr 202210:00

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

00:00

📘 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.

05:00

🧮 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

Pointers in C are variables that store the memory address of another variable. In the video, the concept of pointers is central as it explores how to use pointers with functions to manipulate and return values directly in memory. For example, a pointer is passed to a function to modify the value of a variable without returning it explicitly.

💡Function

A function is a block of code designed to perform a specific task. In this video, functions are explained with examples that pass both regular variables and pointer variables as parameters. The video demonstrates how passing pointers to functions allows direct manipulation of memory, as opposed to just passing values.

💡Memory Address

A memory address is the specific location in memory where a variable is stored. The video frequently discusses passing memory addresses to functions using pointers, emphasizing that pointers allow functions to directly access and modify variables stored at those addresses.

💡Pass by Reference

Pass by reference means passing the memory address of a variable to a function, allowing the function to modify the original variable. In C, this is done using pointers. The video provides examples where variables are modified directly through pointers passed to functions, such as changing a number’s value within a function.

💡Dereferencing

Dereferencing a pointer means accessing the value stored at the memory address that the pointer points to. The video explains dereferencing through examples where pointer variables are used inside functions to modify the values of variables by accessing their memory addresses.

💡Return Pointer

Returning a pointer from a function allows the function to return a memory address instead of a value. The video provides examples where functions return pointers to the memory addresses of variables, enabling the caller to directly work with the memory locations of computed results, such as the square of a number.

💡Integer Pointer

An integer pointer (int *) is a pointer variable that stores the memory address of an integer variable. Throughout the video, integer pointers are used in examples where functions take pointers as arguments and return them, manipulating integers stored in memory by using the pointers.

💡Pointer Arithmetic

Pointer arithmetic involves performing operations like addition or multiplication on values pointed to by pointers. The video demonstrates this when computing the square of a number or adding two numbers by dereferencing the pointer to access and manipulate the values stored at specific memory addresses.

💡Main Function

The main function is the entry point for a C program. In the video, it is where variables are initialized and functions are called. The video shows how the main function is used to set up variables and pass their addresses to other functions to manipulate their values using pointers.

💡Print Statement

The print statement in C, typically done using printf(), is used to output values to the console. In the video, printf() is used to demonstrate how the values of variables are printed before and after being manipulated by functions using pointers, showing the effect of passing pointers on the original variables.

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

play00:00

hey guys welcome back to this series on

play00:02

c programming in the last few videos we

play00:04

have been learning about pointers in c

play00:06

we'll continue that in this video as

play00:08

well today we will learn to use pointers

play00:10

and function together this is going to

play00:12

be an interesting video so let's get

play00:14

started

play00:18

firstly let's revise the working of the

play00:21

function with the help of an example

play00:23

here i have created a function that

play00:25

calculates the square of a number the

play00:27

function takes single parameter and it

play00:30

then computes the square of the number

play00:33

and then returns the square here we are

play00:36

passing a regular variable to the

play00:38

function

play00:39

in c we can also pass a pointer variable

play00:41

as an argument to a function let's see

play00:44

an example in which i'll create a

play00:46

function that accepts a pointer as an

play00:48

argument and change the value pointed by

play00:50

the pointer

play00:51

so i'll create a function void

play00:55

find

play00:56

value here this function takes pointer

play01:00

as an argument

play01:03

and

play01:04

inside the function

play01:07

asterisk num is equals to 39.

play01:12

here the non-parameter is a pointer

play01:14

variable and we have changed the value

play01:16

pointed by this pointer now let's create

play01:18

a number variable inside the main so

play01:20

i'll create here int

play01:22

number

play01:23

and i'll assign value 21 to this

play01:26

and i'll call the function by passing

play01:28

the address of the number so i'll call

play01:31

the function find

play01:32

value

play01:34

bracket

play01:36

ampersand

play01:38

number

play01:40

since our function accepts a pointer

play01:42

variable which stores the memory address

play01:44

we are passing the address of the number

play01:46

variable

play01:48

now let me print the value of the number

play01:50

so i'll use print statement

play01:52

printf

play01:54

inside quotation number

play01:58

percent d

play02:00

comma

play02:02

number

play02:05

let's run this code

play02:07

as you can see the value of the number

play02:09

is changed to 39. this happens because

play02:12

when we pass the address of a number

play02:14

variable

play02:15

the pointer variable inside the function

play02:17

is assigned with the address of the

play02:19

number

play02:20

so when we change the value at the

play02:23

memory address the value of the number

play02:25

is also changed

play02:29

now let's see another example in this

play02:32

example we'll find the square of a

play02:34

number like before but this time we'll

play02:36

use a pointer variable instead of a

play02:38

regular variable here i have the code

play02:40

from our earlier program let's remove

play02:42

this function

play02:45

now i'll create a find square function

play02:47

void

play02:48

find square

play02:54

and this function accepts a pointer

play02:55

variable as its parameter

play02:57

so

play02:59

int

play03:01

asterix

play03:02

number

play03:06

inside the function i'll find the square

play03:07

of the number so int

play03:10

square is equals to

play03:13

asterisk number

play03:15

multiplied with

play03:17

number

play03:20

this syntax might look confusing for you

play03:22

here asterix number is the value pointed

play03:25

by the number pointer and we are

play03:27

multiplying the value with itself

play03:30

now the square variable stores the

play03:31

square of the number now let me assign

play03:34

this to the value pointed by the number

play03:36

pointer so

play03:38

asterix number

play03:40

is equals to

play03:42

square

play03:44

now inside the main function i'll change

play03:47

this find value to find square

play03:51

so find square

play03:54

i'll also modify this print statement

play03:56

i'll write here

play03:57

square

play03:58

is now i'll run this code

play04:03

as you can see we have computed the

play04:05

square of the number

play04:08

by the way do you want to take your

play04:09

programming journey to the next level

play04:11

watching tutorials is fine but it has

play04:14

one limitation it's hard to learn and

play04:16

practice together at the same time lucky

play04:18

for you we recently launched programmies

play04:21

pro that provides tutorials along with

play04:23

quizzes and challenges which will help

play04:25

you practice and test your learning in

play04:27

real time also the course includes

play04:29

projects to give you an experience of

play04:31

how programming works in the real world

play04:34

sign up now visit this link which is

play04:36

also available in the video description

play04:38

below

play04:40

just like regular variable we can also

play04:43

return a pointer variable from a

play04:45

function let me show you an example here

play04:48

we have our code from our earlier

play04:50

program now suppose i want to return

play04:52

pointer from the function not just

play04:54

compute the square so first i'll change

play04:57

the return type of the function from

play04:59

void

play05:00

to

play05:01

integer pointer that is int asterisk

play05:06

now inside the function i'll add a

play05:08

return statement that returns a pointer

play05:10

so return

play05:12

number

play05:14

this statement returns the address

play05:16

pointed by number

play05:18

now to store the return value i'll

play05:20

assign this function call to a variable

play05:23

so int asterisk

play05:26

result

play05:27

is equals to this function call

play05:30

since the return value is an address we

play05:33

have created a pointer to store the

play05:35

result now let's use this result

play05:38

variable to print the square value

play05:41

i'll modify this print statement here

play05:43

i'll remove this square is

play05:46

and i'll write here

play05:47

result is

play05:49

percent d

play05:50

and i'll remove this number and i'll

play05:53

write here

play05:54

asterix

play05:55

result

play05:57

now let's run this program

play05:59

as you can see the return address stores

play06:02

the square of the number

play06:04

okay guys we need your support to keep

play06:06

these types of content free for all

play06:08

users youtube really likes engagement on

play06:10

the video so leave a comment below press

play06:12

that like button and hit subscribe if

play06:14

you haven't already let's keep the

play06:16

engagement score higher so that more

play06:18

people can discover and enjoy these

play06:20

courses

play06:22

let's see one more example to make it

play06:24

more clear for you so i'll create a

play06:27

function that adds to number and returns

play06:29

the sum let me go back to my code editor

play06:32

i'll remove these all code from here

play06:36

and i'll remove these statements from

play06:38

here

play06:39

so first i'll create a function that

play06:41

accepts three pointer so i'll create a

play06:44

function here

play06:45

that is int

play06:47

asterisk add

play06:49

number

play06:54

int

play06:55

asterisk

play06:56

num1

play06:57

comma int

play06:59

asterisk

play07:01

num2 comma int

play07:04

asterix sum

play07:06

inside the function we will compute the

play07:09

sum of the number pointed by num1 and

play07:11

num2 and store the result to the address

play07:13

pointed by the sum

play07:15

so

play07:16

we'll do

play07:18

asterix

play07:19

sum is equals to

play07:22

asterisk num1

play07:24

plus

play07:25

asterisk

play07:26

num tube

play07:27

then i'll return the sum

play07:29

so return

play07:31

sum

play07:33

now inside the main function i'll create

play07:35

two variables int number 1

play07:39

and i'll assign value 32

play07:41

and another variable in number 2

play07:45

and i'll assign value 18 to this and a

play07:48

sum variable to store the sum let me

play07:51

call the function by passing the

play07:52

addresses of three variables

play07:54

so i'll call the function add numbers

play08:00

parenthesis ampersand number 1

play08:03

comma

play08:05

ampersand number 2

play08:08

comma

play08:09

ampersand sum

play08:12

since the function returns the address

play08:14

i'll assign this function call to a

play08:15

pointer so int

play08:18

asterisk result is equals to this

play08:22

function

play08:23

finally i'll print this using printf

play08:25

statement

play08:26

so print f bracket inside quotation

play08:30

sum

play08:31

is percent d

play08:34

comma

play08:36

asterisk result

play08:40

now let me run this program

play08:42

as you can see we have computed the sum

play08:45

of two numbers using the function and

play08:48

pointers

play08:51

now to revise what you have learned in

play08:53

this program here is a programming task

play08:55

for you create a program to find the

play08:57

multiplication of two numbers using a

play08:59

function and a pointer here is how your

play09:01

programs would work create a function

play09:03

that assets three pointers inside the

play09:06

function multiply values represented by

play09:08

two pointers and assign the result to

play09:10

the address of the third pointer inside

play09:12

the main function create three variables

play09:15

two variables with the value 13 and nine

play09:17

and third variable to store their

play09:19

product call the function with addresses

play09:21

of the three variables as argument store

play09:24

the return value inside a pointer and

play09:26

print the value pointed by the returned

play09:28

address you will find the answer to this

play09:30

question in our github repository and

play09:32

also if you want to revise this concept

play09:35

all these programs will be there as well

play09:37

i'll put the link in the video

play09:38

description below now that we have

play09:40

reached the end of this video it's time

play09:42

for programming squeeze what type of

play09:44

value does the following function return

play09:47

comment your answer below see you in the

play09:49

next video happy programming

play09:54

[Music]

Rate This

5.0 / 5 (0 votes)

Связанные теги
C programmingpointersfunctionscoding tutorialprogramming basicsC languagecoding examplesfunction pointerslearning Ccoding practice
Вам нужно краткое изложение на английском?