This is the 21st day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”


Recently, I want to review C language, so I will update an article about C language in nuggets every day! Freshmen who are just learning C, and those who want to review C /C++, don’t miss it! Solid foundation, slow down is fast!

5. Concepts of addresses, arrays, and Pointers

Q: Can addresses compare sizes?
Addresses can be sized. Addresses are essentially16A numeric pointer in base stores an address, which can be treated as a piece of data and therefore can be compared in sizeCopy the code

Pointer to array relationship?

Pointers have nothing to do with arrays

Array:

Array: a contiguous space containing elements of the same type. The size, type, and number of elements of the array are relatedCopy the code
Such as:1.int arr[10]; The array arr is of type:int[10] Drop the name of the array to get the type of the array2.int arr2[5] array arr2 is of type:int[5Arr and arR2 are different arraysint(*pa)[10] = &arr;  //pa is an array pointer
   int(*pa2)[5] = &arr2; 
Copy the code

Remove the array name -> array type

Remove the array name and number of elements -> The element type of the array


Understanding array names

The array name is the address of the first element

But there are two exceptions:

1. Sizeof – the array name represents the entire array and measures the sizeof the entire array in bytes

2.& Array name – Retrieves the address of the entire array

& Array name +1: skips the entire array


Pointer:

Pointer: is a variable that holds an address

A pointer is a variable, and a variable has an address. So pointer variables also have their own addresses

Pointer variable size: 4byte (32-bit platform) 8byte (64byte platform)


int a = 10;
int* p =&a;
// * p indicates a pointer, and int indicates that p points to an int type
int** pp = &p;	//pp is a secondary pointer
Copy the code

6. Pointer arrays

Pointer arrays are arrays

int main(a)
{
    int a = 10;
    int b = 20;
    int c = 30;
    int* arr[3] = {&a,&b,&c};
    int i = 0;
    for(i = 0; i <3; i++) {printf("%d\n",*(arr[i]));
        // The parentheses can also be left out, because [] has a higher priority than *
        printf("%d\n",*arr[i]);
    }
    return 0;
}
Copy the code

7. Pointers and structures

About structures

Structs can be defined inside the main function, but are not recommended


Structure type definitions do not take up space the actual structure variables do take up space ****


A global structure, uninitialized, whose variables are initialized to 0 by default

Variables in static zones are not initialized and default to 0 **

Static: static, global variables


typedef struct Book
{
	char name[20];
	float price;
	char author[20];
}Stu; b1, b2;			//b1,b2 are global variables
//typedef rename type name Stu ==struct Book type name

/ / value
void Print(Stu b1)
{
	printf("%f %s %s\n", b1.price, b1.author, b1.name);
}
// address - structure pointer receives
void Print2(Stu* b1)
{
	printf("%f %s %s\n", b1->price, b1->author, b1->name);
}
int main(a)
{
	struct Book b3 = { "Mango".19.0."Lemon" };	//b3 is a local variable stored on the stack

	Print(b3);/ / value
	Print2(&b3);	/ / reference
	return 0;
}
Copy the code

For the above two ways of passing structure: passing value, passing address

Address transfer: only 4 bytes are passed, which wastes little space

Transfer value: directly open up a space with the same size of the original structure, waste of space, will lead to stack problems

So we prefer addressing


That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!