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 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
May be interested
- Replace the mouse cursor with funny picturesinstead of the default windows mouse cursor, you can create more funny and lovely mouse pointers that suit your taste and help create more interest in working with computers.
- How to change the cursor size on the Debian 10 desktopdebian allows you to change the cursor size via the settings utility. you can access the settings utility through the application launcher search.
- Macromedia Flash - Dynamic cursor objectread this exercise and see how to create a dynamic cursor with just a few basic steps and of course use action script in flash 8.
- Macromedia Flash - Move objectswith this exercise, you will learn how to move any object with the cursor in flash 8 using the action script.
- The basic steps of fixing cursor cursors flutter in Windowsthe status of the mouse pointer jumps, the mouse is disordered or the mouse moves automatically while typing the keyboard occurs quite popular, it causes inhibition for the user to keep the mouse to hold the cursor back. this may be because the touchpad on your laptop is too sensitive or maybe because you accidentally touched the touchpad when typing. please try to apply the following corrections below.
- Macromedia Flash - Push objects away from the cursorthis tutorial will show you how to make a flash file with the effect of pushing the object away from the cursor by using some action script fragments in flash 8. let's look at the following example file.
- Create snow effect when moving mouse on Windowsin many parts of the world, snow began to fall and turn everything into a white color from the roof, the street, the park, the playground, everything looked like a long white sheet of paper. you can create snowfall in your computer using the cursor snowflakes software, making it snow every time you move the mouse on the screen.
- How to Restart Windows 8to restart windows 8, move the cursor to the upper/lower right corner → click → click the button → click . you can also use an alternate method that uses only the keyboard in situations where you cannot use the mouse. move your cursor to th...
- Mouse cursor spins error in Windows 10+11 and how to fix it quicklyare you feeling annoyed because the mouse pointer keeps spinning in windows 10+11? it's okay, tipsmake has measures in place
- How to change mouse cursor on Google Chrometo change the type of mouse pointer on google chrome, we can install the support utility.