Search Header Logo
Pointers in C

Pointers in C

Assessment

Presentation

Computers

12th Grade

Easy

Used 66+ times

FREE Resource

12 Slides • 8 Questions

1

Pointers in C

Slide image

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?

1

int *number;

2

char *name[20];

3

float height[];

4

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 −

Slide image

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?

1

10011001

2

54

3

intpointer

4

b587a63d

14

Multiple Choice

What must be used to print the ADDRESS of a variable?

1

*

2

&

3

||

4

^

15

Multiple Choice

What must be used to access a VALUE from a pointer?

1

*

2

&

3

||

4

^

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


Slide image

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

Slide image

Show answer

Auto Play

Slide 1 / 20

SLIDE