0% found this document useful (0 votes)
79 views14 pages

Data Structure 1: Pointer, Function, Structure, Array, Recursive Function Call

Pointer, Function, structure, array, recursive function call Pointers allow variables to indirectly reference memory addresses containing values. Pointers contain memory addresses rather than directly containing a value. Pointer variables are declared with a * and can point to data of any type. Pointers can be initialized to 0, NULL, or an address. Functions can be called by reference by passing the address of an argument using the & operator. Pointer arithmetic and operations like incrementing allow pointers to change what memory address they reference. The sizeof operator returns the size in bytes of its operand like a variable, type, or array. Arrays and pointers are closely related as array names act like pointers. Pointer-to-pointers allow pointers that point

Uploaded by

Nahid Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views14 pages

Data Structure 1: Pointer, Function, Structure, Array, Recursive Function Call

Pointer, Function, structure, array, recursive function call Pointers allow variables to indirectly reference memory addresses containing values. Pointers contain memory addresses rather than directly containing a value. Pointer variables are declared with a * and can point to data of any type. Pointers can be initialized to 0, NULL, or an address. Functions can be called by reference by passing the address of an argument using the & operator. Pointer arithmetic and operations like incrementing allow pointers to change what memory address they reference. The sizeof operator returns the size in bytes of its operand like a variable, type, or array. Arrays and pointers are closely related as array names act like pointers. Pointer-to-pointers allow pointers that point

Uploaded by

Nahid Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Data Structure 1

Pointer,
Function, structure, array,
recursive function call
Pointers
Introduction
• Pointers
– Pointers are variables containing memory addresses of a
variable with specific value.
– A variable name directly references a value
– A pointer indirectly reference a value.
– A pointer ‘p’ pointing to a character ‘c’ –
Pointer Variable Declarations and
Initialization
• Pointer variables
– Contain memory address as their value
– Normal variables contain a specific value (direct reference)
– Pointers contain address of a variable that has a specific
value (indirect reference)
– Indirection – referencing a pointer value
• Pointer declarations
– * used with pointer variables: e.g., int *myPtr;
– Declares a pointer to an int (pointer of type int *)
– Multiple pointers require using a * before each variable declaration:
int *myPtr1, *myPtr2;
– Can declare pointers to any data type
– Initialize pointers to 0, NULL, or an address
• 0 or NULL –points to nothing (NULL preferred)
Calling Functions by Reference
• Call by reference with pointer arguments
– Pass address of argument using & operator
– Allows you to change actual location in memory
– Arrays are not passed with & because the array
name is already a pointer.
Pointer Expressions and Pointer
Arithmetic
• Arithmetic operations can be performed on
Pointers
– Increment/decrement pointer (++ or --)
– Add an integer to a pointer( + or += , - or -=)
– Pointers may be subtracted from each other
• 5 element int array on machine with 4 byte int
– vPtr points to first element v[ 0 ]
– at location 3000 (vPtr = 3000)
– vPtr += 2; sets vPtr to 3008
– vPtr points to v[ 2 ] (incremented by 2),
but the machine has 4 byte int, so it points to address 3008
sizeof Operator
• Sizeof
– Returns size of operand in bytes
– For arrays: size of 1 element *number of elements
– sizeof can be used with
• Variable names
• Type name
• Constant values
Relationship Between Pointers and
Arrays
• Arrays and pointers closely related
– Array name like a constant pointer
– Pointers can do array subscripting operations
Pointer-to-pointers
• We can have pointers to – int, char, float, any
structure….
• Declaration of a pointer-to-pointer:
• int
**ipp; where two * indicates two level of
pointers.
• ipp points to ip1 which points to i.
• **ipp is i, or 10.
Pointers to Functions
• Pointer to function
– Contains address of function
– Similar to how array name is address of first element
– Function name is starting address of code that defines
Function
• Function pointers can be
– Passed to functions
– Stored in arrays
– Assigned to other function pointers
Data Structure Books
• Text Books:
• Mark Allen Weiss - Data Structures and Algorithm Analysis in
C, 2nd Edition, Addison-Wesley, 1997
• P.S. Deshpande and O.G. Kakde - C and Data Structures,
Charles Rive Media, 2004
• Reference Books:
• Data structures and problem solving using C++, By Mark Allen
Weiss - Addison-Wesley (2000)

You might also like