

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
15 questions
Computational Thinking Algorithms and Programming
Presentation
•
12th Grade
17 questions
Punctuation
Presentation
•
12th Grade
17 questions
Adjectives/ Verbs
Presentation
•
12th Grade
17 questions
ACT Math Prep
Presentation
•
12th Grade
13 questions
K.2 Pause for People
Presentation
•
KG
18 questions
Relative Pronouns
Presentation
•
12th Grade
15 questions
Software Class
Presentation
•
University
15 questions
Conditionals
Presentation
•
12th Grade
Popular Resources on Wayground
10 questions
GPA Lesson
Presentation
•
9th - 12th Grade
7 questions
Albert Einstein
Quiz
•
3rd Grade
31 questions
Bridge A Review
Quiz
•
3rd Grade
6 questions
Blue Sue and Red Ruth
Quiz
•
3rd Grade
8 questions
(Day12 HW) Inverse Trig Ratios
Quiz
•
9th Grade
20 questions
Summer Geometry QUIZ (Week3)
Quiz
•
9th Grade
16 questions
Theme Practice
Quiz
•
7th Grade
20 questions
Taxes
Quiz
•
9th - 12th Grade