The best C Pointer Tutorial In 2024, In this tutorial you can learn What is a pointer?,How to use the pointer?,The C NULL pointer,C pointer Detailed,

C Pointer

Learning C language pointer easy and fun. Through a pointer, you can simplify the implementation of some of the C programming tasks, there are some tasks, such as dynamic memory allocation, no pointer is unenforceable. So, I want to become a good C programmer, learning pointers are necessary.

As you know, each variable has a memory location, each memory location defines the address-of operator access can use the hyphen (&), which represents an address in memory. Consider the following examples, which will define the output variable address:

#include <stdio.h>

int main ()
{
   int  var1;
   char var2[10];

   printf("var1 ๅ˜้‡็š„ๅœฐๅ€๏ผš %x\n", &var1  );
   printf("var2 ๅ˜้‡็š„ๅœฐๅ€๏ผš %x\n", &var2  );

   return 0;
}

When the above code is compiled and executed, it produces the following results:

var1 ๅ˜้‡็š„ๅœฐๅ€๏ผš bff5a400
var2 ๅ˜้‡็š„ๅœฐๅ€๏ผš bff5a3f6

By way of example above, we understand what a memory address and how to access it. Let's look at what is a pointer.

What is a pointer?

A pointer is a variable whose value is the address of another variable, namely, the direct address of the memory location.Before Like other variables or constants, you must use a pointer memory address of other variables, it is declared. The general form of a pointer variable declaration is:

type *var-name;

Here, type is a pointer to the base type, it must be a valid C data type,var-name is the name of the pointer variable. Is used to declare a pointer asterisk * multiplication use the asterisk is the same. However, in this statement, the asterisk is used to specify a variable is a pointer. The following is a valid pointer declaration:

int    *ip;    /* ไธ€ไธชๆ•ดๅž‹็š„ๆŒ‡้’ˆ */
double *dp;    /* ไธ€ไธช double ๅž‹็š„ๆŒ‡้’ˆ */
float  *fp;    /* ไธ€ไธชๆตฎ็‚นๅž‹็š„ๆŒ‡้’ˆ */
char   *ch     /* ไธ€ไธชๅญ—็ฌฆๅž‹็š„ๆŒ‡้’ˆ */

All pointers actual data type of the value, whether it is an integer, float, string, or other data types are the same, is a hexadecimal number represents a long memory address. The only difference between the different types of data pointer, the pointer is a variable or constant data types.

How to use the pointer?

Will frequently use the pointer when the following actions: define a pointer variable, the variable address assigned to the pointer, access the value of the pointer variable available addresses. This is done by using the unary* operator to return the value of the variable is located in the operand address specified.The following examples relate to these actions:

#include <stdio.h>

int main ()
{
   int  var = 20;   /* ๅฎž้™…ๅ˜้‡็š„ๅฃฐๆ˜Ž */
   int  *ip;        /* ๆŒ‡้’ˆๅ˜้‡็š„ๅฃฐๆ˜Ž */

   ip = &var;  /* ๅœจๆŒ‡้’ˆๅ˜้‡ไธญๅญ˜ๅ‚จ var ็š„ๅœฐๅ€ */

   printf("Address of var variable: %x\n", &var  );

   /* ๅœจๆŒ‡้’ˆๅ˜้‡ไธญๅญ˜ๅ‚จ็š„ๅœฐๅ€ */
   printf("Address stored in ip variable: %x\n", ip );

   /* ไฝฟ็”จๆŒ‡้’ˆ่ฎฟ้—ฎๅ€ผ */
   printf("Value of *ip variable: %d\n", *ip );

   return 0;
}

When the above code is compiled and executed, it produces the following results:

Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20

The C NULL pointer

At the time of variable declarations, if not the exact address can be assigned, assign a NULL value for the pointer variable is a good programming practice. Fu NULL pointer value is called anull pointer.

A NULL pointer is defined in the standard library zero constants. Consider the following program:

#include <stdio.h>

int main ()
{
   int  *ptr = NULL;

   printf("ptr ็š„ๅ€ผๆ˜ฏ %x\n", ptr  );
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

ptr ็š„ๅ€ผๆ˜ฏ 0

On most operating systems, the program does not allow access to memory address 0, because the memory is reserved for the operating system. However, the memory address 0 has a special significance, it indicates that the pointer does not point to an accessible memory location. But according to the convention, if the pointer contains a null value (zero value), it is assumed that it does not point to anything.

To check for a null pointer, you can use the if statement, as follows:

if(ptr)     /* ๅฆ‚ๆžœ p ้ž็ฉบ๏ผŒๅˆ™ๅฎŒๆˆ */
if(!ptr)    /* ๅฆ‚ๆžœ p ไธบ็ฉบ๏ผŒๅˆ™ๅฎŒๆˆ */

C pointer Detailed

In C, there are many pointers related concepts that are very simple, but very important. Listed below are some important concepts associated with the pointer C programmers must be clear:

ๆฆ‚ๅฟตๆ่ฟฐ
ๆŒ‡้’ˆ็š„็ฎ—ๆœฏ่ฟ็ฎ— ๅฏไปฅๅฏนๆŒ‡้’ˆ่ฟ›่กŒๅ››็ง็ฎ—ๆœฏ่ฟ็ฎ—๏ผš++ใ€--ใ€+ใ€-
ๆŒ‡้’ˆๆ•ฐ็ป„ ๅฏไปฅๅฎšไน‰็”จๆฅๅญ˜ๅ‚จๆŒ‡้’ˆ็š„ๆ•ฐ็ป„ใ€‚
ๆŒ‡ๅ‘ๆŒ‡้’ˆ็š„ๆŒ‡้’ˆ C ๅ…่ฎธๆŒ‡ๅ‘ๆŒ‡้’ˆ็š„ๆŒ‡้’ˆใ€‚
ไผ ้€’ๆŒ‡้’ˆ็ป™ๅ‡ฝๆ•ฐ ้€š่ฟ‡ๅผ•็”จๆˆ–ๅœฐๅ€ไผ ้€’ๅ‚ๆ•ฐ๏ผŒไฝฟไผ ้€’็š„ๅ‚ๆ•ฐๅœจ่ฐƒ็”จๅ‡ฝๆ•ฐไธญ่ขซๆ”นๅ˜ใ€‚
ไปŽๅ‡ฝๆ•ฐ่ฟ”ๅ›žๆŒ‡้’ˆ C ๅ…่ฎธๅ‡ฝๆ•ฐ่ฟ”ๅ›žๆŒ‡้’ˆๅˆฐๅฑ€้ƒจๅ˜้‡ใ€้™ๆ€ๅ˜้‡ๅ’ŒๅŠจๆ€ๅ†…ๅญ˜ๅˆ†้…ใ€‚
C Pointer
10/30