Programming and Computer Application Practices: "Pointer"
Programming and Computer Application Practices: "Pointer"
“POINTER”
By:
Wildan Aghniya
17063048
ELECTRICAL EDUCATION
FACULTY OF ENGINEERING
2019
Purpose
Short theory
Pointer variables are often said to be variables that point to other objects. In reality, a
pointer variable contains the address of another object (i.e. an object that is said to be pointed
by a pointer).
with types can be any type that has been discussed in the previous chapters, as well as
the following chapters. The variable_name is the name of the pointer variable.
In C, you can create a special variable that stores the address (rather than the value).
This variable is called pointer variable or simply a pointer.
In order for a pointer to point to another variable, the pointer must first be filled with
the address of the variable to be designated. To state the address of a variable, the operator &
(address operator, unary) can be used, by placing it in front of the variable name.
If a variable has been designated by a pointer, the variable pointed to by the pointer
can be accessed through the variable itself (direct access) or through a pointer (indirect
access). Indirect access is done by using an indirection operator (indirect) in the form of a
symbol * (unary).
Access and change the contents of a Pointer Variable
As in the basic concept, pointers can be used to access another variable through its
address, it can take, read / access and change the value of a variable. To be able to access the
value on a variable can use the * (star) before the variable name, so that it indicates that the
pointer variable will read the value of the designated address.
pointer to array
In most contexts, array names "decays" to pointers. In simple words, array names
are converted to pointers. That's the reason why you can use pointer with the same name as
array to manipulate elements of the array. However, you should remember that pointers and
arrays are not same.
pointer to string
The variable name of the string str holds the address of the first element of the array
i.e., it points at the starting memory address.
So, we can create a character pointer ptr and store the address of the string strvariable
in it. This way, ptr will point at the string str. In the following code we are assigning the
address of the string str to the pointer ptr.
Array of Pointer
An array can be used to store a number of pointers. The pointer array can be
initialized when declared.
Pointer to Pointer
Pointer in function
Pointers and their relation to functions that will be discussed below include:
Pointers as function parameters.
Pointers as output functions.
A function can be made so that the output is a pointer. For example, a function
produces an output in the form of a pointer that points to the month__name string,
Work Steps
Result
1.
Analysis
The program above, the ouput show us value from Z is 50 and S is 30.
In C language, #include can be used to impor functions that has been define from
header file.
Printf() function used to display an output on the screen or monitor. The control string
like:
- %d to display integers.
- %f to display floating-point number.
- %c to display character.
- %s to to display string.
\n is using to the new character will be in the new line from the program.
Conclusion
The pointer variable type is a type of variable that contains the address of the actual
variable.
The pointer variable type must be the same as the designated variable type.
The relationship between pointers and arrays at C is very close, because actually
arrays will be translated internally in pointer form
Pointer variables can be strings, arrays or other types of variables.
A pointer can point to another pointer (pointer to pointer)
Pointer variables can be used as parameters in a function, as can also be used as a
return value of a function.
References
Jobsheet.
https://www.programiz.com/c-programming/c-pointers-arrays.
https://www.dyclassroom.com/c/c-pointers-and-strings.