Header Ads

Pointers:C Programming Language - Part 7

Pointers



Computer memory and memory addresses

A great tool in pointer programming. Before learning about pointer, let's know some basic things that will work to understand.


How are variables stored in computer memory / RAM?

RAM is one byte in one cell one. And each byte has an address. And each byte has 8 bits.

Memory cell in ram


When we say that our RAM 8 Giga byte, then our computer's RAM can store up to 8 000 000 000 bytes of data, and each of them has an address. The first one continues to grow in the next T1, followed by the address 2. Although the computer addresses the addresses in hexadecimal number system.

When we execute / run the program after declaration of a variable, then the computer echoes some memory for those variables. How many bytes of memory will be allocated, depending on the data type of that variable and the compiler.

Generally the compilers have 2 byte memory allotts for an int. Likewise, for a char variable, 1 byte memory is allocated. For the floating-point number 4 byte memory is allocated.

As such, when a computer displays a declaration int a; Then it can understand that it is an integer variable and it needs to allocate 2 bytes of memory. Then, from the empty space of the RAM, it's a two-byte memory allocator for this integer.

We can easily find the memory location of a variable, let us see the following program

1
2
3
4
5
6
7
#include <stdio.h>
Int main ()
{
Int a = 5;
Printf ("Memory address of variable a is:% x", & a);
Return 0;
}
The above program will show something like this: Memory address of variable a is: 2686732 One computer shows one value. And once one value will appear. Because every time we run the program, each space is allocated in memory for each variable. And the address of that place is changed every time.

& Ampersend is used to find the memory address of a variable. Which is called address-of operator [&]. With which we can get the address or memory location of another variable.

When we run the program, the computer saves 2-bytes of memory for the variable A from the empty space of the RAM. For Computer Automation, there is a cell allocation of 2686732 and 2686733 for a. And to know the memory address, just know the start address. When we printed A's memory address, we got the start address 2686732 only. If the variable 2686732 and 2686733 have been allocated for a variable and its value is stored in 5 cells in these two cells. Now if we change the value of a and set another value, such as 8, then the value of the two cells at RAM 2686732 and 2686733 will be changed and in these two cells there will be 8 stores. Let's now know what the pointer is.

Pointers

Pointer is a variable whose value is the memory address of another variable. Pointer is a data, array, or variable computer's computer memory address or points. Before using pointers variables like other variables, the computer / compiler should say it is a pointer variable. A pointer declares the variable as follows.

Data_type * name;

For the integer pointer: int * i;

Asterisk [*] is declared as a pointer using a variable before. Called an indirection operator or value-at-address operator. Here are some example types of pointer declaration of data type:

1
2
3
4
Int * ip; / * pointer to an integer * /
Double * dp; / * pointer to a double * /
Float * fp; / * pointer to a float * /
Char * ch / * pointer to a character * /
We will now see how the pointer is used in a program.

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
Int main ()
{
Int a = 5; / * Variable declaration * /
Int * ip; / * Pointer variable declaration * /
Ip = & a; / * Store address of "a" in pointer variable * /
Printf ("Address of a variable:% xn", & a);
/ * Address stored in pointer variable * /
Printf ("Address stored in ip variable:% xn", ip);
Return 0;
}
Here we have declared a variable. Then we declare a pointer variable. Then we put a memory address in the pointer variable. Then & I saw the address of a variable with the operator and printed the operator. And I saw the value of the pointer variable and printed it. Both of these values ​​are the same.

If we wish we can now find the value of a with the IP pointer variable.

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
Int main ()
{
Int a = 5;
Int * ip;
Ip = & a;
/ * Access the value using the pointer * /
Printf ("Value of * ip variable:% dn", * ip);
Return 0;
}
When we run the program, the ip will show the address of the variable, and print its value. Look, when we want to extract the address of a variable with a pointer variable, then just type the pointer variable. But when we want to find out the value of the original variable with pointer variables, then add * before the pointer variable. As we print ip [pointer variables] in the first program we got the address. And in the next program we print the * ip * with an * ip before we get the value of the original variable.

  

Pass the pointer to the function


We're a pointer or one like a variable Or we can pass the memory address function to a variable. See the following program:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
Void func1 (int * pNum) {
   * PNum = 5;
   Return;
}
Int main () {
   Int num = 1;
   Printf ("Before passing:% d \ n", num);
   Func1 (& num);
   Printf ("After passing:% d \ n", num);
   Return 0;
}
Here we have taken a variable called num, which has been entered as 1. Then we print it. Got 1.


Now we've called the function. I have passed the call address num variable of the memory address. Then we went to the function and changed the value of that address to 5 sine.


Then we again print num variables. We got 5. This way we can pass the pointer to the function.

Our YouTube Channel:Clik Here



No comments

Theme images by sebastian-julian. Powered by Blogger.