其他分享
首页 > 其他分享> > day-13 代码

day-13 代码

作者:互联网

获取系统时间
int main()
{
	time_t now;
	struct tm local;
	while (1)
	{
		time(&now);
		localtime_s(&local, &now);
		cout.fill('0');
		cout << local.tm_hour << ":";
		cout.fill('0');
		cout << local.tm_min << ":";
		cout.fill('0');
		cout << local.tm_sec << '\r';
		Sleep(1000);
	}

	return 0;
}
指针和二维数组
int main()
{
	int a[][3]{ {5, 1, 2}, {3, 4, 0}, {8, 9, 7} };
	int sum = 0;
	int* p = &(a[0][0]);

	int nlen = sizeof(a[0]) / sizeof(int);
	cout << *a << ", p = " << p << ", p+1 = "<< *(p+1) << ", nlen = " << nlen << endl;
	int i, j, t;
	for (i = 0; i < 3; i++)
	{
		cout << "*(a+i): " << *(a + i) << ", &(a[i][0]/a[i]): " << &a[i] << endl; //*(a+i)是指第i行的地址
		cout << "line: " << i << ", p[i]:" << p[i] << endl;

		p = a[i];
		for (j = 0; j < 3; j++)
		{
			t = *(p + j);// a[i][j];
			//t = *(p++);
			sum += t;
			cout << "t=" << t << ", &t: " << &t
				<< ", &a[i][j]: " << &a[i][j] << endl;
		}
		cout << endl;
	}
	cout << sum << endl;

	cout << endl;
	return 0;
}
/*错误
int main()
{
	int a[][3]{ {5, 1, 2}, {3, 4, 0} };
	int sum = 0;
	int* p = a[0];
	cout << *a << "  " << p << endl;
	int i, j, t;
	for (i = 0; i < 2; i++)
	{
		cout << "line: " << i << ", p[i]:" << p[i] << endl;
		for (j = 0; j < 3; j++)
		{
			//t = *(p + i) + j; a[i][j];
			t = *(p++);
			sum += t;
			cout << "t=" << t << ", &t: " << &t
				<< ", &a[i][j]: " << &a[i][j] << endl;
		}
		cout << endl;
	}
	cout << sum << endl;

	cout << endl;
	return 0;
}*/
二维字符串指针数组
int main()
{
	const char* ch[] = { "china", "c", "string" };
	const char** p = ch;
	cout << **p << "  " << **(p + 1) << "  " << **(p + 2)
		<< endl;
	cout << *p << "  " << *(p + 1) << "  " << *(p + 2)
		<< endl;


	cout << endl;
	return 0;
}
/*output
c  c  s
china  c  string
*/
字符串数组和指针赋值
int main()
{
	char ch1[] = { 'h', 'e', 'l', 'l', 'o' };
	char ch2[] = "hello";
	char* pch1 = ch1;
	//strcpy_s(pch1, ch1);
	char* pch2 = ch2;
	cout << "sizeof(ch1):" << sizeof(ch1) << ",ch1:" << ch1 << endl;
	cout << "sizeof(ch2):" << sizeof(ch2) << ",ch2:" << ch2 << endl;
	cout << "sizeof(pch1):" << sizeof(pch1) << ",pch1:" << pch1 << endl;
	cout << "sizeof(pch2):" << sizeof(pch2) << ",pch2:" << pch2 << endl;



	cout << endl;
	return 0;
}
/*output
sizeof(ch1):5,ch1:hello烫烫烫炭g{婶
sizeof(ch2):6,ch2:hello
sizeof(pch1):4,pch1:hello烫烫烫炭g{婶
sizeof(pch2):4,pch2:hello
*/
字符串指针数组
int main()
{
	char ch1[] = { 'h', 'e', 'l', 'l', 'o' };
	char ch2[] = "hello";
	char* pch1 = ch1;
	*(pch1 + 5) = '\0';
	//strcpy_s(pch1, ch1);
	char* pch2 = ch2;
	cout << "sizeof(ch1):" << sizeof(ch1) << ",ch1:" << ch1 << endl;
	cout << "sizeof(ch2):" << sizeof(ch2) << ",ch2:" << ch2 << endl;
	cout << "sizeof(pch1):" << sizeof(pch1) << ",pch1:" << pch1 << endl;
	cout << "sizeof(pch2):" << sizeof(pch2) << ",pch2:" << pch2 << endl;



	cout << endl;
	return 0;
}
/*output
//报错
sizeof(ch1):5,ch1:hello
sizeof(ch2):6,ch2:hello
sizeof(pch1):4,pch1:hello
sizeof(pch2):4,pch2:hello
*/
字符串常量指针
int main()
{
	char ch1[] = "hello";
	char* pch1 = ch1;
	const char* pch2 = "hello"; //phc2和pch3都指向常量存储区的同一个地址
	const char* pch3 = "hello";

	cout << (pch2 - pch3) << endl;


	cout << endl;
	return 0;
}
/*output
0
*/
两次循环输出字符串指针数组
int main()
{
	const char* point[] = { "one", "two", "string" };
	int i = 0;
	while (*point[2]!='\0')
	{
		i++;
		cout << *point[2]++ << "  ";
		cout << i << endl;
	}
	/*while (*point[2] != '\0')  //此时*point[2]为空,不执行
	{
		i++;
		cout << *point[2]++ << "  ";
		cout << i << endl;
	}*/
    while (*point[2] == '\0')
	{
		i++;
		cout << *point[2]++ << "  ";
		cout << i << endl;
	}


	cout << endl;
	return 0;
}
/*
s  1
t  2
r  3
i  4
n  5
g  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
*/
拷贝char to char*
int main()
{
	char arr[] = "there is a string";
	char* point;
	char arr2[30] = "";
	int i = 0, arrLen = 0;
	point = arr2;
	while (arr[arrLen]!='\0')
	{
		arrLen++;
	}
	for (i; i < arrLen; i++)
	{
		*point = arr[i];
		cout << *point;
		point++;
		/**point++ = arr[i];
		cout << *point;*/
	}
	cout << endl;
	cout << arr2 << endl;

	cout << endl;
	return 0;
}
/*output
there is a string
there is a string

/*

there is a string
*/

标签:13,cout,pch1,int,代码,char,ch1,main,day
来源: https://www.cnblogs.com/lzq0820Time/p/15377373.html