c++ scanf读取_使用scanf()读取内存地址并在C中打印其值

c++ scanf读取

Here, we have to input a valid memory address and print the value stored at memory address in C.

在这里,我们必须输入一个有效的内存地址并在C中打印存储在内存地址中的值。

To input and print a memory address, we use "%p" format specifier – which can be understood as "pointer format specifier".

要输入和打印内存地址,我们使用“%p”格式说明符-可以理解为“指针格式说明符”

Program:

程序:

In this program - first, we are declaring a variable named num and assigning any value in it. Since we cannot predict a valid memory address. So here, we will print the memory address of num and then, we will read it from the user and print its value.

在此程序中-首先,我们声明一个名为num的变量,并在其中分配任何值。 由于我们无法预测有效的内存地址。 因此,在这里,我们将打印num的内存地址,然后从用户读取并打印其值。

#include <stdio.h>
int main(void) 
{
int num = 123;
int *ptr; //to store memory address
printf("Memory address of num = %p\n", &num);
printf("Now, read/input the  memory address: ");
scanf ("%p", &ptr);
printf("Memory address is: %p and its value is: %d\n", ptr, *ptr);
return 0;
}

Output

输出量

Memory address of num = 0x7ffc505d4a44
Now, read/input the  memory address: 7ffc505d4a44
Memory address is: 0x7ffc505d4a44 and its value is: 123 

Explanation:

说明:

In this program, we declared an unsigned int variable named num and assigned the variable with the value 123.

在此程序中,我们声明了一个名为num的无符号int变量,并为其分配了值123

Then, we print the value of num by using "%p" format specifier – it will print the memory address of num – which is 0x7ffc505d4a44.

然后,我们使用“%p”格式说明符打印num的值-它会打印num的内存地址-0x7ffc505d4a44

Then, we prompt a message "Now, read/input the memory address: " to take input the memory address – we input the same memory address which was the memory address of num. The input value is 7ffc505d4a44. And stored the memory address to a pointer variable ptr. (you must know that only pointer variable can store the memory addresses. Read more: pointers in C language).

然后,我们提示消息“现在,读取/输入内存地址:”以输入内存地址-我们输入与num的内存地址相同的内存地址。 输入值为7ffc505d4a44 。 并将存储的地址存储到指针变量ptr中 。 (您必须知道只有指针变量才能存储内存地址。更多:C语言指针)。

Note: While input, "0x" is not required.

注意:输入时,不需要“ 0x”

And finally, when we print the value using the pointer variable ptr. The value is 123.

最后,当我们使用指针变量ptr打印值时。 值是123

翻译自: https://www.includehelp.com/c-programs/read-a-memory-address-using-scanf-and-print-its-value.aspx

c++ scanf读取

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542679.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python正则匹配_Python正则表达式只匹配一次

我正在尝试创建一个简单的降价乳胶转换器,只是为了学习 python和基本的正则表达式,但我不知道试图弄清楚为什么下面的代码不起作用&#xff1a; re.sub (r\[\*\](.*?)\[\*\]: ?(.*?)$, r\\footnote{\2}\1, s, flagsre.MULTILINE|re.DOTALL) 我想转换像&#xff1a; s "…

Virtual Network (1) - How to use it in a guest

本文将讲述一个问题&#xff1a;kvm guest使用libvirt xml定义如何使用virtual network&#xff1f;1&#xff09;nat&#xff0c; route &#xff0c;isolated, open类型在host中定义virtual network会创建一个虚拟的bridge&#xff0c;相当于一个交换机。guest只需要连接到这…

java string做除法_如果用java来实现传统方式的除法,用String来保存结果,想精确多少位都行,那改怎么做?...

我会加分的&#xff0c;提个思路都行&#xff0c;目前做了个乘法和加法&#xff0c;但是现在对除法没有什么思路。以下是我编写的功能&#xff1a;publicclassCalculator{publicstaticStringmulti(Strings1,Strings2){if(s1nu...我会加分的&#xff0c;提个思路都行&#xff0c…

c语言数组的声明和初始化_C声明和初始化能力问题和解答

