100% found this document useful (1 vote)
184 views21 pages

Pointers: Anush Kuchipudi Sanjeev Kumar Gupta

This document discusses pointers in C programming. It defines pointers as variables that contain memory addresses and notes that pointers allow indirect referencing of values. It provides examples of pointer declarations and discusses how pointers can reference array elements and functions. The document also covers pointers to two-dimensional arrays, pointers to pointers, and enumerations.

Uploaded by

Anush Kuchipudi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
184 views21 pages

Pointers: Anush Kuchipudi Sanjeev Kumar Gupta

This document discusses pointers in C programming. It defines pointers as variables that contain memory addresses and notes that pointers allow indirect referencing of values. It provides examples of pointer declarations and discusses how pointers can reference array elements and functions. The document also covers pointers to two-dimensional arrays, pointers to pointers, and enumerations.

Uploaded by

Anush Kuchipudi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

POINTERS

prepared by
Anush Kuchipudi
Sanjeev Kumar Gupta
Content
1. Pointers

2. Pointer and Array

3. Pointer and 2-Dimensional array

4. Pointer to functions

5. Pointer to pointer

6. Enumerations
POINTER
 Pointers are variables that contain memory addresses as their values.

 A variable name directly references a value.

 A pointer indirectly references a value. Referencing a value through a


pointer is called indirection.

 A pointer variable must be declared before it can be used.


CONCEPT OF ADDRESS AND POINTERS
 Memory can be
ADDR1 Content 1
conceptualized as a linear set Content 2
ADDR2
of data locations. Content 3
ADDR3
 Variables reference the Content 4
contents of a locations Content 5
 Pointers have a value of the Content 6
address of a given location Content 7
Content 8
Content 9
Content 10
Content 11
ADDR11
DECLARATION OF POINTERS
 Examples of pointer declarations:

FILE *fptr;
int *a;
float *b;
char *c;

 The asterisk, when used as above in the declaration, tells


the compiler that the variable is to be a pointer, and the
type of data that the pointer points to, but NOT the name of
the variable pointed to.
POINTER AND ARRAY
The compiler allocates a base address and sufficient
amount of storage.
The base address is the location of the first element.
The array declared as:
static int x[5]={1,2,3,4,5};
Elements x[0] x[1] x[2] x[3] x[4]
Value 1 2 3 4 5
Address 1000 1002 1004 1006 1008
POINTER AND ARRAY
OUTPUT
POINTERS AND 2-DIMENSIONAL
ARRAYS
The C language embodies an unusual but powerful capability it
can treat parts of arrays as arrays.

Each row of a two-dimensional array can be thought of as a


one-dimensional array.

This is a very important fact if we wish to access array


elements of a two-dimensional array using pointers.
OUTPUT
POINTERS AND 2-DIMENSIONAL
ARRAYS
1234 56 1221 33 1434 80 1312 78

-30 -28 -26 -24 -22 -20 -18 -14


POINTERS TO FUNCTIONS
Useful technique is the ability to have pointers to
functions.

int func(int a, float b);


int *func(int a, float b);
int (*func)(int a, float b);

Once you've got the pointer, you can assign the address of
the right sort of function just by using its name like an array
(*func)(1,2); /* or */ func(1,2);
POINTER TO POINTER

Pointer is a variable, which contains address of variable.

Variable itself could be another variable.

A pointer contains another pointers address.


OUTPUT
POINTER TO POINTER
ALLOCATION

3 -12 -14

-12 -14 -16


ENUMERATION
 Enumeration is a user-defined data type. It is defined using the
keyword enum and the syntax is:
enum tag_name {name_0, …, name_n} ;

 The tag_name is not used directly. The names in the braces are
symbolic constants that take on integer values from zero through n.
As an example, the statement:
enum colors { red, yellow, green } ;
 Creates three constants. red is assigned the value 0, yellow is assigned
1 and green is assigned 2.
OUTPUT

You might also like