The difference between *a and a in c language

下次还敢
Release: 2024-05-02 17:18:19
Original
847 people have browsed it

In C language, a is a pointer to the memory address of variable a, and a is an ordinary variable. a stores the value in a, and a stores the value of itself. a accesses the value pointed to by dereferencing it, while a accesses its own value directly. &a returns the memory address of a, while a returns the memory address pointing to the value.

The difference between *a and a in c language

The difference between *a and a in c language

In c language, *a and a are two different concepts. The main differences between them are as follows:

  • Type: *a is a pointer toa is a pointer to the memory address of a variable; and a is an ordinary variable.
  • Value: The value of *a is the value stored in the a variable; and the value of a itself.
  • Operation: *a can access the value it points to through the dereference operator (*); and aOnly its own value can be accessed directly.
  • Address: &a returns the memory address of the a variable; and *a returns the a variable The memory address of the stored value.

Detailed explanation:

  • Pointer (*a): Pointer is a data type that points to the memory address of a variable . *a represents a pointer to the memory address of variable a. The value stored at that memory address can be accessed through the dereference operator (*).
  • Variable (a): A variable is a named memory location that can store a value. a represents an ordinary variable, which stores a specific value.
  • Dereference: The dereference operator (*) is used to access the value pointed to by the pointer. For example, *a means taking the value stored in the memory address pointed to by the a pointer.
  • Address operator: Address operator (&) is used to obtain the memory address of a variable. For example, &a returns the memory address of the a variable.

Example:

<code class="c">int a = 10;
int *p = &a;

printf("变量 a 的值:%d\n", a);
printf("指向 a 的指针 p 的值:%d\n", *p);</code>
Copy after login

Output:

<code>变量 a 的值:10
指向 a 的指针 p 的值:10</code>
Copy after login

In this example, a is a stored value of 10 The variable, p is a pointer to the memory address of the a variable. *p Dereference the pointer and return the value stored in the a variable, which is 10.

The above is the detailed content of The difference between *a and a 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