-
Essay / Pointer Function in C Programming
The pointer is one of the functions in C programming. The main function of the pointer is to act as an address referent of a variable. The pointer obtains the value of a variable by referring to its address. The relationship between pointer and C programming is similar to that of index and book, the book contains everything except that the index can indicate the item we are looking for by referring to the page number. Say no to plagiarism. Get a tailor-made essay on “Why Violent Video Games Should Not Be Banned”? Get the original essayThe pointer also acts as a simple and precise tunnel for a program to allocate or reference memory in a memory location. It acts as a personal secretary of a piece of data or a variable because it has the memory location and value of the data. However, it can also be used as a flag to display the value of the variable, without affecting the variable itself. An example program and output will be provided in the following section, further explanation will be provided. To declare a pointer, the “*” sign or an asterisk is used in the pointer variable declaration. An example of a single-line declaration of a pointer variable is shown below. Example 1: int* a; //Pointer declarationNote: Since the pointer variable is address based, the pointer variable type is integer. Next, "&" or the ampersand sign is used to assign the address location of a variable; Example 2: int b = 5; int * a = &b; // pointer a is a reference to the address. The lines above show that the address of the value of b, which is 5, has been assigned to the pointer variable a. Now pointer a is able to retrieve the memory location and value of b for the user. However, to get the value of b, dereferencing technique is used in the program. To dereference a variable, which gets the variable value from the held pointer, "*" or the asterisk sign is used again in the line. However, the context of using the asterisk sign in dereferencing is slightly different from that of declaring the pointer. Example 3: int b=5,c ; // we use the new variable c to dereference the pointer int*a=&b;c=*a; // c contains the value of b, which is 5Now the variable c contains the value of b using the dereference method. Example program#includeint pointer (int a) //pointer declaration function{ int*b = &a;printf("The value of x is %dn",*b);printf("The address of x is % dn",b);printf("The address of the pointer is %d",&b);return 0; }void main(){int x =6; //main variable, which will be explained pointer(x); }In this program consists of applying a pointer which applies address reference, dereference method and isolates the main variable from the pointer, so that the main variable is not affected by the external environment. The program flow is as follows: Function "pointer" is declared, the purpose of this function is used to declare the pointer variable. In the main function, the main variable x is declared, the value 6 is assigned to it. The pointer function is called, the value of x is passed to it. The function argument 'a'.The pointer variable b is declared and a memory location is assigned to it from the local variable a. There are three printf functions, which point to the value of the main variable x, (dereference), the memory location of x and location of the address of the pointer itself respectively. Therefore, the output of the program appears like this: Output: The value of x is 6 The address of x is 6422256 The address of the pointer is 6422236..