pointers.c

来自「指针和数组的使用方法例子」· C语言 代码 · 共 148 行

C
148 行
字号
//Pointers and arrays explained

#include <stdio.h>
#include <conio.h>

void cFunc(char*,char*);
void iFunc(int*);

int main()
{
	int iArray[10]  = {1,2,3,4,5,6,7,8,9,10};
	char cArray[12] = "Hello World";

	//An array name is itself a pointer.
	//It holds that address of the first element of an array.
	
	int  *p_int;		//A pointer to an integer
	char *p_char;		//A pointer to a character

	clrscr();

	puts("Pointers and arrays\n");

	//Initialize the pointers (make them point to something)
	p_int  = iArray;		
	p_char = cArray;
	
	//The following two statements are equal to the ones above
	//	p_int  = &iArray[0];
	//	p_char = &cArray[0];
	
	//So we can now say that 'iArray' is also a pointer to an integer		
	//and 'cArray' is a pointer to a character.

	//If you look at the prototypes above, you'll see that that cFunc(char*)
	//accepts a pointer to a char, and iFunc(int*) accepts a pointer to an int.
	//So the following two statements would be exactly the same,
	
	cFunc(cArray, p_char);
	//cArray holds the address of cArray[0].
	//Remember that an array name is a pointer to
	//the first element of that array.

//	cFunc(cArray, &cArray[0]);	   //p_char also holds the address of cArray[0]

	//and the following assignments are also equal

	iFunc(iArray);
//	iFunc(p_int);
//	iFunc(&iArray[0]);

	return getch();
}

void cFunc(char* cArray, char* p_char)
{
	char x[11] = "Test Array";

	//The following statement would not compile.
	//	array = &x;
	//Even though an array name is pointer, its value can never be changed
	//Array names are known as constant pointers.
	//If p_char was passed to this function from main(), then it would
	//work, because p_char is not declared as a constant pointer.
	
	//This statement would compile
	p_char = &x[2];		//Initializing p_char to point to third element
					//of x.
	puts("In cFunc()");
	puts("----------\n");
	printf("This is the string x      >>%s<<", x);
	printf("\nThis is the string cArray >>%s<<", cArray);
	printf("\nThis is the string p_char >>%s<<", p_char);

	//Why does the first printf statement print the whole string?
	//Because that's the way strings work. 'array' is a pointer
	//to the first element of the array. Because it's pointing to a char,
	//the compiler knows where the next element is by jumping to the next
	//byte (the size of a char). It also knows where the string ends because a NULL terminator
	//was added to the array. If you only allocate 10 bytes for the array x,
	//then the results might be erroneous.
	//If you print a string with printf, you would specify the address
	//of the first element in the argument list.
	//If you specify any other address of that array, then you would
	//print all the characters from that element to the NULL terminator,
	//as you can see in the second printf statement.

}

void iFunc(int* iArray)
{
	int y[10] = {1,2,3,4,5,6,7,8,9,10};

	int i, *ptr;

	puts("\n\nIn iFunc()");
	puts("----------");
	printf("\nTrying to print y as a string >>%s<<\n", y);

	//Why do these statements print funny characters.
	//Because they are not strings. Only char arrays can be strings.

	puts("\nTo print all the elements, you would need to print each individual");
	puts("element.");

	//There are two ways of doing this.

	printf("\n1. Print the elements of iArray directly\n");

	for(i=0; i < 10; i++)
		printf("%d ", iArray[i]);

	printf("\n2. Print them indirectly by using a pointer\n");
	ptr = iArray;

	for(i=0; i<10 ; ptr++,i++)	
		printf("%d ", *ptr);
	
	//When ptr was incremented, the compiler printed the next element of the
	//array. This is so because the compiler knows that it is a pointer to
	//an int. So the compiler will increment it by the size of the type that
	//the pointer is pointing to.
	
	//Remember to dereference the pointer to print the elements
	//by using the indirection operator, '*'.
}

//Try uncommenting some of the other statements in main to see the results.
//They should be exactly the same.

//To declare a pointer constant, just use the const keyword before the declaration
//eg. const int*  p_int;	
//	const char* p_char;
//Declaring them as constant pointers makes them exactly the same as
//array names, which are constant pointers to a specific type.

//Using array names as arguments to functions is good way to pass arrays to
//functions. 
//Use pointer arithmetic to manipulate strings by moving the NULL terminator
//and then appending other strings to it (very useful).   

//When working with pointers, try and visualise what is happening in memory,
//then try to estimate the results.
//If you can't, then draw a picture of what's happening in memory, you'll
//be able to trace memory leaks more efficiently. 

//Everything in C programming works on pointers, so if you don't understand
//them, then your ability to be a good coder will be weak.	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?