c语言数组的声明和初始化This section contains aptitude questions and answers on C language Declarations and Initialization. 本节包含有关C语言声明和初始化的适切性问题和解答。 1) What will be the output of following program ? int main(){int m10;int xprintf(…

python2和python3的默认编码_python2和python3哪个版本新

Python2 还是 Python3 &#xff1f; py2.7是2.x系列的最后一个版本&#xff0c;已经停止开发&#xff0c;不再增加新功能。2020年终止支持。 所有的最新的标准库的更新改进&#xff0c;只会在3.x的版本里出现。Python3.0在2008年就发布出来&#xff0c;而2.7作为2.X的最终版本并…

html-css样式表

一、CSS&#xff1a;Cascading Style Sheet—层叠样式表&#xff0c;其作用是美化HTML网页。 样式表分类&#xff1a;内联样式表、内嵌样式表、外部样式表 1、内联样式表 和HTML联合显示&#xff0c;控制精确&#xff0c;但是可重用性差&#xff0c;冗余多。 例如&#xff1a;&…

java 栈 先进后出_栈先进后出,堆先进先出

1.栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方。与C不同&#xff0c;Java自动管理栈和堆&#xff0c;程序员不能直接地设置栈或堆。2.栈的优势是&#xff0c;存取速度比堆要快&#xff0c;仅次于直接位于CPU中的寄存器。但缺点是&#xff0c;存在栈中的数据大小与生…

c#给定二维数组按升序排序_在数组中按升序对数字进行排序| 8086微处理器

c#给定二维数组按升序排序Problem: Write a program in 8086 microprocessor to sort numbers in ascending order in an array of n numbers, where size n is stored at memory address 2000 : 500 and the numbers are stored from memory address 2000 : 501. 问题&#xf…

使用python套用excel模板_Python自动化办公Excel-从表中批量复制粘贴数据到新表

1、模块安装 1&#xff09;cmd模式下&#xff1a; pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openpyxl 2&#xff09;如果有安装Pycharm&#xff0c;则在程序中操作如下&#xff1a; 菜单栏&…

在HubSpot是如何应对Fat JAR困境的

在七月底&#xff0c;Spring Boot和Dropwizard分别发布了1.4和1.0版本&#xff0c;它们都是基于Fat JAR的。随着人们更多地采用这些框架和微服务架构&#xff0c;Fat JAR成为了通用的部署机制。\\Fat JAR技术会将Java应用的所有依赖打包到一个bundle之中&#xff0c;便于执行&a…

给定数字的b+树创建_在C ++中找到给定数字中的两个的下一个和上一个幂

给定数字的b树创建Problem statement: 问题陈述&#xff1a; Find Next and previous power of two of a given number 查找给定数字中两个的下一个和上一个幂 Next power of two 下一个二的幂 Example(1):input: 22output: 32 ( as 32 is 2^5)Example(2):input: 54output…

java 字节数组作用_这段java代码中字节数组b起到了什么作用?

importjava.io.*;importjavax.swing.*;publicclassIOMonitor{publicstaticvoidmain(String[]temp){//TODO自动生成的方法存根byteb[]newbyte[2];try{FileInputStreamfisnewFileInput...import java.io.*;import javax.swing.*;public class IOMonitor {public static void main…

如何查看本地的崩溃log_过年回家,还怕抢不到票?程序员教你如何抢票

2019年接近尾声&#xff0c;距离春节回家的日子越来越近&#xff0c;26日起&#xff0c;2020年除夕火车票正式开售&#xff0c;抢票大战也进入白热化阶段。是否为某抢票 App 加速而烦恼&#xff0c;是否为车票“秒光而烦恼”。别慌&#xff0c;作为连“对象”都是 new 出来的程…

获取列表中包含的元素数 在C#中

Given a list, and we have to count its total number of elements using List.Count property. 给定一个列表&#xff0c;我们必须使用List.Count属性计算其元素总数 。 C&#xff03;清单 (C# List) A list is used to represent the list of the objects, it is represent…

I00037 亏数(Deficient number)

数论中&#xff0c;若一个正整数除了本身之外所有因子之和比此数自身小&#xff0c;则称此数为亏数。亏数&#xff08;Deficient number&#xff09;也称为缺数&#xff0c;参见百度百科_亏数&#xff0c;或参见维基百科的Deficient number。亏数在OEIS中的数列号为A005100。 问…

hashmap转红黑树的阈值为8_面试必考的 HashMap,这篇总结到位了

点击蓝色“JavaKeeper”关注我哟加个“星标”&#xff0c;一起成长&#xff0c;做牛逼闪闪的技术人1 概述HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长.HashMap是非线程安全的,只适用于单线程环…

linux用户组管理命令_Linux用户和组命令能力问题和解答

linux用户组管理命令This section contains Aptitude Questions and Answers on Linux User and Group Commands. 本节包含有关Linux用户和组命令的 Aptitude问答。 1) Which of the following commands is used to create a new user in the Linux operating system? create…

Failed to start firewalld.service: Unit firewalld.service is masked.

2019独角兽企业重金招聘Python工程师标准>>> FireWall in Centos 7 masked How to resolve the error message belowFailed to issue method call: Unit firewalld.service is masked. The main reason a service is masked is to prevent accidental starting or e…

mysql第二个索引_MySQL高级第二章——索引优化分析

一、SQL性能下降原因1.等待时间长&#xff1f;执行时间长&#xff1f;可能原因&#xff1a;查询语句写的不行索引失效(单值索引、复合索引)CREATE INDEX index_user_name ON user(name);(底层做了一个排序)CREATE INDEX index_user_nameEmail ON user(name,email);查询关联join…

递归反转链表改变原链表吗_在不使用递归的情况下找到链表的长度

递归反转链表改变原链表吗Solution: 解&#xff1a; Algorithm to find length 查找长度的算法 Input: 输入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head. 一个单链表 &#xff0c;其第一个节点的地址存储在指针(例…