What is the difference between *p and p in C language

青灯夜游
Release: 2022-11-29 18:03:10
Original
47773 people have browsed it

Difference: 1. The meanings are different. "*p" represents the content stored in the memory address pointed by this pointer. "p" represents the name of a pointer variable, which refers to the memory address pointed by this pointer variable. . 2. The output formats are different. "*p" usually outputs a variable or constant of the same type as the pointer. "p" outputs a hexadecimal number and the address of a pointer. 3. The functions are different. "*p" tells the program to go to that address to retrieve data, and "p" is used to store the address.

What is the difference between *p and p in C language

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

Understand *p, p

In C language, *p and p are commonly used A pointer represents a pointer variable.

If you want to use pointers, you need to first understand addresses and data: you can imagine that there are many boxes, each box has a corresponding number, that number is called "address", and the stuff in the box is called "data" .

p is a pointer variable, used to store the address. You can think of it as the number of the box mentioned above. "*" is the dereference operator. You can think of it as opening the box. p means opening p. No. box and take out the data inside.

To put it simply, remember, p stores the address, and p tells the program to go to that address to retrieve data.

In C language, the * sign has three uses, namely:

  • The multiplication sign is used for multiplication operations, such as 5*6.

  • Declare a pointer, used when defining pointer variables, such as int *p;.

  • Indirect operator, obtains the value in the memory pointed to by the pointer, such as printf("%d",*p);.

The difference between *p and p in C language

1. The meanings are different.

*p represents the content stored in the memory address pointed by this pointer.

p represents the name of a pointer variable, which refers to the memory address pointed to by the pointer variable.

2. The output format is different

*p is generally a variable or constant that is consistent with the pointer type.

pThe output is a hexadecimal number and the address of a pointer.

3. Different functions

#*pLet the program go to that address to retrieve the data.

p stores the address.

Example:

#include <stdio.h>

int main(void){ 

int x=3;

int *p,*q;

p=&x,q=&x;

printf("%d\n",*p++);

printf("%d\n",(*q)++);

printf("%d\n",x);

}
Copy after login

The output result is: 3, 3, 4;

What is the difference between *p and p in C language

Explanation:

What is the difference between *p and p in C language

Expanded knowledge: *p and **p Difference

int *p: Level one pointer, indicating that the address pointed to by p stores an int type value

int * *p: Second-level pointer, indicating that the address pointed to by p stores a pointer to type int (that is, the address pointed to by p stores a first-level pointer to int)

For example:

int i=10; //定义了一个整型变量
int *p=&i; //定义了一个指针指向这个变量
int **p1=&p; //定义了一个二级指针指向p指针
Copy after login

Then the way to extract the value of 10 is:

printf("i=[%d]\n",*p);
printf("i=[%d]\n",**p1);
Copy after login

What is the difference between *p and p in C language

##Related recommendations: "

C Video Tutorial"

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