c++经典编程题_【经典C语言知识】C/C++编程难点总结
知識點一:指針
1. ?指針:變量在內存中所存儲空間的首編號,就稱作為該變量的地址,也叫做指針。
指針變量: 他專門存放另外一個變量的指針? ? ?
int*? ? p_age;
p_age=&age;
2.數組與指針
使用指針訪問一維數組元素:如果指針變量p_score已經指向數組中的一個元素,則p_score+1表示指向同一數組中的下一個元素。
#include
using namespace std;
int?
main()
{
int score[5];
int *p_score;
int sum = 0;
for (int i = 0; i < 5; i++)
{
cin >> score[i];
}
p_score = score;
for (int i = 0; i < 5; i++)
{
sum += *(p_score + i);
}
cout << sum << endl;
system("pause");
return 0;
}
二維數組的指針
#include
using namespace std;
int main()
{
int score[2][3];
int* p;
int sum = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> score[i][j];
}
}
p = score[0];? ? ?//二維數組中,不能夠是p=score
for (; p < score[0] + 6; p++)
{
sum += *p;? ?//對輸入的6個值進行求和
}
cout << sum << endl;
system("pause");
return 0;
}
指針和函數
使用指針作為函數的參數
#include
using namespace std;
void swap(int*?
data1, int* data2)
{
int tmp;
tmp = *data1;
*data1 = *data2;
*data2 = tmp;
}
int main()
{
int a = 10, b = 20;
swap(&a,&b);
cout << a << " " << b << endl;
system("pause");
return 0;
}
使用數組名作為函數的參數
#include
using namespace std;
int average(int* arr, int num)
{
int sum = 0;
for (int i = 0; i < num; i++)
{
sum = sum + arr[i];
}
return (sum / num);
}
int main()
{
int?
score[5] = {60,50,70,80,90};
cout << average(score, 5) << endl;
system("pause");
return 0;
}
知識點二:使用new運算符動態開辟空間
C++中允許在程序中動態開辟空間,即用多少開辟多少運算符new:
在堆內存區中進行內存的動態分配
例如:
double* d;
d=new double;
*d=30.5;或者 double* d;
d=new double(30.5);
使用數組動態開辟空間
動態數組:指針變量=new[整形表達式]
例子:
#include
using namespace std;
int main()
{
int n;
int* p;
int sum=0;
cout << "請輸入班級的人數";
cin >> n;
p = new int[n];
if (!p)
{
return 1;
}
cout << "請輸入數學的成績";
for (int i = 0; i < n; i++)
{
cin >> p[i];
}
for (int i = 0; i < n; i++)
{
sum += p[i];
}
cout << "平均值為" << sum / n << endl;
system("pause");
delete[n]p;
?p = NULL;
return 0;
}
知識點三:使用delete動態釋放空間delete d;
d=NULL; //一個好的習慣
知識點四:結構體
struct 結構體名
{
?類型 標識符 成員名;
?類型標識符 成員名;
......
};
#include
using namespace std;
struct goods {
char? name[15];
float price;
int amount;
float total;
};
int main()
{
struct goods?
myGoods;
strcpy(myGoods.name, "連衣裙");
myGoods.price = 50;
myGoods.amount = 2;
myGoods.total = myGoods.price*myGoods.amount;
cout << "寶貝名稱:" << myGoods.name << "單價:" <
myGoods.price << "數量:? " << myGoods.amount << "總價:" << myGoods.total << endl;
system("pause");
return 0;
}
知識點四:共用體
概念:使幾個不同的變量共占同一段內存的結構稱為“共用體
”,也叫做“聯合體”。
union 共用體名
{
int? i;
char ch;
float f;?
}; //這個共用體會?
開辟四個空間
union data a b;??
例子:
#include
using namespace std;
union category
{
int c1;
char position[10];
};
struct person
{
char name[10];
int num;
char job;
union category cat;
};
int main()
{
struct person stu;
struct person tea;
strcpy(stu.name, "zhang");
stu.num = 501;
stu.job = 's';
stu.cat.c1 = 5001;
strcpy(tea.name, "li");
tea.num = 200;
tea.job = 't';
strcpy(tea.cat.position, "prof");
cout << "學生的情況: " << stu.name << "," << stu.num <
"," << stu.cat.c1 << endl;
cout << "老師的情況:" << tea.name << "," << tea.num << "," <
tea.cat.position << endl;
?system("pause");
return 0;
}
總結:同一個內存段可以用來存放幾種不同類型的成員,但是在每一個瞬間只能夠存放其中一種,而不是同時存放幾種;
共用體變量中起作用的成員是最后一次存放的成員,在存入新的一個成員后原有的成員的作用就會失去作用;共用體變量的地址和它的各成員的地址都是同一地址。不能對共用體變量名賦值,也不能企圖引用變量名得到一個值,又不能在定義共用體變量時候對它初始化。
我們官方的QQ群:281549832
特別感謝網友的大力支持。
我們的開源團隊不斷擴大,希望大家快來一起加入我們吧。
在這里還是要謝謝大家的大力支持!
大家快來關注吧!
總結
以上是生活随笔為你收集整理的c++经典编程题_【经典C语言知识】C/C++编程难点总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dnar.exe是什么进程 dnar进程
- 下一篇: 雪莲5毛一包的定价13年没涨 网友力挺良