Several declarations involving pointers are shown below.The individual declarations range from simple to complex.
int *p; /*p is a pointer to an integer quantity*/
int *p[10]; /*p is a 10-element array of pointers of integer quantities*/
int (*p)[10]; /*p is a pointer to a 10-element integer array */
int *p(void); /*p is a function that returns a pointer to an integer quantity*/
int p(char *a); /*p is a function that accepts an argument which is a pointer to a character and returns an integer quantity*/
int *p(char *a); /*p is a function that accepts an argument which is a pointer to a character and returns a pointer to an integer quantity*/
int (*p)(char *a); /*p is a pointer to a function that accepts an argument which is a pointer to a character and returns a pointer to an integer quantity*/
int (*p(char *a))[10]; /*p is a function that accepts an argument which is a pointer to a character and returns a pointer to a 10-element integer quantity*/
int p(char (*a)[]); /*p is a function that accepts an argument which is a pointer to a character array and returns an integer quantity*/
int p(char *a[]); /*p is a function that accepts an argument which is an array of pointers to characters and returns an integer quantity*/
int *p(char a[]); /*p is a function that accepts an argument which is an array of characters and returns a pointer to an integer quantity*/
int *p(char (*a)[]); /*p is function that accepts an argument which is a pointer to a character array and returns a pointer to an integer quantity*/
int *p(char *a[]); /*p is a function that accepts an argument which is an array of pointers to characters and returns a pointer to an integer quantity*/
int (*p)(char (*a)[]); /*p is a pointer to a function that accepts an argument which is is a pointer to a character array and returns an integer quantity*/
int *(*p)(char (*a)[]); /*p is a pointer to a function that accepts an argument which is a pointer to a character array and returns a pointer to an integer quantity*/
int *(*p)(char *a[]); /*p is a pointer to a function that accepts an argument which is an array of pointers to characters and returns a pointer to an integer quantity*/
int (*p[10])(void); /*p is a 10-element array of pointers to functions;each function returns an integer quantity*/
int (*p[10])(char a); /*p is a 10-element array of pointers to functions;each function accepts an argument which is a character, and returns an integer quantity*/
int *(*p[10])(char a); /*p is a 10-element array of pointers to functions;each function accepts an argument which is a character, and returns a pointer to an integer quantity*/
int *(*p[10])(char *a); /*p is a 10-element array of pointers to functions;each function accepts an argument which is a pointer to a character, and returns a pointer to an integer quantity*/
Showing posts with label 8. POINTERS. Show all posts
Showing posts with label 8. POINTERS. Show all posts
Saturday, January 2, 2010
Friday, January 1, 2010
8.9 PASSING FUNCTIONS TO OTHER FUNCTIONS
A pointer to a function can be passed to another function as argument.This allows transferring of one function to another, as if the first function were a variable.let us refer the first function as guest function, and the second function as host function.
The formal argument declaration of the guest function can be written as
data-type (*function-name) ( )
where data-type refers to the data type of the quantity returned by the guest and function-name is the name of the guest. The formal argument declaration can also be written as
data-type (*function-name) ( type 1, type 2,..... )
or as
data-type (*function-name) ( type 1 arg 1, type 2 arg 2,..... )
where type 1, type 2,..... refers to the data types of the arguments associated with the guest; arg 1, arg 2,....... refers to the names of the arguments associated with the guest.
The host function declaration can now be written as
funct-data-type funct-name
(arg-data-type (*pt-var) ( type 1 arg 1, type 2 arg 2,..... ),
|<--- pointer to guest function ----------->|
data types and names of other funct args);
where funct-data-type refers to the data type of the quantity returned by the host function; funct-name refers to the name of host function; arg-data-type refers to the data type of the quantity returned by the guest function; pt-var refers to the pointer variable pointing to the guest function, and
type 1 arg 1, type 2 arg 2,..... refer to the data types and the corresponding names of the guest function's arguments.
The skeletal outline of a C program is shown below.This program consists of four functions: main, process, funct1 and funct2.Note that process is host function for funct1 and funct2. Each of the three subordinate functions returns an integer quantity.
int process(int (*)(int, int)); /*function declaration (host)*/
int funct1(int, int); /*function declaration (guest)*/
int funct2(int, int); /*function declaration (guest)*/
main()
{
int i, j;
...................
i = process(funct1); /*pass funct1 to process; return a value for i*/
...................
j = process(funct2); /*pass funct2 to process; return a value for j*/
...................
}
process(int (*pf)(int, int)) /*host function definition*/
/*(formal argument is a pointer to a function */
{
int a, b, c;
...................
c = (*pf)(a, b); /*access the function passed to this function; return a value for c */
...................
return(c);
}
funct1(a, b) /*guest function definition*/
int a, b;
{
int c;
c=......... /*use a and b to evaluate c*/
return(c);
}
funct2(x, y) /*guest function definition*/
int x, y;
{
int z;
z=......... /*use x and y to evaluate z*/
return(z);
}
The formal argument declaration of the guest function can be written as
data-type (*function-name) ( )
where data-type refers to the data type of the quantity returned by the guest and function-name is the name of the guest. The formal argument declaration can also be written as
data-type (*function-name) ( type 1, type 2,..... )
or as
data-type (*function-name) ( type 1 arg 1, type 2 arg 2,..... )
where type 1, type 2,..... refers to the data types of the arguments associated with the guest; arg 1, arg 2,....... refers to the names of the arguments associated with the guest.
The host function declaration can now be written as
funct-data-type funct-name
(arg-data-type (*pt-var) ( type 1 arg 1, type 2 arg 2,..... ),
|<--- pointer to guest function ----------->|
data types and names of other funct args);
where funct-data-type refers to the data type of the quantity returned by the host function; funct-name refers to the name of host function; arg-data-type refers to the data type of the quantity returned by the guest function; pt-var refers to the pointer variable pointing to the guest function, and
type 1 arg 1, type 2 arg 2,..... refer to the data types and the corresponding names of the guest function's arguments.
The skeletal outline of a C program is shown below.This program consists of four functions: main, process, funct1 and funct2.Note that process is host function for funct1 and funct2. Each of the three subordinate functions returns an integer quantity.
int process(int (*)(int, int)); /*function declaration (host)*/
int funct1(int, int); /*function declaration (guest)*/
int funct2(int, int); /*function declaration (guest)*/
main()
{
int i, j;
...................
i = process(funct1); /*pass funct1 to process; return a value for i*/
...................
j = process(funct2); /*pass funct2 to process; return a value for j*/
...................
}
process(int (*pf)(int, int)) /*host function definition*/
/*(formal argument is a pointer to a function */
{
int a, b, c;
...................
c = (*pf)(a, b); /*access the function passed to this function; return a value for c */
...................
return(c);
}
funct1(a, b) /*guest function definition*/
int a, b;
{
int c;
c=......... /*use a and b to evaluate c*/
return(c);
}
funct2(x, y) /*guest function definition*/
int x, y;
{
int z;
z=......... /*use x and y to evaluate z*/
return(z);
}
8.8 ARRAYS OF POINTERS
A multidimensional array can be expressed in terms of an array of pointers rather than a pointer to a group of contiguous arrays, .in such situations the newly defined array will have one less dimension than the original multidimensional array. Each pointer will indicate the begining of a seperate (n-1) dimensional array.
In general terms, a two-dimensional array can be defined as a one-dimensional array of pointers by writing
data-type *array[expression 1];
rather than a conventional array definition,
data-type array[expression 1][expression 2];
Similarly, an n-dimensional array can be defined as an (n-1) dimensional array of pointers by writing
data-type *array[expression 1][expression 2]......[expression n-1];
rather than
data-type array[expression 1][expression 2]......[expression n];
In these declarations data-type refers to the data type of the original one-dimensional,array is the array name,expression 1,expression 2,......,expression n are positive-valued integer expressions that indicate the maximum number of elements associated with each subscript.
You can notice that array name and its preceding asterisk are not enclosed in parentheses in this type of declaration.
Let us now approach the problem of entering a list of strings into the computer and rearranging them into alphabetical order using a one-dimensional array of pointers, where each pointer indicates the
begining of a string.
/*sort a list of strings into alphabetical otder using an array of pointers*/
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
void reorder(int n, char *x[]);
main()
{
int i, n=0;
char *x[10];
printf("Enter each string on a seperate line below\n\n");
printf("Type \'END\' when finished\n\n");
/*read in the list of strings*/
do {
/*allocate memory*/
x[n] = (char *)malloc(12 *sizeof(char));
printf("string %d", n+1);
scanf("%s", x[n]);
}
}
In general terms, a two-dimensional array can be defined as a one-dimensional array of pointers by writing
data-type *array[expression 1];
rather than a conventional array definition,
data-type array[expression 1][expression 2];
Similarly, an n-dimensional array can be defined as an (n-1) dimensional array of pointers by writing
data-type *array[expression 1][expression 2]......[expression n-1];
rather than
data-type array[expression 1][expression 2]......[expression n];
In these declarations data-type refers to the data type of the original one-dimensional,array is the array name,expression 1,expression 2,......,expression n are positive-valued integer expressions that indicate the maximum number of elements associated with each subscript.
You can notice that array name and its preceding asterisk are not enclosed in parentheses in this type of declaration.
Let us now approach the problem of entering a list of strings into the computer and rearranging them into alphabetical order using a one-dimensional array of pointers, where each pointer indicates the
begining of a string.
/*sort a list of strings into alphabetical otder using an array of pointers*/
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
void reorder(int n, char *x[]);
main()
{
int i, n=0;
char *x[10];
printf("Enter each string on a seperate line below\n\n");
printf("Type \'END\' when finished\n\n");
/*read in the list of strings*/
do {
/*allocate memory*/
x[n] = (char *)malloc(12 *sizeof(char));
printf("string %d", n+1);
scanf("%s", x[n]);
}
while(strcmp(x[n++], "END"));
/*reorder the list of strings*/
reorder(--n, x);
/*display the reordered list of strings*/
printf("\n\nReordered list of strings:\n");
for (i=0; i < n;++i)
printf("\nstring %d: %s", i+1, x[i]);
}
}
void reorder(int n, char *x[]) /*rearrange the list of strings*/
{
{
char *temp;
int i, item;
for (item=0; item < n-1; ++item)
/*find the lowest of all remaining strings*/
for (i= item+1; i < n; ++i)
if (strcmp(x[item], x[i] ) > 0) {
/*interchange the two strings*/
temp = x[item];
x[item] = x[i];
x[i] = temp;
}
return;
}
Thursday, December 31, 2009
8.7 POINTERS AND MULTIDIMENSIONAL ARAYS
It is also possible to represent multidimensional array in terms of a pointer as with the case of one dimensional array.A two-dimensional array is actually a collection of one-dimensional array.Therefore, a two-dimensional array can be defined as a pointer to a group of contiguous one dimensional arrays.Thus, a two-dimensional array declaration can be written as
data-type (*ptvar)[expression 2];
rather than
data-type array[expression 1][expression 2];
This concept can be generalized to higher dimensional arrays; that is,
data-type (*ptvar)[expression 2][expression 3].........[expression n];
replaces
data-type array[expression 1][expression 2]...........[expression n];
In the declaration data-type refers to the data type of the array, ptvar is the name of the pointer variable, array is the corresponding array name, and expression 1, expression 2,...........,expression n are positive-valued integer expression that indicate the maximum number of array elements associated with each subscript.
Suppose x is a two-dimensional integer array having 10 rows and 20 columns.We can declare x as
int (*x)[20];
rather than
int x[10][20];
In the first declaration, x is defined to be a pointer to a group of contiguous, one-dimensional, 20-element integer arrays.Thus, x points to the first 20-element array, which is actually the first row (i.e. row 0) of the original two-dimensional array. Similarly, (x+1)point to second 20-element array, which is the second row (row 1) of the two-dimensional array, and so on.
Now consider a three-dimensional floating point array t.This array can be defined as
float (*t)[20][30];
rather than
float[10][20][30];
data-type (*ptvar)[expression 2];
rather than
data-type array[expression 1][expression 2];
This concept can be generalized to higher dimensional arrays; that is,
data-type (*ptvar)[expression 2][expression 3].........[expression n];
replaces
data-type array[expression 1][expression 2]...........[expression n];
In the declaration data-type refers to the data type of the array, ptvar is the name of the pointer variable, array is the corresponding array name, and expression 1, expression 2,...........,expression n are positive-valued integer expression that indicate the maximum number of array elements associated with each subscript.
Suppose x is a two-dimensional integer array having 10 rows and 20 columns.We can declare x as
int (*x)[20];
rather than
int x[10][20];
In the first declaration, x is defined to be a pointer to a group of contiguous, one-dimensional, 20-element integer arrays.Thus, x points to the first 20-element array, which is actually the first row (i.e. row 0) of the original two-dimensional array. Similarly, (x+1)point to second 20-element array, which is the second row (row 1) of the two-dimensional array, and so on.
Now consider a three-dimensional floating point array t.This array can be defined as
float (*t)[20][30];
rather than
float[10][20][30];
8.6 OPERATIONS ON POINTERS
In section 8.4 we have seen that an integer value added to an array name in order to access an individual array element is interpreted as an array subscript;it points to the location of the array element relative to the first element in the array.
In the program shown below, the first and last elements of an integer array are pointed by two different pointer variables.
Execution of the program results in the following output
The first line indicates that addresses of a[0] and a[5] are 52 and 5C respectively.The difference between these two hexadecimal numbers is 10 (converted to decimal).
The permissible operations on pointers are summarized below:
1. The address of an ordinary variable can be assigned to a pointer variable (e.g, pv=&v).
2. The value of a pointer variable can be assigned to another pointer vatiable (e.g., pv=px) provided both pointers point to objects of the same data type.
3. A null (zero) value can be assigned to a pointer variable (e.g., pv=NULL, NULL--->symbolic constant that represent the value 0).
4. An integer quantity can be added or subtracted from a pointer variable (e.g., pv+3, ++pv etc.).
5. One pointer variable can be subtracted from another provided both pointers point to the elements of the same array.
6. Two pointer variables pointing to objects of the same data type can be compared.
Other arithmetic operations on pointers are not allowed.Thus, a pointer variable cannot be multiplied by a constant;two pointer variables cannot be added and so on.
In the program shown below, the first and last elements of an integer array are pointed by two different pointer variables.
#include < stdio.h >
main()
{
int *px, *py; /*integer pointers*/
static int a[6] = {1, 2, 3, 4, 5, 6};
px = &a[0];
py = &a[5];
printf("px=%X py=%X", px, py);
printf("\n\npy - px=%X ", py-px);
}
Execution of the program results in the following output
px=52 py=5C
py - px=5
The first line indicates that addresses of a[0] and a[5] are 52 and 5C respectively.The difference between these two hexadecimal numbers is 10 (converted to decimal).
The permissible operations on pointers are summarized below:
1. The address of an ordinary variable can be assigned to a pointer variable (e.g, pv=&v).
2. The value of a pointer variable can be assigned to another pointer vatiable (e.g., pv=px) provided both pointers point to objects of the same data type.
3. A null (zero) value can be assigned to a pointer variable (e.g., pv=NULL, NULL--->symbolic constant that represent the value 0).
4. An integer quantity can be added or subtracted from a pointer variable (e.g., pv+3, ++pv etc.).
5. One pointer variable can be subtracted from another provided both pointers point to the elements of the same array.
6. Two pointer variables pointing to objects of the same data type can be compared.
Other arithmetic operations on pointers are not allowed.Thus, a pointer variable cannot be multiplied by a constant;two pointer variables cannot be added and so on.
Wednesday, December 30, 2009
8.5 DYNAMIC MEMORY ALLOCATION
Since an array name is actually a pointer to the first element within the array, it should be possible to define the array as a pointer variable rather than as a conventional array.For conventional array definition, a fixed block of memory is reserved at the beginning of program execution, but this does not occur in case of array represented in terms of pointer variable.Therefore, in order to represent an array, the use of a pointer variable requires some type of initial memory assignment before the array elements are processed.This is known as dynamic memory allocation. Generally, the malloc library function is used for this purpose.
Let us write a C program to illustrate the use of dynamic memory allocation.
/*reorder a one-dimensional, integer array from smallest to largest, using pointer notation*/
#include < stdio.h >
#include < stdlib.h >
void reorder (int n, int *x);
main()
{
int i, n, *x;
/*read in a value for n*/
printf("How many numbers will be entered? ");
scanf("%d", &n);
printf("\n");
/*allocate memory*/
x = (int *) malloc(n * sizeof(int));
/*read in the list of numbers*/
for (i =0;i < n;++i) {
printf("i = %d x = ", i+1);
scanf("%d", x+i);
}
/*reorder all array elements*/
reorder(n, x);
/*display the reordered list of numbers*/
printf("\n\nReordered list of numbers: \n\n");
for (i =0;i < n;++i)
printf("i = %d x = %d\n", i+1, *(x+i));
}
void reorder (int n, int *x) /*rearrange the list of numbers*/
{
int i, item, temp;
for (item=0; item < n-1; ++item)
/*find the smallest of all remaining elements*/
for (i = item+1; i < n; ++i)
if (*(x+i) < *(x+item)) {
/*interchange two elements*/
temp = *(x+ item);
*(x+ item) = *(x+ i);
*(x+ i) = temp;
}
return;
}
In this program, the integer array is defined as a pointer to a integer. The malloc library function is used to assign memory to the pointer variable.
We can also see that the scanf function specifies the address of ith element as x+i rather than x[i].
Within the function reorder,we can see that the second formal argument is now defined as a pointer variable rather than an integer array.In the if statement, we can see that x[i] is written as *(x+i), and x[item] as *(x+ item)
An advantage of dynamic memory allocation is that it is able to reserve as much as memory required during program execution, and then release the memory when it is no longer needed.
Let us write a C program to illustrate the use of dynamic memory allocation.
/*reorder a one-dimensional, integer array from smallest to largest, using pointer notation*/
#include < stdio.h >
#include < stdlib.h >
void reorder (int n, int *x);
main()
{
int i, n, *x;
/*read in a value for n*/
printf("How many numbers will be entered? ");
scanf("%d", &n);
printf("\n");
/*allocate memory*/
x = (int *) malloc(n * sizeof(int));
/*read in the list of numbers*/
for (i =0;i < n;++i) {
printf("i = %d x = ", i+1);
scanf("%d", x+i);
}
/*reorder all array elements*/
reorder(n, x);
/*display the reordered list of numbers*/
printf("\n\nReordered list of numbers: \n\n");
for (i =0;i < n;++i)
printf("i = %d x = %d\n", i+1, *(x+i));
}
void reorder (int n, int *x) /*rearrange the list of numbers*/
{
int i, item, temp;
for (item=0; item < n-1; ++item)
/*find the smallest of all remaining elements*/
for (i = item+1; i < n; ++i)
if (*(x+i) < *(x+item)) {
/*interchange two elements*/
temp = *(x+ item);
*(x+ item) = *(x+ i);
*(x+ i) = temp;
}
return;
}
In this program, the integer array is defined as a pointer to a integer. The malloc library function is used to assign memory to the pointer variable.
We can also see that the scanf function specifies the address of ith element as x+i rather than x[i].
Within the function reorder,we can see that the second formal argument is now defined as a pointer variable rather than an integer array.In the if statement, we can see that x[i] is written as *(x+i), and x[item] as *(x+ item)
An advantage of dynamic memory allocation is that it is able to reserve as much as memory required during program execution, and then release the memory when it is no longer needed.
Tuesday, December 29, 2009
8.4 POINTERS AND ONE DIMENSIONAL ARRAYS
An array name is really a pointer to the first element in the array. Therefore, if x is one dimensional array, then the address of the first array element can be expressed as either &x[0] or simply as x. Moreover, the address of second array element can be written as either &x[1] or as (x+1), and so on.
Since &x[i] and (x+i) both represent the address of the ith element of x, it would seem reasonable that x[i] and *(x+i) both represent the content of the address i.e., the value of the ith element of x.
Here is a simple C program that illustrates the relationship between array elements and their addresses.
Execution of the program results in the following output.
Since &x[i] and (x+i) both represent the address of the ith element of x, it would seem reasonable that x[i] and *(x+i) both represent the content of the address i.e., the value of the ith element of x.
Here is a simple C program that illustrates the relationship between array elements and their addresses.
#include < stdio.h >
main()
{
static int x[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
int i;
for (i = 0;i <= 9; ++i) {
/*display an array element*/
printf("\ni= %d x[i]= %d *(x+i)= %d", i, x[i], *(x+i));
/*display the corresponding array address*/
printf(" &x[i]= %X x+i= %X", &x[i], (x+i));
}
}
Execution of the program results in the following output.
i=0 x[i]=10 *(x+i)=10 &x[i]=72 x+i =72
i=1 x[i]=11 *(x+i)=10 &x[i]=74 x+i =74
i=2 x[i]=12 *(x+i)=10 &x[i]=76 x+i =76
i=3 x[i]=13 *(x+i)=10 &x[i]=78 x+i =78
i=4 x[i]=14 *(x+i)=10 &x[i]=7A x+i =7A
i=5 x[i]=15 *(x+i)=10 &x[i]=7C x+i =7C
i=6 x[i]=16 *(x+i)=10 &x[i]=7E x+i =7E
i=7 x[i]=17 *(x+i)=10 &x[i]=80 x+i =80
i=8 x[i]=18 *(x+i)=10 &x[i]=82 x+i =82
i=9 x[i]=19 *(x+i)=10 &x[i]=84 x+i =84
8.3 PASSING POINTERS TO A FUNCTION
Pointers are often passed to a function as arguments.This allows data items within the calling portion of the program to be accessed by the function, altered within the function, and then returned to the calling portion of the program in the altered form.We refer to this use of pointers as passing arguments by reference (or by address or by location).
While using pointers as arguments to a function, some care is required with the formal argument declarations within the function. Specifically, formal pointer arguments that must be preceded by asterisk.Function prototypes are written in the same manner. If a function declaration does not include variable names, the data type of each pointer argument must be followed by an asterisk.
The difference between ordinary arguments, which are passed by value, and pointer arguments, which are passed by reference is illustrated with the help of the following C program.
After execution of the program, the following output is generated;
While using pointers as arguments to a function, some care is required with the formal argument declarations within the function. Specifically, formal pointer arguments that must be preceded by asterisk.Function prototypes are written in the same manner. If a function declaration does not include variable names, the data type of each pointer argument must be followed by an asterisk.
The difference between ordinary arguments, which are passed by value, and pointer arguments, which are passed by reference is illustrated with the help of the following C program.
#include < stdio.h >
void funct1 (int u, int v); /*function prototype*/
void funct2 (int *pu, int *pv); /*function prototype*/
main()
{
int u = 1, v = 3;
printf("\nBefore calling funct1: u=%d v=%d", u, v);
funct1(u, v);
printf ("\nAfter calling funct1: u=%d v=%d", u, v);
printf ("\n\nBefore calling funct2: u=%d v=%d", u, v);
funct2(&u, &v);
printf ("\nAfter calling funct2: u=%d v=%d", u, v);
}
void funct1 (int u, int v)
{
u = 0;
v = 0;
printf ("\nwithin funct1: u=%d v=%d", u, v);
return;
}
void funct2 (int *pu, int *pv);
{
*pu = 0;
*pv = 0;
printf ("\nwithin funct2: *pu=%d *pv=%d", *pu, *pv);
return;
}
After execution of the program, the following output is generated;
Before calling funct1: u=1 v=3
within funct1: u=0 v=0
After calling funct1: u=1 v=3
Before calling funct2: u=1 v=3
within funct2: *pu=0 *pv=0
After calling funct2: u=0 v=0
Notice the values of u and v are unchanged within main after the call to funct1, though the values of these variables are changed within main after the call of funct2.
Monday, December 28, 2009
8.2 POINTER DECLARATIONS
Pointer variables, like all other variables, must be declared before they may be used in a C program. The interpretation of a pointer declaration differs, however, from the interpretation of other variable declarations. When a pointer variable is declared, the variable name must be preceded by an asterisk (*). The data type that appears in the declaration refers to the object of the pointer,i.e., the data item that is stored in the address represented by the pointer, rather than the pointer itself.
Thus, a pointer declaration may be written in general terms as
data-type *ptvar;
where ptvar is the name of the pointer variable, and data-type refers to the data type of the pointer's object. Remember that an asterisk must precede ptvar.
A C program contains the following declarations.
float u, v;
float *pv;
The first line declares u and v to be floating-point variables. The second line declares pv to be a pointer variable whose object is a floating-point quantity;i.e., pv points to floating-point quantity. Note that pv represents an address, not a floating-point quantity.
Thus, a pointer declaration may be written in general terms as
data-type *ptvar;
where ptvar is the name of the pointer variable, and data-type refers to the data type of the pointer's object. Remember that an asterisk must precede ptvar.
A C program contains the following declarations.
float u, v;
float *pv;
The first line declares u and v to be floating-point variables. The second line declares pv to be a pointer variable whose object is a floating-point quantity;i.e., pv points to floating-point quantity. Note that pv represents an address, not a floating-point quantity.
8.1 FUNDAMENTALS
A pointer is a variable that represents the location (rather than the value) of a data item, such as a variable or an array element.
Within the computer's memory, every stored data item occupies one or more contiguous memory cells (i.e. adjacent words or bytes). The number of memory cells required to store a data item depends on the type of data item.For example, a single character will typically be stored in one byte (8 bits) of memory; an integer usually requires two contiguous bytes; a floating-point number may require four contiguous bytes; and a double precision quantity may require eight contiguous bytes.
Suppose v is a variable that represents some particular data item.The compiler will automatically assign memory cells for this data item.The data item can then be accessed if we know the location (i.e. the address) of the first memory cell. The address of v's memory location can be determined by the expression &v, where & is a unary operator, called the address operator, that evaluates the address of its operand.
Now let us assign the address of v to another variable, pv. Thus,
pv = &v
This new variable is called a pointer to v, since it "points" to the location where v is stored in memory.Thus, pv is referred to as a pointer variable.
The data item represented by v (i.e. , the data item stored in v's memory cells) can be accessed by the expression *pv, where * is unary operator, called the indirection operator, that operates only on a pointer variable. Therefore, *pv and v both represent the same data item (i.e, the contents of the memory cells). Furthermore, if we write pv = &v and u = *pv, then u and v will both represent the same value; i.e., the value of v will indirectly be assigned to u.
Consider the simple C program shown below.
This program involves the use of two integer expressions. The first, 2 * (v+5), is an ordinary arithmetic expression whereas the second, 2* (*pv+5), involves the use of a pointer. The expressions are equivalent, since v and *pv each represent the same integer value.
The following output is generated when the program is executed.
Within the computer's memory, every stored data item occupies one or more contiguous memory cells (i.e. adjacent words or bytes). The number of memory cells required to store a data item depends on the type of data item.For example, a single character will typically be stored in one byte (8 bits) of memory; an integer usually requires two contiguous bytes; a floating-point number may require four contiguous bytes; and a double precision quantity may require eight contiguous bytes.
Suppose v is a variable that represents some particular data item.The compiler will automatically assign memory cells for this data item.The data item can then be accessed if we know the location (i.e. the address) of the first memory cell. The address of v's memory location can be determined by the expression &v, where & is a unary operator, called the address operator, that evaluates the address of its operand.
Now let us assign the address of v to another variable, pv. Thus,
pv = &v
This new variable is called a pointer to v, since it "points" to the location where v is stored in memory.Thus, pv is referred to as a pointer variable.
The data item represented by v (i.e. , the data item stored in v's memory cells) can be accessed by the expression *pv, where * is unary operator, called the indirection operator, that operates only on a pointer variable. Therefore, *pv and v both represent the same data item (i.e, the contents of the memory cells). Furthermore, if we write pv = &v and u = *pv, then u and v will both represent the same value; i.e., the value of v will indirectly be assigned to u.
Consider the simple C program shown below.
#include < stdio.h >
main ()
{
int u1, u2;
int v = 3;
int *pv; /* pv pointer to v*/
u1= 2* (v+5); /*ordinary expression*/
pv = &v;
u2= 2* (*pv +5); /*equivalent expression*/
printf("\nu1=%d u2=%d", u1, u2);
}
This program involves the use of two integer expressions. The first, 2 * (v+5), is an ordinary arithmetic expression whereas the second, 2* (*pv+5), involves the use of a pointer. The expressions are equivalent, since v and *pv each represent the same integer value.
The following output is generated when the program is executed.
u1=16 u2=16
Subscribe to:
Posts (Atom)