Manage mouse pointers (windows forms

The real power of C lies in pointers. The pointers are slightly difficult khổng lồ grasp at first. After going through the basics of pointers, you will get a better idea about what they are và how to use them.

Bạn đang xem: Manage mouse pointers (windows forms

What is a Pointer? #

A pointer is a variable used to lớn store a memory address. Let's first learn how memory is organized inside a computer.

Memory in a computer is made up of bytes (A byte consists of 8 bits) arranged in a sequential manner. Each byte has a number associated with it just lượt thích index or subscript in an array, which is called the address of the byte. The address of byte starts from 0 lớn one less than the size of memory. For example, say in a 64MB of RAM, there are 64 * 2^trăng tròn = 67108864 bytes. Therefore the address of these bytes will start from 0 to lớn 67108863.

*

Let's see what happens when you declare a variable.


As we know an int occupies 4 bytes of data (assuming we are using a 32-bit compiler) , so compiler reserves 4 consecutive sầu bytes from memory to lớn store an integer value. The address of the first byte of the 4 allocated bytes is known as the address of the variable marks. Let's say that address of 4 consecutive bytes are 5004, 5005, 5006 and 5007 then the address of the variable marks will be 5004.

*

Address Operator (&) #

To find the address of a variable, C provides an operator called address operator (&). To find out the address of the variable marks we need lớn place & operator in front of it, like this:


&marks
The following program demonstrates how to use the address operator (&).


1 2 3 4 5 6 7 8 91011121314
// Program to lớn demonstrate address(&) operator#includeint main() int i = 12; printf("Address of i = %u ", &i); printf("Value of i = %d ", i); // signal to operating system program ran fine return 0;
Expected Output:


12
Address of i = 2293340Value of i = 12
Note: Address of i may vary every time you run the program.

How it works:

To find the address of the variable, precede the variable name by & operator. Another important thing to notice about the program is the use of %u conversion specification. Ređiện thoại tư vấn that %u conversion specification is used to lớn print unsigned decimal numbers & since the memory addresses can't be negative, you must always use %u instead of %d.

Address of operator (&) can't be used with constants or expression, it can only be used with a variable.


&var; // ok&12; // error because we are using & operator with a constant&(x+y) // error because we are using & operator with an expression
We have been using address operator(&) in the function scanf() without knowing why? The address of a variable is provided khổng lồ scanf(), so that it knows where lớn write data.

Declaring Pointer Variables #

As already said a pointer is a variable that stores a memory address. Just lượt thích any other variables you need khổng lồ first declare a pointer variable before you can use it. Here is how you can declare a pointer variable.

Syntax: data_type *pointer_name;

data_type is the type of the pointer (also known as the base type of the pointer). pointer_name is the name of the variable, which can be any valid C identifier. Let's take some examples:


int *ip means that ip is a pointer variable capable of pointing lớn variables of type int. In other words, a pointer variable ip can store the address of variables of type int only. Similarly, the pointer variable fp can only store the address of a variable of type float. The type of variable (also known as base type) ip is a pointer khổng lồ int & type of fp is a pointer to lớn float. A pointer variable of type pointer to int can be symbolically represented as (int *). Similarly, a pointer variable of type pointer to lớn float can be represented as (float *).

Just lượt thích other variables, a pointer is a variable so, the compiler will reserve some space in memory. All pointer variable irrespective of their base type will occupy the same space in memory. Normally 4 bytes or 2 bytes (On a 16-bit Compiler) are used to lớn store a pointer variable (this may vary from system lớn system).

Assigning Address khổng lồ Pointer Variable #

After declaring a pointer variable the next step is khổng lồ assign some valid memory address khổng lồ it. You should never use a pointer variable without assigning some valid memory address to it, because just after declaration it contains garbage value và it may be pointing to lớn anywhere in the memory. The use of an unassigned pointer may give an unpredictable result. It may even cause the program khổng lồ crash.


Here ip is declared as a pointer khổng lồ int, so it can only point khổng lồ the memory address of an int variable. Similarly, fp can only point khổng lồ the address of a float variable. In the last two statements, we have assigned the address of i và f to lớn ip and fp respectively. Now, ip points lớn the variable i và fp points lớn variable f. It is important to lớn note that even if you assign an address of a float variable lớn an int pointer, the compiler will not show you any error but you may not get the desired result. So as a general rule always you should always assign the address of a variable to its corresponding pointer variable of the same type.

*

We can initialize the pointer variable at the time of declaration, but in this case, the variable must be declared & initialized before the pointer variable.

Xem thêm: Kinh Nghiệm Giảm Cân Đẹp Dáng Của Thành Viên Trên Webtretho, Kinh Nghiệm Giảm Cân Không Thuốc


You can assign the value of one pointer variable to another pointer variable If their base type is same. For example:


After the assignment, p1 & p2 points khổng lồ the same variable marks.

*

As already said when a pointer variable is declared it contains garbage value và it may be point anywhere in the memory. You can assign a symbolic constant called NULL (defined in stdio.h) lớn any pointer variable. The assignment of NULL guarantees that the pointer doesn't point to lớn any valid memory location.


Dereferencing Pointer Variable #

Dereferencing a pointer variable simply means accessing data at the address stored in the pointer variable. Up until now, we have sầu been using the name of the variable lớn access data inside it, but we can also access variable data indirectly using pointers. To make it happen, we will use a new operator called the indirection operator (*). By placing indirection operator (*) before a pointer variable we can access data of the variable whose address is stored in the pointer variable.


Here ip stores address of variable i, if we place * before ip then we can access data stored in the variable i. It means the following two statements does the same thing.


Indirection operator (*) can be read as value at the address. For example, *ip can be read as value at address ip.

Note: It is advised that you must never apply indirection operator to an uninitialized pointer variable, doing so may cause unexpected behaviour or the program may even crash.


Now we know by dereferencing a pointer variable, we can access the value at the address stored in the pointer variable. Let's dig a little deeper khổng lồ view how the compiler actually retrieves data.


Let's say pointer cp contains the address 1000. When we write *cp the compiler knows that it has to retrieve information from the starting address 1000. Now the question arises how much data khổng lồ retrieve sầu from starting address 1000 ? 1 bytes, 2 bytes; What vì chưng you think ? To know how much information lớn retrieve sầu from starting address 1000, the compiler looks at the base type of pointer and will retrieve sầu the information depending upon the base type of pointer. For example, if the base type is pointer khổng lồ char then 1 byte of information from the starting address will be retrieved and if the base type pointer lớn int then 4 bytes of information from the starting address will be retrieved. It is important khổng lồ note that if you are on a system where the form size of int is 2 bytes then 2 bytes of information from the starting address will be retrieved.

So in our case, only 1 byte of data from starting address will be retrieved. i.e the data stored at address 2000 will be retrieved only.

Similarly, if ip points khổng lồ the address 2000. On writing *ip compiler will retrieve 4 bytes of data starting from address 2000.

In the following image, shaded portion shows the number of bytes retrieved.

*

Before moving ahead, Interpret the meaning of the following expression:

*(&i) , where i is a variable of type int.

We know from the precedence table that parentheses () has the highest precedence, so &i is evaluated first. Since &i is the address of variable i, so dereferencing it with * operator will give sầu us the value of the variable i. So we can conclude that writing *(&i) is same as writing i.

The following example demonstrates everything we have sầu learned about pointers so far.


#includeint main() int i = 12, *ip = &i; double d = 2.31, *dp = &d; printf("Value of ip = address of i = %d ", ip); printf("Value of fp = address of d = %d ", d); printf("Address of ip = %d ", &ip); printf("Address of dp = %d ", &dp); printf("Value at address stored in ip = value of i = %d ", *ip); printf("Value at address stored in dp = value of d = %f ", *dp); // memory occupied by pointer variables // is same regardless of its base type printf("Size of pointer ip = %d ", sizeof(ip)); printf("Size of pointer dp = %d ", sizeof(dp)); // signal to lớn operating system program ran fine return 0;
Value of ip = address of i = 2686788Value of fp = address of d = 1202590843Address of ip = 2686784Address of dp = 2686772Value at address stored in ip = value of i = 12Value at address stored in dp = value of d = 2.310000Size of pointer ip = 4Size of pointer dp = 4
Note: Memory address may vary every time you run the program.

There is nothing new in the above program that deserves any explanation. Before we proceed lớn next chapter, always remember that the kích thước of the pointer variables is same regardless of its base type but the kích thước of the memory address that will be accessed while dereferencing depends on upon the base type of the pointer variable.


report this ad C Programming Tutorial report this ad