c ++查找字符串_C ++结构| 查找输出程序| 套装3
c ++查找字符串
Program 1:
程序1:
#include <iostream> #pragma pack(1) using namespace std;typedef struct{int A;int B;char c1;char c2; } S;int main() {cout << sizeof(S);return 0; }Output:
輸出:
In a 32-bit system: 10 In a 64-bit system: 18Explanation:
說明:
We compiled the program on a 32-bit system. It will print "10" on the console screen.
我們?cè)?2位系統(tǒng)上編譯了該程序。 它將在控制臺(tái)屏幕上打印“ 10” 。
Normally, the above program will print "12" due to structure padding. But, here, we used #pragma pack(1) directives. It is used to avoid the structure padding. Then the size of the above structure will be "10".
通常,由于結(jié)構(gòu)填充,上述程序?qū)⒋蛴?strong>“ 12” 。 但是,在這里,我們使用了#pragma pack(1)指令。 用于避免結(jié)構(gòu)填充。 那么上述結(jié)構(gòu)的大小將為“ 10” 。
Program 2:
程式2:
#include <iostream> using namespace std;struct st {char c1;int A;int B;char c2; };int main() {struct st* ptr;cout << sizeof(ptr);return 0; }Output:
輸出:
In a 32-bit system: 4 In a 64-bit system: 8Explanation:
說明:
We compiled this program on a 32-bit system.
我們?cè)?2位系統(tǒng)上編譯了該程序。
Here, we created a structure with 4 members, and a pointer to the structure.
在這里,我們創(chuàng)建了一個(gè)具有4個(gè)成員的結(jié)構(gòu) ,以及一個(gè)指向該結(jié)構(gòu)的指針。
cout<<sizeof(ptr);The above statement will print "4". Because, the size of any type of pointer in a 32-bit system is 4 bytes.
上面的語句將打印“ 4 ”。 因?yàn)?#xff0c;在32位系統(tǒng)中,任何類型的指針的大小均為4個(gè)字節(jié)。
Program 3:
程式3:
#include <iostream> using namespace std;struct st {char c1;int A;int B;char c2; };int main() {struct st ob;struct st* ptr = &ob;cout << sizeof(*ptr);return 0; }Output:
輸出:
16Explanation:
說明:
We compiled the program on 32-bit based system.
我們?cè)诨?2位的系統(tǒng)上編譯了該程序。
Here, we created a structure, C++ uses structure padding and allocate space for variables block-wise, block size is 4 bytes for 32-bit based system.
在這里,我們創(chuàng)建了一個(gè)結(jié)構(gòu),C ++使用結(jié)構(gòu)填充并按塊逐個(gè)分配變量,對(duì)于基于32位的系統(tǒng),塊大小為4個(gè)字節(jié)。
In the main() function we created a structure variable ob, and a pointer ptr that contains the address of ob. ?
在main()函數(shù),我們創(chuàng)建了一個(gè)結(jié)構(gòu)變量的ob和包含OB的地址的指針PTR。
cout<<sizeof(*ptr);The above statement will print the size of the structure.
上面的語句將打印結(jié)構(gòu)的大小。
1st block is allocated for c1 that is 4 bytes. 2nd block is allocated for A that is 4 bytes. 3rd block is allocated for B that is 4 bytes. 4th block is allocated for c2 that is 4 bytes.Then, the total size is "16" bytes will be printed on the console screen.
然后,總大小為“ 16”字節(jié)將被打印在控制臺(tái)屏幕上。
翻譯自: https://www.includehelp.com/cpp-tutorial/structures-find-output-programs-set-3.aspx
c ++查找字符串
總結(jié)
以上是生活随笔為你收集整理的c ++查找字符串_C ++结构| 查找输出程序| 套装3的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 的garch预测_随机森林预测
- 下一篇: strictmath_Java Stri