Sei sulla pagina 1di 4

03 Mart 2013 Pazar 14:21

C Array of String In this section, you will learn how to create an array of string using pointers in C. The declaration of an array of character pointers is an extremely useful extension to single string pointer declarations. In the example below, A two element array of character pointers where each element is a pointer to a character. We have initialized the array with two elements by giving index numbers. In C, format specifier %s is used with the printf to print the string. Here is the code: ARRAYOF.C #include <stdio.h> #include <conio.h> void main() { clrscr(); char *array[2]; array[0]="Hello"; array[1]="World"; printf("The Array of String is = %s,%s\n", array[0], array[1]); getch(); }

C array of pointers In this section, you will learn how to create array of pointers. A pointer is a variable that contains the memory location of another variable. The values you assign to the pointers are memory addresses of other variables ( or other pointers). A running program gets a certain space in the main memory. Syntax of declaring a pointer: data_type_name * variable name First of all, specify the data type of data stored in the location, which is to identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Then specify the name of variable. You can see in the given example, we have created an array of pointers of maximum size 3. Then we have assigned the objects of array pointer. The address of the variable. array[0] = &x; array[1] = &y; array[2] = &z; Here is the code: ARRAYPOI.C #include <stdio.h> #include <conio.h>
-1-

03 Mart 2013 Pazar 14:21

main() { clrscr(); int *array[3]; int x = 10, y = 20, z = 30; int i; array[0] = &x; array[1] = &y; array[2] = &z; for (i=0; i< 3; i++) { printf("The value of %d= %d ,address is %u\t \n", i, *(array[i]), array[i]); } getch(); return 0; }

C Structure Pointer This section illustrates you the concept of Structure Pointer in C. You can see in the given example, we want to access the employee's information through structure pointer. For that, we declare the structure type st consisting of members id, name and address. Then we have declared a structure pointer as: struct st employee, *stptr; We point the pointer to employee with the expression stptr = &employee. Then access the given members by de-referencing the pointer. For this, we have used another structure selection operator which works on pointers to structures. If p is a pointer to a structure and m is a member of that structure, then p->m. We set the values of specified members as: stptr->id = 1; stptr->name = "Angelina"; stptr->address ="Rohini,Delhi"; Here is the code: STPOINTE.C #include <stdio.h> #include <conio.h> int main() { struct st { int id; char *name; char *address;
-2-

03 Mart 2013 Pazar 14:21

}; struct st employee, *stptr; stptr = &employee; stptr->id = 1; stptr->name = "Angelina"; stptr->address ="Rohini,Delhi"; printf("Employee Information: id=%d\n%s\n%s\n", stptr->id, stptr->name, stptr->address); getch(); return 0; }

C Pointer to a function In this section, you will learn how to use 'Pointer' function in C.

C provides a special feature of pointer to a function. As you know that every function defined in C language have a base address attached to it. This base address acts as an entering point into that function. This address can be stored in a pointer known as function pointer. The pointer to a function is the declaration of pointer that holds the base address of the function. The declaration of a pointer to a function is: Syntax : return_type (* pointer_name) ( variable1_type variable1_name , variable2_type variable2_name , variable3_type variable3_name .................); You can see in the given example, we have create a function mul to find the product of three numbers. Then we have declared the function pointer for storing the base address of function mul in the following way: int (*function_pointer) (int,int,int); Here is the code: POINTERF.C #include <stdio.h> #include <conio.h> int mul(int a, int b, int c) { return a*b*c; } void main() { int (*function_pointer)(int, int, int); function_pointer = mul; printf("The product of three numbers is:%d", function_pointer(2, 3, 4));
-3-

03 Mart 2013 Pazar 14:21

getch(); }

C Multiple Indirection This section illustrates you the concept of Multiple Indirection. C permits the pointer to point to another pointer. This creates many layers of pointer and therefore called as multiple indirection. A pointer to a pointer has declaration is similar to that of a normal pointer but have more asterisk sign before them. This indicates the depth of the pointer. You can see in the given example, we have declared an integer variable i initialized to 100. The statement int ** pt creates a pointer which points to pointer to a variable with int value. We have passed the value of i to pointer pt2 and to pass the value of pt2 to pt1, we have used the statement pt1 = &pt2; which shows the Multiple Indirection. Here is the code: MULTINDI.C #include <stdio.h> #include <conio.h> void main() { int i =100; int **pt1; int *pt2; clrscr(); pt2 = &i; pt1 = &pt2; printf("The value of **pt1 = %d \n "+ "The value of *pt2 = %d", **pt1, *pt2); getch(); }

-4-

Potrebbero piacerti anche