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.pswp.cn/news/542679.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542679.shtml
英文地址,請注明出處:http://en.pswp.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;其第一個節點的地址存儲在指針(例…