Declaring a Pointer
Declaring A Pointer |
- Pointing variables, must be declared before they may be used in a C program.
- When a pointer variable is declared, the variable name must be preceded by asterisk(*).
- The data type that appears in the declaration refers to the data item that is stored in the address represented by the pointer.
- Pointer variables can point to numeric or character variable, arrays, functions, other pointer variables, or null (zero) value.
Usage:
data-type *pointer_variable;
- data-type refers to the data type of the pointer's object.
- *pointer_variable is the name of the pointer variable.
- Within a variable declaration, a pointer variable can be initialized by assigning it the address of another variable.
Program-1
Difference between ordinary arithmetic expression & Declaring a Pointer
#include <stdio.h>
/*Difference between ordinary arithmetic expression & Declaring a Pointer. */
void main()
{ int num1, num2;
int value=3;
int *value_pointer; /*value_pointer points to value */
num1 =2 * (value+5); /* arithmetic expression */
value_pointer =&value;
num2 = 2 * (*value_pointer+5);
printf("\nNum1 = %d Num2 = %d", num1, num2);
}
Run
Num 1 = 16 Num2 = 16
Explanation:
There are 2 integer expressions in the above program. The first, 2 * (value + 5), is an ordinary arithmetic expression, while the second, 2 * (*value_pointer + 5), involves the use of pointers. Still, the expressions are equal to each other because value and *value_pointer each represent the same integer value.
Program-2
Assigning a value to a variable
#include <stdio.h>
/*Assigning a value to a variable */
void main()
{ int value=3;
int *value_pointer;
value_pointer =&value;
printf("\n *Value_pointer = %d Value = %d", *value_pointer, value);
*value_pointer = 7;
printf("\n *Value_pointer = %d Value = %d", *value_pointer, value);
}
Run
*Value_pointer = 3 Value = 3
*Value_pointer = 3 Value = 3
Explanation:
- Assign an initial value of 3 to the integer variable value
- Assign the address of value to the pointer variable value_pointer
The expression *value_pointer represents the value 3
- The first printf statement displays the current values of *value_pointer and value
- The value of *value_pointer is reset to 7
So, value will be reassigned the value 7
- The second printf statement displays the new values of *value_pointer and value.
Exercise regularly. Eat a nutritious diet. Don't smoke - WHO (Covid-19).
See also...
- Learn how to write the C program step-by-step | Lesson -1
- Learn how to write the C program step-by-step | Lesson -2
- Learn how to write the C program step-by-step | Lesson -3
- Learn how to write the C program step-by-step | Lesson -4
- Learn how to write the C program step-by-step | Lesson -5
- Learn how to write the C program step-by-step | Lesson -6
- Learn how to write the C program step-by-step | Lesson -7
Let's see also...
- Revolution in Information Technology
- Important Terminology of Computer Science
- Most useful website list for job seekers
- Do you know how to delete Facebook account
- What is TCP/IP Reference Model
No comments:
For More Details Subscribe & Comment..!