Pointer constants, constant pointers and scanf input issues in C language

This constant pointer (the constant we usually define) is particularly easy to confuse with pointer constants, so let’s briefly talk about it:

 

 Look at the code:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 
  5 int main() {
  6         int a = 20,b = 30;
  7         //常量指针,指向地址的值不能更改
  8         int const *pa = &a; 
  9         //指针常量,指向的地址不能更改
 10         int * const pb = &b;
 11         printf("%d %d\n",*pa,*pb);//打印值正常打印
 12         //我想修改常量指针地址保存的值
 13         *pa = 530;//也就是把a改成530
 14         //我想修改指针常量保存的地址
 15         pb = &a;//让它指向a
 16         return 0;
 17 }

 operation result:

 The error was reported above on lines 14 and 15.

Let’s talk about the scanf input problem. Generally, we don’t understand a function. We can check it through the manual:

 This is to input data into the memory, that is to say, we must add the address symbol of the variable:

Let's check the memory address changes through vs2013 on Windows:

Code:

The above steps are to turn on debugging => Start debugging, the program will stop at the breakpoint we set, and it will print us a memory address, as follows:

Then we click Debug => Window => Memory => to jump to this address. According to column 1, four-byte integer, signed display, it is obvious that what we see is a garbled code:

 Then we continue to execute the program, which will ask us to enter a data. We enter 12, and then we can see the memory changes as follows:

Obviously this position has changed to 12, indicating that scanf inputs a data, which is closely related to the memory address.

 Of course, we can also enter data for scanf through the data in the document:

First, we first find the file location of our debugger exe:

Then open cmd and enter a location like this:

 I have a 2.txt text here:

 Now I just want to pass 100 into the program:

It will only look for the first data in the document to see if it conforms to the format. If it does not conform to the format, it will print the following garbled information:

 Let’s talk about the issue of scanf input symbol format control:

In the input form of %d%d%d like the above, each data must be distinguished by spaces or tab or enter keys.

 The input form of data is as follows:

The array itself represents the memory address of this space, so there is no need to & to get the address.

Let’s talk about the scanf format control statement input:

There are strings and = in the scanf above. Such strings must be printed out one by one in the original format when we assign values. 

But there is a bug here, prompting us that garbled characters appear. Why? Obviously there is a problem with the end of the string, because the end of the string is \0, so we must allow the program to find the end and print out the correct string sequence. So at this time, we thought that it would be enough to initialize all the character arrays to \0 characters.

If you see 0 above, it means \0

So modify the code:

Operation returns to normal:

 Let’s talk about the use of the * format character:

 This symbol can be placed in front of the format character, which means that the current data will be skipped and no value will be assigned.

Code:

Note that since you skipped it, don’t assign a value. We give str2 an initial value m here. 

Then we create a text and get the value from the text:

 The above will skip love and will not print love.

Guess you like

Origin blog.csdn.net/Pxx520Tangtian/article/details/122794458
Recommended