C language -- the relationship between pointer and string

People suffer because you are growing up. --------magic_guo

In memory, strings are stored in the form of arrays. In C programs, strings are stored in character arrays. Therefore, if you know the relationship between arrays and pointers, you can better understand the relationship between pointers and strings.
1. For string reference method in C language:
① Reference string by array name

# include<stdio.h>


// Reference string by array name
int main(void)
{
	char string[] = "I love China!";
	printf("%s\n", string);
	printf("%c\n", string[7]);

	return 0;
}

// %s can input and output a string as a whole

/* You can output a string by using the character array name or the character pointer variable,
For a numerical array, you can't try to output all its elements with the array name. You can only input or output the elements of the numerical array one by one */

② Reference string by pointer variable

int main(void)
{
	char * string;
	string = "I love China!";
	printf("%s\n", string);


	return 0;
}

// The difference between referencing a string constant through a pointer variable and an array name
// Initialization:
//	char * string = "I love China!"; equivalent to
	// char * string;
	// string = "I love China!";

// Use array name
// string[] = "I love China!";
// Not equivalent to
	// char string[];
	// string = "I love China!";
	// Array can assign initial value to each element at the time of definition, but it cannot assign value to all elements in character array as a whole with assignment statement.

Example:

# include<stdio.h>

// Copy string a to string b and output string b

// Common method
int main(void)
{
	char a[] = "I am a student!", b[20];
	int i;
	for (i=0; a[i]!='\0'; i++)
	{
		b[i] = a[i];
	}
	b[i] = '\0';
	printf("a = %s\n", a);
	printf("b = %s\n", b);
	return 0;
}


// Normal method embedded pointer representation
int main(void)
{
	char a[] = "I am a student!", b[20];
	int i;
	for (i=0; *(a+i)!='\0'; i++)
		*(b+i) = *(a+i);
	b[i] = '\0';
	printf("a = %s\n", a);
	printf("b = %s\n", b);	

	return 0;
}

// Using pointer variables
int main(void)
{
	char a[] = "I am a student!", b[20], *p, *q;
	p = a, q = b;
	for (; *p!='\0'; p++,q++)
		*q = *p;
	*q = '\0';
	printf("a = %s\n", a);
	printf("b = %s\n", b);	

	return 0;
}

2. Character pointer as function parameter
① Using character array name as parameter and argument of function

# include<stdio.h>

// Define a copy string function to realize the function of string copy
// Using character array name as parameter and argument of function

int main(void)
{
	void copy_string(char from[], char to[]);
	char a[] = "I am a teacher!";
	char b[] = "You are a farmer!";
	printf("string a=%s\nstring b=%s\n", a, b);
	printf("copy string a to string b:\n");
	copy_string(a, b);
	printf("string a=%s\nstring b=%s\n", a, b);


	return 0;
}

void copy_string(char from[], char to[])
{
	int i = 0;
	while (from[i]!='\0')
	{
		to[i] = from[i];
		i++;
	}
	to[i] = '\0';
}

//You can change the loop body of the copy string function to:
//while (from[i]!='While (from [i]! = '\ 0'') because ASCII code is used for operation inside the computer, the ASCII code of '\ 0' corresponds to 0

② Using character pointer variables as parameters and arguments of functions

# include<stdio.h>

// Using character pointer variables as parameters and arguments of functions

int main(void)
{
	void copy_string(char * from, char * to);
	char a[] = "I am a teacher!";
	char b[] = "You are a farmer!";
	char * p, *q;
	p = a, q = b;
	printf("string a=%s\nstring b=%s\n", a, b);
	printf("copy string a to string b:\n");
	copy_string(p, q);
	printf("string a=%s\nstring b=%s\n", a, b);

	return 0;
}

void copy_string(char * from, char * to)
{
	while ((*to=*from)!='\0')
	{
		to++, from++;
	}
}

3. Comparison between character variable and character array
① The character array consists of several elements, each of which stores a character, while the character pointer variable stores the address (the address of the first character in the string), not the whole string.
② Different assignment methods: the character pointer variable can be assigned, but the array name cannot be assigned
Reason: Although the array name represents the address, it is a constant, and the value of the constant cannot be changed; however, the array can assign initial values to each element during definition.
③ The value of each element in the character array can be changed, but the content of the string constant pointed to by the character pointer variable cannot be replaced.
Example:

# include<stdio.h>

int main(void)
{
	char a[] = "I am a teacher!";
	char * b = "You are a farmer!";
	a[2] = 'l';						// Legal, l replaces the original value a of array element a[2]
	b[2] = 'l';						// Illegal, string constant cannot be changed

	return 0;
}
Published 13 original articles, won praise 1, visited 313
Private letter follow

Tags: ascii C

Posted on Sun, 15 Mar 2020 05:54:16 -0400 by cl_stef