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 ++查找字符串