#include<stdio.h>
int main()
{
int num[10] = { 1,2,3,4,5,6,7,8,9,10};
int new_num[10];
int m;
int i=0;
scanf_s("%d", &m);
/*while (i<10)
{
if (i<10-1-m)
{
new_num[i] = num[i+m+1];
}
else
{
new_num[i] = num[i-10+m+1];
}
i++;
}*/
int* p;
int* q;
p = num+m+1;
q = num;
for (int j = 0; j < 10; j++)
{
if (j<9-m)
{
new_num[j] = *(p++);
}
else
{
new_num[j] = *(q++);
}
}
for (int i = 0; i < 10; i++)
{
printf("%d ", new_num[i]);
}
system("pause");
return 0;
}
#include<stdio.h>
int main()
{
char month[12][10] =
{ "January","February","March","April","May","June",
"July","August","September","October","November","December" };
int num;
scanf_s("%d", &num);
//printf("%s\n", month[num-1]);
//printf("%s\n",*(month+num - 1));
system("pause");
return 0;
}
函数指针:其实就是把函数的名字换成指针的名字。
#include<stdio.h>
int main()
{
int sum(int a, int b);
int (*p)();
p = sum;
printf("%d", p(5, 6));
system("pause");
return 0;
}
结构体指针地址移动
#include<stdio.h>
struct node
{
int a;
int b;
int c;
};
int main()
{
struct node s = { 3,5,6 };
//&s 是一个结构体地址,也就是结构体指针,对指针的操作加1,减1,指的是加上它的类型大小,&s +1 等于 &s + sizeof(struct node)。
int* pt = (int*)(&s + 1);
printf("%d\n", *(pt));//数组越界
printf("%d\n", *(pt-1));//6
printf("%d\n", *(pt - 2));//5
printf("%d\n", *(pt - 3));//3
system("pause");
return 0;
}
#include<stdio.h>
int main()
{ int a[6] = { 1,2,3,4,5,6 };
printf("%d\n", *((int*)(&a + 1) - 1));//输出6
printf("%d\n", *((int*)(&a + 2) - 7));//输出6
printf("%d\n", *((int*)(&a + 2) - (sizeof(a)/sizeof(int)+1)));//输出6
system("pause");
return 0;
}
int sub(int a, int b)
{
return a - b;
}
#include<stdio.h>
int main()
{
char* p = "abcde";
printf("%d %c\n", p, *p);//1001 a
p += 3;
printf("%d %c\n", p, *p);//1001+3 d
//-优先级大于&
printf("%d\n", 0 & 64 - 1);//0&63=0
//-1:11111111 11111111 11111111 11111111
printf("%d\n", 64 & 0 - 1);//64&-1=64
system("pause");
return 0;
}
字符串换序
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
char * reserve(char* str1,char* str2)
{
int n = strlen(str1);
int i = 0;
while (*str1 != '\0')
{
str2[n - i - 1] = *str1++;
i++;
}
str2[n] = '\0';
return str2;
}
int main()
{
char *str1=(char*)malloc(sizeof(char)*100);
char* str2 = (char*)malloc(sizeof(char) * 100);
assert(str1 != NULL && str2 != NULL);
cin >> str1;
reserve(str1, str2);
printf("%s", str2);
return 0;
}
带默认形参的函数
//形参带默认值的子函数
#include<iostream>
using namespace std;
int add(int a = 3, int b = 5)
{
return a + b;
}
int main()
{
cout << add(5, 6) << endl;//11
cout << add(5) << endl;//10
cout << add() << endl;//8
system("pause");
return 0;
}
//函数重载
//函数重载
#include<iostream>
using namespace std;
int add(int a, int b)
{ return a + b;}
double add(int a, double b)
{ return a + b;}
double add(double a, int b)
{ return a + b;}
double add(double a, double b)
{ return a + b;}
int main()
{
cout << add(10, 20) << endl;
cout << add(10, 22.5) << endl;
cout << add(11.5, 20) << endl;
cout << add(11, 22.5) << endl;
system("pause");
return 0;
}
函数模板
//函数模板
#include<iostream>
using namespace std;
template<class T1,class T2>
//返回值类型和形参a类型一样
T1 add(T1 a, T2 b)
{
cout << sizeof(T1) << "," << sizeof(T2) << "\t";
return a + b;
}
int main()
{
cout << add(10, 20) << endl;
cout << add(3.14, 5.98) << endl;
cout << add('A', 2) << endl;
system("pause");
return 0;
}
内联函数(inline)
//内联函数
#include<iostream>
using namespace std;
template<class T1,class T2>
//返回值类型和形参a类型一样
inline T1 add(T1 a, T2 b)
{
cout << sizeof(T1) << "," << sizeof(T2) << "\t";
return a + b;
}
int main()
{
cout << add(10, 20) << endl;
cout << add(3.14, 5.98) << endl;
cout << add('A', 2) << endl;
system("pause");
return 0;
}