c ++查找字符串
Program 1:
程序1:
#include <iostream>
#include <math.h>
using namespace std;
struct st {
int A = NULL;
int B = abs(EOF + EOF);
} S;
int main()
{
cout << S.A << " " << S.B;
return 0;
}
Output:
輸出:
0 2
Explanation:
說明:
Here, we created a structure that contains two integer variables. Here, we used NULL, EOF, and abs() function for initialization of structure elements.
在這里,我們創建了一個包含兩個整數變量的結構。 在這里,我們使用NULL , EOF和abs()函數初始化結構元素。
The values of NULL and EOF are 0 and -1 respectively. And the function abs() returns positive value always.
NULL和EOF的值分別為0和-1。 并且函數abs()始終返回正值。
A = NULL; //that is 0
A = 0;
B = abs(EOF+EOF);
= abs(-1+-1);
= abs(-1-1);
= abs(-2);
= 2;
Then, the final value 0 and 2 will be printed on the console screen.
然后,最終值0和2將被打印在控制臺屏幕上。
Program 2:
程式2:
#include <iostream>
using namespace std;
typedef struct{
int A = 10;
int B = 20;
} S;
int main()
{
cout << S.A << " " << S.B;
return 0;
}
Output:
輸出:
main.cpp: In function ‘int main()’:
main.cpp:11:14: error: expected primary-expression before ‘.’ token
cout << S.A << " " << S.B;
^
main.cpp:11:28: error: expected primary-expression before ‘.’ token
cout << S.A << " " << S.B;
^
Explanation:
說明:
Here, we created a structure using typedef that contains two integer variables A and B. Consider the below statement,
在這里,我們使用typedef 創建了一個結構 , 該結構包含兩個整數變量A和B。 考慮以下語句,
cout <<S.A<<" "<<S.B;
Here, we accessed A using S. but S is not an object or variable of structure, we used typedef it means S is type, so we need to create an object of structure using S like, S ob;
在這里,我們訪問的使用S.但S是不是一個對象或結構的變量,我們使用的typedef這意味著S是類型,所以我們需要創建使用S-狀結構的目的,S OB;
Then, ob.A and ob.B will be the proper way to access A and B.
然后, ob.A和ob.B將是訪問A和B的正確方法。
Program 3:
程式3:
#include <iostream>
using namespace std;
typedef struct{
int A;
char* STR;
} S;
int main()
{
S ob = { 10, "india" };
S* ptr;
ptr = &ob;
cout << ptr->A << " " << ptr->STR;
return 0;
}
Output:
輸出:
10 india
Explanation:
說明:
Here, we created a structure with two members A and STR. In the main() function, we created the object that is ob, and? a pointer ptr and then assigned the address of ob to ptr. Accessing the elements using referential operator -> and then printed them on the console screen.
在這里,我們創建了一個具有兩個成員A和STR的結構。 在main()函數中,我們創建了對象ob和一個指針ptr ,然后將ob的地址分配給了ptr。 使用引用運算符->訪問元素,然后將其打印在控制臺屏幕上。
The final output "10 india" will be printed on the console screen.
最終輸出“ 10 india”將打印在控制臺屏幕上。
翻譯自: https://www.includehelp.com/cpp-tutorial/structures-find-output-programs-set-1.aspx
c ++查找字符串