c ++查找字符串
Program 1:
程序1:
#include <iostream>
using namespace std;
int main()
{
typedef struct
{
int A;
char* STR;
} S;
S ob = { 10, "india" };
S* ptr;
ptr = &ob;
cout << ptr->A << " " << ptr->STR[2];
return 0;
}
Output:
输出:
10 d
Explanation:
说明:
Here, we created a local structure with two members A and STR that is defined in the main() function. and created the object that is ob, and a pointer ptr, assigned the address of ob to ptr.
在这里,我们用main()函数中定义的两个成员A和STR创建了一个本地结构。 并创建对象ob和一个指针ptr,将ob的地址分配给ptr 。
cout <<ptr->A<<" "<<ptr->STR[2];
Here, we accessed elements using referential operator "->".
在这里,我们使用引用运算符“ -> ”访问元素。
ptr->A will be "10" and ptr->STR[2] will "d", it will access the 2nd element of the string STR.
ptr-> A将为“ 10”,而ptr-> STR [2]将为“ d” ,它将访问字符串STR的 第二个元素。
Program 2:
程式2:
#include <iostream>
using namespace std;
typedef struct{
int A;
char* STR;
} S;
int main()
{
S ob = { 10, "india" };
cout << sizeof(ob);
return 0;
}
Output:
输出:
In a 32-bit system: 8
In a 64-bit system: 16
Explanation:
说明:
Here, we created a structure with two members, one is an integer variable and another is a character pointer. We know that size of an integer is 4 bytes / 8 bytes and the size of any type of pointer is also 4 bytes / 8 bytes.
在这里,我们创建了一个具有两个成员的结构 ,一个是整数变量,另一个是字符指针。 我们知道整数的大小为4字节/ 8字节,任何类型的指针的大小也为4字节/ 8字节。
Program 3:
程式3:
#include <iostream>
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: 12
In a 64-bit system: 24
Explanation:
说明:
Here, A and B will allocate two blocks of 4 bytes / 8 bytes, and normally character variable takes 1-byte memory space but due to structure padding c1 and c2 will take 1-1 byte and block is padded by remaining 2 bytes / 6 bytes. And then no other variable can use the padded space.
在这里, A和B将分配两个4字节/ 8字节的块,通常字符变量占用1字节的存储空间,但是由于结构填充, c1和c2将占用1-1字节,而块将由剩余的2字节/ 6填充个字节。 然后没有其他变量可以使用填充的空间。
1st block will store A, 2nd block will store B, and 3rd block will store c1, c2, and 2 bytes / 6 bytes space padding.
第一个块将存储A , 第二个块将存储B , 第三个块将存储c1, c2和2字节/ 6字节的空间填充。
Then the final size of the structure will be 12 / 24 bytes.
然后该结构的最终尺寸将是12 / 24-字节。
翻译自: https://www.includehelp.com/cpp-tutorial/structures-find-output-programs-set-2.aspx
c ++查找字符串