Cursor in C
Pointer in the C language is easy to learn. Some tasks in C language are made easier by pointers, and other tasks become more flexible, such as in memory allocation, which cannot be performed without using a cursor. Therefore it is necessary to master the cursor when becoming a complete C programmer. Now start with the simplest steps.
As you know, each variable in a given memory area and each memory location has its address defined for ease of access using the operator (&), corresponding to its address in memory. . Looking at the example below, it will print the address of the variable defined:
#include int main () { int bien1 ; char bien2 [ 25 ]; printf ( "Dia chi cua bien1 la: %xn" , & bien1 ); printf ( "Dia chi cua bien2 la: %xn" , & bien2 ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and run C program to see results:
So you understand what is the memory address and how to access it, which is the basic definition. Now let's see what a pointer is.
What is a pointer?
A pointer - a pointer is a variable in which its value is the address of another variable. For example, the address of the device. Like variables and constants, you must declare the pointer before you can use it to store any address of the variable. The general form of cursor declaration is as follows:
kieu_du_lieu * ten_bien ;
Here, kieu_du_lieu is the basic pointer data type, it is a valid type in C language and var-ten_bien is the value name of the cursor. The * character part used in declaring pointers is the same as what you use for multiplication. However, in this declaration, the * character is designed to use pointer variables. Here are some valid declarations of pointers:
int * contro ; /* con tro tro toi mot so nguyen */ double * phithuebao ; /* con tro tro toi mot so double */ float * hocphi ; /* con tro tro toi mot so float */ char * ho , * ten ; /* con tro tro toi mot ky tu */
The actual data type of the value of all pointers, can be integers, floats, characters, or other types as a long hexadecimal number - Long hexa represents a memory address. The only difference of pointers of different data types is the data type of the variable or constant that the pointer points to.
How to use pointers in C?
There are several important operations, which will help us work with pointers on a regular basis: a) we define pointer variables, b) assign the address of the variable to a subset and c) finally access Address variable values in pointer variables. This is done by the operator * returning the value of the variables contained in the address specified by this operator. Here are the uses of the above operations:
#include int main () { int bien = 20 ; /* phan khai bao bien thuc su */ int * contro ; /* phan khai bao bien con tro */ contro = & bien ; /* luu tru dia chi cua bien trong con tro */ printf ( "Dia chi cua bien la: %xn" , & bien ); /* dia chi duoc luu tru trong bien con tro */ printf ( "Dia chi duoc luu tru trong bien contro la: %xn" , contro ); /* Truy cap gia tri boi su dung con tro */ printf ( "Gia tri cua bien *contro la: %dn" , * contro ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and run C program to see results:
NULL pointer in C
There is a good practice when we assign a NULL value to a return variable in case you don't have the correct address to be assigned. This usually happens during the declaration process. A pointer assigned a NULL value is called a null pointer.
The null pointer is a constant with a value of 0 defined in some standard libraries. See the program below:
#include int main () { int * contro = NULL ; printf ( "Gia tri cua contro la: %xn" , contro ); return 0 ; }
Compiling and running the above C program will result:
Gia tri cua contro la : 0
In most systems, the program does not allow access and memory cells have addresses 0 because this memory is reserved for the operating system. If the pointer reaches a null value, it is considered to be pointless.
To check if it is a null pointer, you can use the if statement as follows:
if ( contro ) /* la true neu contro khong phai la null */ if (! contro ) /* la true neu contro la null */
Details about the cursor in C
Pointers are many but easy to define and very important in C language programming. Here are the important and clear definitions of pointers in the C programming language:
Concept DescriptionArithmetic pointer in C
There are 4 algebra operators that can be used on pointers: ++, -, +, -Array of pointers in C
You can define arrays to hold pointers.Pointers point to pointers in C
C allows you to point to a pointer .Transmit pointers to functions in C
Pass a parameter by reference or address: both for passed parameters can be changed in the function called by the called function.Returns the pointer from the function in C
C allows a function to return a pointer to a local variable, static variable, and also dynamically allocated memory.According to Tutorialspoint
Previous article: Array in Language C
Next lesson: String in C
You should read it
- Cursor NULL in C / C ++
- Cursor this in C ++
- Cursor in C / C ++
- How to use Head Pointer on a Mac
- The basic steps of fixing cursor cursors flutter in Windows
- Arithmetic pointer in C / C ++
- How to create a yellow circle around the mouse cursor on Windows
- Pointers to pointers in C ++
- Instructions to change the interface of the mouse cursor on Windows
- How to change the pointer sensitivity, touchpad on a Mac
- Safe code in C #
- How to change the color and size of the mouse pointer on Windows 10
Maybe you are interested
Apply immediately how to create beautiful effects for computer mouse pointers
Instructions for creating mouse pointer highlights on Windows
How to Fix Lost Mouse Pointer on Laptop Screen?
How to change mouse pointer on Google Chrome - Personalize mouse pointer
How to use pointer events in JavaScript
3 ways to quickly change the mouse pointer