This is the 14th 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!
Creation and initialization of a one-dimensional array
Array creation
An array is a collection of elements of the same type. Create an array:
type_t arr_name [const_n]; //type_t is the element type of an exponential group. //const_n is a constant expression that specifies the size of an arrayCopy the code
Note: array creation, [] must be given a constant, do not use variables.
Initialization of an array
Initialization of an array is the creation of an array while giving its contents some reasonable initial values (initialization). Look at the code:
Int arr1 [10] = {1, 2, 3}; Int arr2 [] = {1, 2, 3, 4}; Int arr3[5]= {1,2,3,4,5}; char arr4[3]= {'a',98,'c'}; char arr5[]= {'a','b','c'}; char arr6[]="abcdef";Copy the code
Arrays are initialized when created without specifying a definite size for the array. The number of elements in the array is determined by what is initialized. But for the following code to distinguish, how is memory allocated
char arr1[]="abc"; Char arr2[3]= {'a','b','c'}; // The end of the array does not hold \0Copy the code
The use of one-dimensional arrays
We introduced an operator for arrays earlier: [], the subscript reference operator. It’s just an array access operator.
#include <stdio.h> int main() { int arr[10]= {0}; Int sz=sizeof(arr)/sizeof(arr[0]); // Assigns a value to the contents of an array, which is accessed using subscripts starting at 0. So: int I =0; For (I =0; i<10; i++) { arr[i]=i; } // Print the contents of array for(I =0; i<10; ++i) { printf("%d ",arr[i]); } return 0; }Copy the code
Conclusion:
1. Arrays are accessed using subscripts, which start at 0. 2. The size of the array can be calculated. int arr[10]; int sz=sizeof(arr)/sizeof(arr[0]);Copy the code
Storage of one-dimensional arrays in memory
int main() { int arr[10]= {0}; int i=0; for(i=0; i<sizeof(arr)/sizeof(arr[0]); ++i) { printf("&arr[%d] = %p\n",i,&arr[i]); } return 0; }Copy the code
Conclusion: Arrays are contiguous in memory. As the index of the array increases, the address of the element increases regularly. It follows that
The difference between the address of an element in an array and the address of an element in a stack
First of all, we need to know that local variables are stored in the stack area, and in the stack area to open up space, is the use of high address space, in the use of low address space, that is, the first to create variables in the high address!
Int main() {int a = 0; int b = 0; printf("&a = %p\n",&a); printf("&b = %p\n", &b); printf("&a - &b = %d\n", &a - &b); return 0; }Copy the code
So there’s a question, right? Isn’t the address of the elements in the array above from lowest to highest? Isn’t that inconsistent?
Answer: An array is a space created once and stored later!
int main()
{
int arr[10] = { 0 };
printf("&arr = %p\n", &arr);
printf("&arr + 1 = %p\n", &arr+1);
return 0;
}
Copy the code
Execution Result:
Array address +1 skips space (the size of space is determined by the size of the array)
The address of the array is actually a pointer. The step size of the pointer +1 depends on the size of the type to which the pointer points
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!