Assigning a value to a memory address by pointer in C language

Source: Internet
Author: User

1. An intuitive approach

Suppose you now need to put an integer number 0x100 into the memory 0x12ff7c address. How can we do that?

We know that it is possible to write data through a pointer to the memory address to which it is pointing, so the memory address here 0x12ff7c its essence is not a pointer. So we can use the following method:

int*p = (int *)0x12ff7c;

*p = 0x100;

It is important to note that the assignment of the address 0x12ff7c to the pointer variable p must be cast.

1.1 Why here, we dare to 0x12ff7c this address to assign value?

As for why the memory address here 0x12ff7c, instead of choosing a different address, such as 0xff00. This is just for the convenience of testing on Visual C + + 6.0. If you choose 0xff00, perhaps in executing *p = 0x100; This statement, the compiler will report a memory access error because the memory at address 0xff00 you may not have the power to access. In that case, how do we know that a memory address can be legitimately accessed? So how do you know that the memory at address 0x12ff7c can be accessed? In fact, this is very simple, we can first define a variable i, such as:

inti = 0;

The memory of the variable i is definitely accessible. Then look at the value of &i on the Watch window of the compiler do not know its memory address? Here I get the address is 0x12ff7c, that's all (the different compilers may assign the variable I memory address differently each time, just as the Visual C + + 6.0 each time). You can assign a value to any address that can be legitimately accessed. Get this address and then put "int i = 0;" This code is removed. All the "evidence" was destroyed completely, and it was done flawlessly.

2. Another method

Is there any other way besides this? Not necessarily. We can even write this code directly:

*(int*)0x12ff7c = 0x100;

This line of code is actually different from the two lines above. The address 0x12ff7c is cast first, telling the compiler that the address will store an int type of data, and then write a data to the memory through the key "*".

The above discussed so much, in fact, the expression of the form is not important, it is important that the way of thinking. That means we have a way of writing data to a specified memory address.

Assigning a value to a memory address by pointer in C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.