Home > Backend Development > C++ > body text

Analysis and comparison of * and & operators in C language

WBOY
Release: 2024-04-03 14:24:01
Original
385 people have browsed it

In C language, the asterisk (*) operator is used to dereference a pointer variable and obtain the value of the variable pointed to; the address operator (&) is used to obtain the variable address. The specific comparison is as follows: Dereference pointer: *Get address: & pointer multiplication: *Bit operation: &

Analysis and comparison of * and & operators in C language

* and & operator analysis and comparison in C language

In C language, the asterisk (*) operator and the address operator (&) operator are two important operators, which are often used for pointer operations and memory management. This article will deeply analyze their uses and differences, and provide practical cases to illustrate.

The asterisk (*) operator

  • The asterisk operator is used to dereference a pointer variable and obtain the value of the variable it points to.
  • Syntax: *Pointer variable
  • For example:

    • int *ptr = &i;
    • printf("%d", *ptr); //Print the value of i

Address operator (&) operator

  • Address operation The operator is used to obtain the address of a variable and returns a pointer to the variable.
  • Syntax: &Variable name
  • For example:

    • int i = 10;
    • int *ptr = &i;
    • printf("%p", ptr); // Print the address of i

Comparison operator

The following is a comparison of the * and & operators in different scenarios:

OperatorScenarioFunction
*Dereference pointerGet the value pointed to the variable
& Get the addressGet the address of the variable
*Pointer multiplicationCalculate the size of the memory space pointed by the pointer
&Bitwise operation (logical AND)Perform logical AND operation on two integers

Practical Case

The following is a practical case that demonstrates how the * and & operators are used for pointer operations:

#include 

int main() {
    int i = 10;
    int *ptr = &i; // ptr指向i

    // 通过解引用指针获取i的值
    printf("i的值:%d\n", *ptr);

    // 通过取地址获取i的地址
    printf("i的地址:%p\n", &i);

    // 通过指针乘法计算ptr指向的内存空间大小
    int size = sizeof(*ptr);
    printf("ptr指向内存空间大小:%d字节\n", size);

    // 使用指针进行递增
    (*ptr)++; 

    // 打印递增后的i值
    printf("递增后的i:%d\n", i);

    return 0;
}
Copy after login

Output:

i的值:10
i的地址:0x7ffe5247ef4c
ptr指向内存空间大小:4字节
递增后的i:11
Copy after login

The above is the detailed content of Analysis and comparison of * and & operators in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!