

Pointers in C
Presentation
•
Computers
•
12th Grade
•
Easy
Used 66+ times
FREE Resource
12 Slides • 8 Questions
1
Pointers in C

2
Lesson Objectives:
Define the term pointer in computing
Create pointer variables for 4 C data types
Assign addresses to a pointer variable
Output data using pointers
Output a memory address using pointers
3
What is a pointer?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.
Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −
type *var-name;
4
Why use a Pointer?
As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory.
Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers.
5
Declaring Pointers
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
6
Examples of Pointer declarations:
int *ip; //pointer to an integer
double *dp; //pointer to an integer
float *fp; //pointer to an integer
char *ch; //pointer to an integer
7
Multiple Choice
Which of the following is NOT a pointer variable declaration?
int *number;
char *name[20];
float height[];
double *dp;
8
Declaring Pointers Cont'd
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
9
Poll
Pointers must have the same data type as the variable the variable in the memory address they are referencing
True
False
10
How to use Pointers?
There are a few important operations, which we will do with the help of pointers very frequently.
(a) We define a pointer variable,
(b) assign the address of a variable to a pointer and
(c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
11
Using pointers
The following example makes use of these operations −
12
When the previous code is compiled and executed, it produces the following result −
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
13
Multiple Choice
Which is an example of data stored in a pointer?
10011001
54
intpointer
b587a63d
14
Multiple Choice
What must be used to print the ADDRESS of a variable?
*
&
||
^
15
Multiple Choice
What must be used to access a VALUE from a pointer?
*
&
||
^
16
NULL Pointers
It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.
17
NULL Pointer
The NULL pointer is a constant with a value of zero.
When the above code is compiled and executed, it produces the following result −
The value of ptr is 0
18
Open Ended
Write code to declare a pointer variable called dpointer of type double.
19
Open Ended
Assign the memory address of the variable height to dpointer.
20
Open Ended
Write code to output the value stored in height using the pointer variable.
Pointers in C

Show answer
Auto Play
Slide 1 / 20
SLIDE
Similar Resources on Wayground
16 questions
Widget
Lesson
•
12th Grade
15 questions
CAI 711 - Week 4 P1
Lesson
•
12th Grade
18 questions
JANJANG
Lesson
•
12th Grade
14 questions
Aprendiendo programación con Coded Arena
Lesson
•
12th Grade
16 questions
Relasi Database
Lesson
•
12th Grade
16 questions
Complex Numbers (Operations)
Lesson
•
11th - 12th Grade
17 questions
Food Chain/Food Web Recap
Lesson
•
12th Grade
16 questions
Review Text Bahasa Inggris Peminatan
Lesson
•
12th Grade
Popular Resources on Wayground
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
14 questions
Boundaries & Healthy Relationships
Lesson
•
6th - 8th Grade
13 questions
SMS Cafeteria Expectations Quiz
Quiz
•
6th - 8th Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
12 questions
SMS Restroom Expectations Quiz
Quiz
•
6th - 8th Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
10 questions
Pi Day Trivia!
Quiz
•
6th - 9th Grade
Discover more resources for Computers
15 questions
Pi Day Trivia
Quiz
•
9th - 12th Grade
10 questions
Understanding Pi and Its Applications
Interactive video
•
7th - 12th Grade
22 questions
El Imperfecto
Quiz
•
9th - 12th Grade
20 questions
Grammar
Quiz
•
9th - 12th Grade
7 questions
History of St. Patrick's Day for Kids | Bedtime History
Interactive video
•
1st - 12th Grade
22 questions
Regular Preterite -AR-ER-IR-
Quiz
•
12th Grade
27 questions
quiz review Senderos 2 En el consultorio
Quiz
•
9th - 12th Grade
25 questions
PI Day Trivia Contest
Quiz
•
9th - 12th Grade