Pointers Inc
Pointers Inc
Pointers Inc
C
Pointer Definition
• Holds a data value that references a unit of
memory storage.
In other words:
• A variable that holds Memory Address
example
1: void main(void)
{
2: int num;
3: int *pNum;
4: num = 57;
5: pNum = #
6: *pNum = 23
7: printf(“%d\n”,
num);
8: return 0;
}
Assigning Addresses to
Pointers
• Pointer to single element
Format: ptr = &element;
• Pointer to first array element
Format: ptr = array;
• Pointer to array element x
Format: ptr = &array[x];
Working with Pointers in
C
int *a, *b; // Declare Pointers a and b
int n=10; // Declare and initialize n
a = &n; // Store the address of n in a
*a = 20; // Change the value of n
// via pointer a
• a = b; // Store the content of
// Pointer a in pointer b
another example
int i = 7; // Declare and initialize i