Pointers in C
Next
Functions in C/C++ >| |
Each data used in an application must be defined, i.e. memory must be reserved for him. Assigning values to a data means that this value is stored in a binary form to a specific memory location. In this case, the data memory is occupied by the number of memory bits that it occupies for a particular type of data. For example. for the int-type data in C / C ++, this would be 16 or 32 bits.
Each memory location has its own address. This number is associated with a location. The smallest memory location that can be addressed independently is usually one byte.
The pointer is a free data into which the address of a location in the memory can be placed.
Data shown by pointers can be of different types.
Each memory location has its own address. This number is associated with a location. The smallest memory location that can be addressed independently is usually one byte.
The pointer is a free data into which the address of a location in the memory can be placed.
Data shown by pointers can be of different types.
When some data takes up more than one byte, the data address is the byte with the least serial number.
The address of a data in the memory can be obtained using the prefixed uninterrupted operator &. See more about operators on the pages operators in languages C/C++.
The address of a data in the memory can be obtained using the prefixed uninterrupted operator &. See more about operators on the pages operators in languages C/C++.
Defining the pointer
* pointer_identifier
Let's look at the following code:
Let's look at the following code:
An integer data was introduced and initialized to a value of 1. Also, a pint to the integer data was introduced and it was initialized with the memory address of the data a. Note that a large number in a hexadecimal record is obtained when printing a pointer value. Memory addresses are usually such values.
A description of the data type at the beginning of a declarative command indicates the type of displayed data. Previously, it was int int. the cursor points to the whole number. If the data were double, then the double data pointer would be defined and initialized in the following way:
double x =2;
double *pdb;
pdb= &x;
Assign data values via the pointer to another data
If we would like to introduce in the previous example another data of type double, for example. y and to initialize this value using a pointer to x, we would add the following code:
double x=2;
double *pdb;
pdb= &x;
double y;
y= *pdb; //y is now 2
Address arithmetic
In C / C ++, the following operations are allowed over the cursors:
- assigning a value to one pointer to another
- Adding integer data to the value of the pointer and deleting the integer data from the value of the pointer
- seizing and comparing two pointers
- comparison of the pointer by zero
Pointers and Arrays
Suppose the following sequence of integers is defined:
int A[7]={5,-2,3,8,11,0,25};
To print the elements of the array using pointers, we define the pointer pA and initialize it with the memory address of the first element of the array:
int * pA = A ;
To print the value of the array element pointed to by the pointer, use *pA:
printf("%d\n",*pA);
In order for the pointer to change the element it points to during the execution of the loop, it needs to be incremented, i.e. increase its value by 1:
pA++;
printf("%d\n",*(pA));
printf("%d\n",*(pA));
or:
printf("%d\n",*(pA+i));
In the first cycle, for i = 0 the value of the pointer, i.e. the address of the first element of the array has the value e.g. 0xfdf0(hexadecimal number) see Figure 3. In each cycle the value of the pointer is incremented by 1(pA++), which means that the new address, pointed to by the pointer, will be greather for the size of the data in bytes . That size in this case is 4 (int data has a size of 4 bytes). This value would also be obtained with sizeof(int).
In the 4th cycle, e.g. i = 3, so the value of the memory address will be greather for 3*4=12 bytes, which is also shown in the figure 3.
In the 4th cycle, e.g. i = 3, so the value of the memory address will be greather for 3*4=12 bytes, which is also shown in the figure 3.
where the variable "i" changes during the cycle.
The complete code can be seen below:
The complete code can be seen below:
#include < stdio.h >
int main()
{
int main()
{
int A[7]={5,-2,3,8,11,0,25};
int * pA = A ;
for(int i = 0;i < 7;i++)
{
return 0;
}
int * pA = A ;
for(int i = 0;i < 7;i++)
{
printf("%d\n",(pA+i));
printf("%d\n",*(pA+i));
}printf("%d\n",*(pA+i));
return 0;
If we start the debugger and stop the program inside the loop, we can analyze how the value of the address pointed to by the pointer changes during the cycle, when the value of the pointer changes by 1. For i = 3 the pointer moves, so that it points to the 4th element in the array , which means that the address has increased by 3 * 4 bytes, i.e. from the value 0x61fdf0 ("0" is on the end) change to 0x61fdfc ("c" is on the end and it represents the number 12 in the hexadecimal system).
Pointers and Strings
Let a string, i.e. a series of char data, be specified as follows:
char text[25]= "Today is a beautiful day";
The elements of this array are characters, with the last character being '\0'. Read more about strings in the C language in the article: C Strings.
To print those characters on the screen, we will use a loop, and access the characters with a pointer. Let's define a pointer to data of type char and initialize it with the memory address of the first element of the array named "text" :
To print those characters on the screen, we will use a loop, and access the characters with a pointer. Let's define a pointer to data of type char and initialize it with the memory address of the first element of the array named "text" :
char* ptr = text;
We will use a while loop, and the characters will be accessed using a pointer to data of type char. The complete code is shown below.
#include < stdio.h >
int main()
{
int main()
{
char text[25]= "Today is a beautiful day";
char* pText = text;
while (*pText != '\0')
{
return 0;
}
char* pText = text;
while (*pText != '\0')
{
printf("%c\n", *pTekst );//Prints the character pointed to by the pText pointer
pText ++;//Move the cursor to the next character
}pText ++;//Move the cursor to the next character
return 0;
The condition that is tested in the while loop is that the code value of the current character is different from the code value of the last character, that is, it must be different from '\0'.
Moving the pointer to point to the next character is achieved by increasing the value of the pointer by 1. In this case, the length of the char data is 1 byte, so the address pointed to by the pointer will change in each cycle by 1 byte and not by 4 bytes, which is was the case with data type int.
After executing the program, it will display:
Moving the pointer to point to the next character is achieved by increasing the value of the pointer by 1. In this case, the length of the char data is 1 byte, so the address pointed to by the pointer will change in each cycle by 1 byte and not by 4 bytes, which is was the case with data type int.
After executing the program, it will display:
Previous
|< Strings in C/C++ |
Next
Functions in C/C++ >| |