int k; // variable type declaration
k = 2; // assignment statement
// 2 is rvalue (value in the right hand side of "=")
// k is lvalue (value in the left hand side of "=")
int *ptr; // pointer to store integer values
ptr = &k; // address of k
// *ptr is rvalue
// ptr is lvalue
printf("value stored in k is %d\n", *ptr);
// k is name of variable; k is an unmodifiable lvalue
// ptr is name of address; ptr is a modifiable lvalue
// value stored in k is 2
/* The size of memory required to store an address depends on the system. Some computers might require special handling to hold a segment and offset under certain circumstances. The actual size required is not too important so long as we have a way of informing the compiler that what we want to store is an address. Such a variable is called a pointer variable.
1) To store a variable, we need to know how many bits/bytes would be required to hold the value.
2) To store address of a variable, we need not know the number of bits/bytes would be required to hold the address. It is machine dependent.
3) Type declaration for a pointer indicates what type of variable will be stored in that address.
/* Pointers vs Arrays
// In C a string is an array of characters terminated with a binary zero character
(written as ).
my_string = ‘T’;
my_string = ‘e’;
my_string = ‘d’:
my_string = ‘‘;
// C permits
my_string = “Ted”;
/* In the above example, my_string has 37 null characters in it, since the size of the array is 40 which is a constant. Here the lvalue is unmodifiable.
This can be avoided if a pointer is used instead. Here the lvalue is modifiable.
// Pointers to structures
typedef struct student
//a STUDENT variable
//a pointer to a STUDENT variable
//assign some values
mary.age = 14;
mary.studentNumber = 5678;
mary.averageGrade = 73;
//tell pupil to point at mary
pupil = &mary;
//Notice in the examples below the use of -> instead of (*pupil).age
//we can now access members of the mary structure using the pupil pointer
printf("Mary's student number is %d\n", pupil->studentNumber);
//we can also do assignment and all other normal variable operations
pupil->averageGrade = 86.2;
printf("Mary's average grade is %f\n", mary.averageGrade);