Talking about pointer advanced-character pointer

Pointer advanced-character pointer

General use:

int main()
{
	char ch = 'w';
	char *pc = &ch;
	printf("%c\n", *pc);//*pc = 'w';

	return 0;
}

Method Two:

int main()
{
	char* pstr = "hello bit.";
	printf("%s\n", pstr);

	return 0;
}

The second method is to put the first address of the constant string in the pointer variable pstr

Interview questions

#include <stdio.h>
int main()
{
	char str1[] = "hello bit.";
	char str2[] = "hello bit.";
	char *str3 = "hello bit.";
	char *str4 = "hello bit.";
	if (str1 == str2)
		printf("str1 and str2 are same\n");
	else
		printf("str1 and str2 are not same\n");

	if (str3 == str4)
		printf("str3 and str4 are same\n");
	else
		printf("str3 and str4 are not same\n");

	return 0;
}

Analyzing
  str1 and str2 is to store two identical strings in two arrays at two different addresses. Therefore, when comparing the addresses of the first elements of the arrays, the results are different. And str3 and str4 point to the same string of strings in the constant area, and the first address of this string is stored in it. Therefore, the comparison of str3 and str4 is equal.

Guess you like

Origin blog.csdn.net/weixin_43580319/article/details/111794556