Pssst… we can write an original essay just for you.
Any subject. Any type of essay.
We’ll even meet a 3-hour deadline.
121 writers online
A pointer variable is a variable that keeps addresses of memory locations. Like other data values, memory addresses, or pointer values, can be stored in variables of the appropriate type. A variable that stores an address is called a pointer variable, but is often simply referred to as just a pointer.
The definition of a pointer variable, ptr, must specify the type of data that ptr will point to. Here is an example: int *ptr; The asterisk before the variable name indicates that ptr is a pointer variable, and the int data type indicates that ptr can only be used to point to, or hold addresses of, integer variables. This definition is read as “ptr is a pointer to int.” it is also useful to think of *ptr as the “variable that ptr point to.” with this view, the definition of ptr just given can be read as “as the variable that ptr points to has type int.’ because the asterisk (*) allows you to pass from a pointer to the variable being pointed to, it is called the indirection operator. It is the responsibility of the programmer to keep track of what type of data is stored in each memory location. The data might be a number or some text (which is just a sequence of numbers, of course) or it might be an address of another location or possible an address of an address and so forth.
There are also some high level languages – un typed languages – that operate in the same way; Forth and BCPL are examples that come to mind. The majority of high level languages support data typing to a lesser or greater extent. This means, in effect, that the programmer specifies that a variable contains a specific type of data and the language only allows appropriate operations on that variable. Advantage of using pointers is efficient in handling Arrays and Structures, Pointers allow references to function and it helps in passing of function as arguments to other functions, it reduces length of the program and its execution time as well and it allows C language to support Dynamic Memory management.
I learned earlier in before chapter that an array name, without brackets and a subscript, actually represents the starting address of the array. This means that an array name is pointer. Program next page by showing an array name being used with the indirection operator. Remember, array elements are stored together in memory, as illustrated in picture below. It make sense that if numbers is the address of numbers , values could be added to numbers to get the addresses of the other elements in the array. In other words, if you add one to number, you are actually adding 1 * sizeof (short) to numbers. If you add two to numbers , the result is numbers +2* sizeof (short), and so on. This conversion means that an element in an array can be retrieved by adding its subscript to a pointer to the array. Pointer to pointer:
Pointers are used to keep the address of other variables of similar data type. But if you want to store the address of a pointer variable, then you again need to store it. Thus, when one pointer variable stores the address of another pointer variable, it is known as Pointer to Pointer variable or Double Pointer. Here, we have used two indirection operator(*) which stores and points to the address of a pointer variable i.e, int *. If we want to store the address of this (double pointer) variable p1, then the syntax will become. For example, int ***p2.
We also have array of structure variables. And to use the array of structure variables readily, we use pointers of structure type.. To access members of structure using the structure variable, we used the dot . Operator. But when we have a pointer of structure type, we use arrow -> to access structure members. Pointer to arithmetic: The pointer variables changed with mathematical statements that perform addition or subtraction. This is demonstrated in program next page. The first loop increments the pointer variable, stepping it through each element of the array backwards.
Not all arithmetic operations may be performed on pointers. For example, you cannot use multiplication or divison with pointers the following operations are allowable: the ++ and – operators may be used to increment or decrement a pointer variable. An integer may be added to or substrated from a pointer variable. This may be performed with the + and- operators. And a pointer may be substracted from another pointer. a few examples and understand this more clearly. int*i; i++; increment it, it will increment by 2 bytes because int is also of 2 bytes. float*i; i++; increment it, it will increment by 4 bytes because float datatype is of 4 bytes. double*i; i++; Similarly, in this case, size of pointer is still 2 bytes. But now, when we increment it, it will increment by 8 bytes because its data type is double.
Pointer as a function parameter is used to hold addresses of arguments passed during function call. This is also known as call by reference. When a function is called by reference any change made to the reference variable will effect the original variable. It is possible to declare a pointer pointing to a function which can then be used as an argument in another function. A pointer to a function is declared as follows, type (*pointer-name)(parameter);. Here is an example: int (*sum)(); int *sum();A function pointer can point to a specific function when it is assigned the name of that function. Int sum (int, int); int (*s)(int, int); s=sum;
Here s is a pointer to a function sum. Now sum can be called using function pointer s along with providing the required argument values. s(10,20);. ; increment it, it will increment by 2 bytes because int is also of 2 bytes. i i++; pointer to integer: It is commonly to store the value of a pointer (i.e. an address) in an “ordinary” variable – like an unsigned integer. An example of where this might be done in fixed is in device. Here is an example: unsigned normal; unsigned *pointer; pointer = &normal; normal = (unsigned)pointer; This would result in the variable normal containing its own address.
We provide you with original essay samples, perfect formatting and styling
To export a reference to this article please select a referencing style below:
Sorry, copying is not allowed on our website. If you’d like this or any other sample, we’ll happily email it to you.
Attention! this essay is not unique. You can get 100% plagiarism FREE essay in 30sec
Sorry, we cannot unicalize this essay. You can order Unique paper and our professionals Rewrite it for you
Your essay sample has been sent.
Want us to write one just for you? We can custom edit this essay into an original, 100% plagiarism free essay.Order now
Are you interested in getting a customized paper?Check it out!