/* ============================================================================ name:testpointer.c Author: LF Version:Copyright:Your Copyright Notice Description: Getting Started with pointers and understanding when a program loads memory, the system allocates memory for both variables and functions, so each variable and function has its corresponding memory address. So in C language there is a very common operation & is used to take the address; The * is the operation used to: take the data from the memory unit corresponding to the address of the memory. That is, the operation according to the address extract summary: int num=9527; int *p=# 1 Since the & operation is to take the address of the variable. For example, the statement &num,num is a variable &num is the address of the variable is taken. Then the *p operation is similar to the principle. * The operation is to take out the contents according to the address, such as statement *p,p is an address; *p takes out the data stored in the address. So p and &num are equivalent!!!! 2 Why is there an int modifier in front of the *p? Because P is just a start address, here Int tells the program to access the range from the start address of P to the address (length of the p+int type) as the end. ============================================================================ * * #include <stdio.h> #include <stdlib.h>int Main (void) {Testpointer ();p ointersize (); return exit_success;} /** * &num takes the address of the variable. * In fact &num is also a pointer, it points to Num. See: * printf ("* (&num) =%d,num=%d\n", * (&num), num); * * int num=9527; * INT *p=# * To be clear: * &num is a constant, but p is a pointer variable that can store an address */voidTestpointer () {int num=9527;int *p=#//pointer p itself in memory address printf ("&p=%x\n", &p);p rintf ("===========\n");// The address of NUM is saved in P. So p and &num are equivalent!!!! printf ("p=%x,&num=%x\n", P,&num);p rintf ("===========\n");//*p, get the saved address in P and remove the value saved in the address.//So *p and num are equivalent to printf (" Num=%d,*p=%d\n ", num,*p);p rintf (" ===========\n ");/** * Top Summary: * 1 Since the & operation is to take the address of the variable. * For example, the statement &num,num is a variable &num is the address of the variable is taken. * Then *p operation is a similar principle. * 2 * operation is to take out the contents according to the address * For example, the statement *p,p is an address; *p takes out the data stored in the address. * So the above example can see that P and &num are equivalent!!!! *//Because &num is an address, it is actually a pointer, pointing to num//so use * (&num) to remove the value pointed to by the pointer.//* (&num) and num are equivalent printf ("* (&num) =%d,num=% D\n ", * (&num), num); printf ("===========\n");} /** * The size of the pointer. * The pointer is an address, so it's all 32. * Int *int_pointer * char *char_pointer; * Double *double_pointer; * They are all 32-bit and represent a starting address!!! */void pointersize () {int *int_pointer;char *char_pointer;double *double_pointer;printf ("Int_pointer size=%d\n", sizeof (Int_pointer));p rintf ("Char_pointer size=%d\n", sizeof (Char_pointer));p rintf ("Double_pointer size=%d\n", sizeof (DouBle_pointer));p rintf ("===========\n");}
Getting started and understanding of pointers