欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

c ++查找字符串_C ++结构| 查找输出程序| 套装3

发布时间:2025/3/11 编程问答 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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: 18

Explanation:

说明:

We compiled the program on a 32-bit system. It will print "10" on the console screen.

我们在32位系统上编译了该程序。 它将在控制台屏幕上打印“ 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".

通常,由于结构填充,上述程序将打印“ 12” 。 但是,在这里,我们使用了#pragma pack(1)指令。 用于避免结构填充。 那么上述结构的大小将为“ 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: 8

Explanation:

说明:

We compiled this program on a 32-bit system.

我们在32位系统上编译了该程序。

Here, we created a structure with 4 members, and a pointer to the structure.

在这里,我们创建了一个具有4个成员的结构 ,以及一个指向该结构的指针。

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 ”。 因为,在32位系统中,任何类型的指针的大小均为4个字节。

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:

输出:

16

Explanation:

说明:

We compiled the program on 32-bit based system.

我们在基于32位的系统上编译了该程序。

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.

在这里,我们创建了一个结构,C ++使用结构填充并按块逐个分配变量,对于基于32位的系统,块大小为4个字节。

In the main() function we created a structure variable ob, and a pointer ptr that contains the address of ob.  

在main()函数,我们创建了一个结构变量的ob和包含OB的地址的指针PTR。

cout<<sizeof(*ptr);

The above statement will print the size of the structure.

上面的语句将打印结构的大小。

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”字节将被打印在控制台屏幕上。

翻译自: https://www.includehelp.com/cpp-tutorial/structures-find-output-programs-set-3.aspx

c ++查找字符串

总结

以上是生活随笔为你收集整理的c ++查找字符串_C ++结构| 查找输出程序| 套装3的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。