SoFunction
Updated on 2025-03-10

Detailed explanation of the role of C language* and & in operating linear tables

In the data structure linear table chapter, there are these operation methods (Operation):

/*Operation*/
 
Initlist(*L);/*Initialize the operation and create an empty linear table L*/
 
ListEmpty(L);/*Judge whether the linear table is an empty table. If the linear table is empty, the return value is true, otherwise it will return false*/ 
 
ClearList(*L);/*Clear the linear table*/
 
GetElem(L,i,*e);/* The value of the i-th position element in the nature table L is returned to e*/
 
LocateElem(L,e);/*Learn in the linear table L that is equal to the given value e. If the search is successful, return the element in the table number; otherwise, return 0 means failure*/
 
ListInsert(*L,i,e);/*Insert element e*/
  
ListDelete(*L,i,*e);/*Delete i position elements and use e to return its value*/
  
ListLength(L);/*Return the number of elements in linear table L*/

We can roughly divide the above functions into two categories. One type does not have * in the parameter list, such as: ListEmpty(L);

Another type has a (*) number in front of L or e. Why?

We can get inspiration from the following code

#include <>
#include <>
void test1(int a,int b)
{
    int c=0;
    c=b;
    b=a;
    a=c;
    printf("%d\n",a);
    printf("%d\n",b);
 
}
int main()
{
   int a=1;
   int b=2;
   test1(a,b);
    printf("%d\n",a);
    printf("%d\n",b);
}

The operation result is as follows. It can be seen that a and b exchange values ​​as formal parameters in test1 have no effect on the actual parameters in the main function.

2
1
1
2
 
Process returned 0 (0x0)   execution time : 0.118 s
Press any key to continue.

Let's make a little change to the above code to observe the results

#include <>
#include <>
void test1(int *a,int *b)
{
 
    int c=0;
    c=*b;
    *b=*a;
    *a=c;
        printf("%d\n",&a);
    printf("%d\n",&b);
 
}
int main()
{
   int a=1;
   int b=2;
   test1(&a,&b);
    printf("%d\n",a);
    printf("%d\n",b);
    return 0;
}

6422000
6422008
2
1
 
Process returned 0 (0x0)   execution time : 0.033 s
Press any key to continue.

Obviously, the test1() method this time finally has an impact on the actual parameters of the main function, because the current test() method directly modifyes the data domains of the storage unit addresses 6422000 and 6422008.

When we continue to operate the linear table, we will suddenly realize it. For example: ListInsert(*L,i,e), if * is not added, then L will always remain in the function: ListInsert(L,i,e), and no actual change can be made. Only by adding * can L be brought out. We only need to participate in the form when defining the method, and to participate in the form when calling the method to obtain the expected effect

Thinking: Another solution: Can you define a List Insert() method so that the return value is the modified linked list L?

List Insert(List L,int i,ElememtType e)
{
    /*
     Here is the insertion operation of the linked list
                             */
    return List;//Note that the returned List value has been modified}

However, this also has a disadvantage. When the insertion occurs in the header, L must point to the new node, otherwise the address will still point to the original header node. There is also a solution, which is to insert a meaningless head node b before the first node a of the linked list, so that every time you want to insert an element at the head of the table, you just need to insert it between a and b.

Note: In C language, *a points to the data field of a, and & is the address of a read.

This is the article about the detailed explanation of the role of C language * and & in operating linear tables. For more related C language linear tables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!