- 2024高级语言程序设计:https://edu.cnblogs.com/campus/fzu/2024C
- 高级语言程序设计课程第五次作业:https://edu.cnblogs.com/campus/fzu/2024C/homework/13304
- 学号:102400215
- 姓名:胡加乘
1
#include <iostream>using namespace std;#define ROWS 3
#define COLS 5int main()
{int arr[ROWS][COLS] ={{ 1, 2, 3, 4, 5 },{ 20, 21, 22, 23, 24 },{ 6, 7, 8, 9, 10}};int (*p)[COLS] = arr; // 指向二维数组的指针// 数组下标访问元素for (int i = 0; i < ROWS; i++){for (int j = 0; j < COLS; j++){cout << arr[i][j] << " ";}cout << endl;}// 指针访问元素for (int i = 0; i < ROWS; i++){for (int j = 0; j < COLS; j++){cout << *(*(p + i) + j) << " ";}cout << endl;}return 0;
}
2
3
#include <iostream>using namespace std;void calcGrade(int (*p)[7], int students);int main()
{int grades[3][7] ={{ 0, 90, 86, 45, 79, 87, 99 },{ 0, 99, 86, 65, 80, 75, 89 },{ 0, 98, 87, 76, 81, 70, 93 },};// 求和并加到[0]中calcGrade(grades, 3);// 打印for (int i = 0; i < 3; i++){for (int j = 0; j < 7; j++){printf("%d ", grades[i][j]);}printf("\n");}return 0;
}void calcGrade(int (*p)[7], int students)
{for (int i = 0; i < students; i++){int sum = 0;for (int j = 1; j < 7; j++){sum += p[i][j];}p[i][0] = sum;}
}
4
#include <iostream>using namespace std;int main()
{int a; // 定义一个int类型的本地变量a。int* a; // 定义一个int类型的野指针变量a。int** a; // 定义一个指向int类型指针的野指针变量a。int a[10]; // 定义一个含有10个元素的数组a。int (*a)[10]; // 定义一个指向一个 每个元素含有10个元素 的数组 的指针变量a。return 0;
}
5
#include <iostream>using namespace std;int main()
{int src[3][5] ={{ 1, 2, 3, 4, 5 },{ 6, 7, 8, 9, 10 },{ 21, 22, 23, 24, 25 }};int dest[3][5];int* psrc = src[0];int* pdest = dest[0];for (int i = 0; i < 3; i++){for (int j = 0; j < 5; j++){*(pdest + i * 5 + j) = *(psrc + i * 5 + j);}}for (int i = 0; i < 3; i++){for (int j = 0; j < 5; j++){cout << dest[i][j] << " ";}cout << endl;}return 0;
}
6
#include <iostream>using namespace std;int mystrlen(char* str);int main()
{char str[] = "Hello world!";int length = mystrlen(str);cout << "Length of 'Hello world!' is: " << length << endl;return 0;
}int mystrlen(char* str)
{char* p = str;int length = 0;while (*p++ != '\0'){length++;}return length;
}
7
#include <iostream>using namespace std;int main()
{int a[3][4] = { 0 };printf("%d\n", sizeof(a)); // 二维数组所有元素的占用字节之和,共3x4个int元素,每个int元素占用4个字节,共3x4x4=48字节。printf("%d\n", sizeof(a[0][0])); // 第一行第一列元素占用4字节。printf("%d\n", sizeof(a[0])); // 第一行所有元素占用字节之和,共4x4=16字节。printf("%d\n", sizeof(a[0] + 1)); // `a[0] + 1`实际上是一个int指针,求的是指针的大小,8字节。printf("%d\n", sizeof(*(a[0] + 1))); // `*(a[0] + 1)`是第一行第二个元素值,int值的大小为4字节。printf("%d\n", sizeof(a + 1)); // `a + 1`指向二维数组的第二行,但本身仍是一个指针,8字节。printf("%d\n", sizeof(*(a + 1))); // `*(a + 1)`解引用第二行,整行共4x4=16字节。printf("%d\n", sizeof(&a[0] + 1)); // 等价于`a + 1`。printf("%d\n", sizeof(*(&a[0] + 1))); // 等价于`*(a + 1)`。printf("%d\n", sizeof(*a)); // `*a`解引用第一行,整行4x4=16字节。printf("%d\n", sizeof(a[3])); // `a[3]`指向二维数组后面的4个int值,因此4x4=16字节。但索引超出界限,存在安全隐患。return 0;
}
8
#include <iostream>using namespace std;int main()
{char str[30];cout << "Please enter a string (1 <= length < 30): " << endl;scanf_s("%s", str, sizeof(str));int m;cout << "Please enter the start index to copy: " << endl;scanf_s("%d", &m);char newStr[30] = { 0 };int i;for (char* pch = str + m - 1, i = 0; *pch; pch++, i++){newStr[i] = *pch;}cout << "Copied: "<< newStr << endl;return 0;
}