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