Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.
pointers store address of variables or a memory location.
Now if this ptr is sent to a function as an argument then the array val can be accessed in a similar fashion.
pointers store address of variables or a memory location.
// General syntax
datatype *var_name;
// An example pointer "ptr" that holds
// address of an integer variable or holds
// address of a memory whose value(s) can
// be accessed as integer values through "ptr"
int *ptr;
To use pointers in C, we must understand below two operators.
- To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us address of variable x.
- One more operator is unary * (Asterisk) which is used for two things :
- To declare a pointer variable: When a pointer variable is declared in C/C++, there must a * before its name.
- To access the value stored in the address we use the unary operator (*) that returns the value of the variable located at the address specified by its operand.fOutput :
Value of Var = 10 Address of Var = 0x7fffa057dd4 After doing *ptr = 20, *ptr is 20
Below is pictorial representation of above program:
- To declare a pointer variable: When a pointer variable is declared in C/C++, there must a * before its name.
Pointer Expressions and Pointer Arithmetic
A limited set of arithmetic operations can be performed on pointers. A pointer may be:
A limited set of arithmetic operations can be performed on pointers. A pointer may be:
- incremented ( ++ )
- decremented ( — )
- an integer may be added to a pointer ( + or += )
- an integer may be subtracted from a pointer ( – or -= )
Pointer arithmetic is meaningless unless performed on an array.
Note : Pointers contain addresses. Adding two addresses makes no sense, because there is no idea what it would point to. Subtracting two addresses lets you compute the offset between these two addresses.
Note : Pointers contain addresses. Adding two addresses makes no sense, because there is no idea what it would point to. Subtracting two addresses lets you compute the offset between these two addresses.
Output:Value of *ptr = 10 Value of ptr = 0x7ffcae30c710 Value of *ptr = 100 Value of ptr = 0x7ffcae30c714 Value of *ptr = 200 Value of ptr = 0x7ffcae30c718
Array Name as Pointers
An array name acts like a pointer constant. The value of this pointer constant is the address of the first element.
For example, if we have an array named val then val and &val[0] can be used interchangeably.
An array name acts like a pointer constant. The value of this pointer constant is the address of the first element.
For example, if we have an array named val then val and &val[0] can be used interchangeably.
Output: Elements of the array are: 5 10 15
Now if this ptr is sent to a function as an argument then the array val can be accessed in a similar fashion.
Pointers and Multidimensional Arrays
Consider pointer notation for the two-dimensional numeric arrays. consider the following declaration
Consider pointer notation for the two-dimensional numeric arrays. consider the following declaration
int nums[2][3] = { {16, 18, 20}, {25, 26, 27} };
In general, nums[i][j] is equivalent to *(*(nums+i)+j)
POINTER NOTATION | ARRAY NOTATION | VALUE |
---|---|---|
*(*nums) | nums[0][0] | 16 |
*(*nums + 1) | nums[0][1] | 18 |
*(*nums + 2) | nums[0][2] | 20 |
*(*(nums + 1)) | nums[1][0] | 25 |
*(*(nums + 1) + 1) | nums[1][1] | 26 |
*(*(nums + 1) + 2) | nums[1][2] | 27 |
No comments:
Post a Comment