The indirection operator can be used in a pointer to a
pointer to an integer, a single-dimensional array of pointers to integers, a
pointer to a char, and a pointer to an unknown type.
The indirection operator is also known as the
“ dereference operator ” .
The indirection operator is also known as the
“ dereference operator ” .
The following C statements illustrate the usage of the
indirection operator:
int a = 1, b; // line 1
int *ptr = &a; // line 2
b = *ptr; // line 3
*) In the line1 , a and b are integer variables and a is assigned a
value of 1.
*) In line 2, the address of a is stored in the integer pointer ptr
(line 2).
*) The indirection operator is used in line 3 to assign the value
the address pointed to by ptr to the integer variable b.
int a = 1, b; // line 1
int *ptr = &a; // line 2
b = *ptr; // line 3
*) In the line1 , a and b are integer variables and a is assigned a
value of 1.
*) In line 2, the address of a is stored in the integer pointer ptr
(line 2).
*) The indirection operator is used in line 3 to assign the value
the address pointed to by ptr to the integer variable b.
void main()
{
int n=5;
int *pn=&n;
int **ppn=&pn;
printf("Value of n = %u",n);
printf("Indirect value = %u",*pn);
printf(“Double indirect value = %u",**ppn);
printf(“Address of n = %u",pn);
printf(“Address of n via indirection = %u",*ppn);
getch();
}
{
int n=5;
int *pn=&n;
int **ppn=&pn;
printf("Value of n = %u",n);
printf("Indirect value = %u",*pn);
printf(“Double indirect value = %u",**ppn);
printf(“Address of n = %u",pn);
printf(“Address of n via indirection = %u",*ppn);
getch();
}
void main()
{
int a=1, b;
int *ptr=&a;
b=*ptr;
printf("Value in a = %u ",a);
printf("Value in b = %u ",b);
printf("Value in ptr = %u ",ptr);
printf("Value in *ptr = %u ",*ptr);
getch();
}
{
int a=1, b;
int *ptr=&a;
b=*ptr;
printf("Value in a = %u ",a);
printf("Value in b = %u ",b);
printf("Value in ptr = %u ",ptr);
printf("Value in *ptr = %u ",*ptr);
getch();
}
Unformatted data files in c
Some applications involve the use of data
files to store block of data, where each block consists of a fixed number
of contiguous bytes.
Each block will generally represent a complex data structure, such as a structure or an array.
For example, a data file may consist of multiple structures having the same composition, or it may contain multiple arrays of the same type and size.
For such applicators it may be desirable to read the entire block from the data, or write the entire block to the data file, rather than reading or writing the individual components (i.e., structure members of array elements) within each block separately.
Each block will generally represent a complex data structure, such as a structure or an array.
For example, a data file may consist of multiple structures having the same composition, or it may contain multiple arrays of the same type and size.
For such applicators it may be desirable to read the entire block from the data, or write the entire block to the data file, rather than reading or writing the individual components (i.e., structure members of array elements) within each block separately.
The library functions fread and fwrite are
intended to be used in situations of this type.
There functions are often referred to as unformatted read and write functions. Similarly, data files of this type are often referred to as unformatted data file.
Each of these functions requires four arguments: a pointer to the data block, the size of the data block, the number of data blocks being transferred, and the stream pointer.
Thus, a typical fwrite function might be written as:
fwrite(&customer, sizeof(record), 1, fpt);
Where customer is a structure variable of type record, and fpt is the stream pointer associated with a data file that has been opened for output.
There functions are often referred to as unformatted read and write functions. Similarly, data files of this type are often referred to as unformatted data file.
Each of these functions requires four arguments: a pointer to the data block, the size of the data block, the number of data blocks being transferred, and the stream pointer.
Thus, a typical fwrite function might be written as:
fwrite(&customer, sizeof(record), 1, fpt);
Where customer is a structure variable of type record, and fpt is the stream pointer associated with a data file that has been opened for output.
No comments:
Post a Comment