C language array type, definition, initialization, traversal explanation, array Initialization

Source: Internet
Author: User
Tags array definition

C language array type, definition, initialization, traversal explanation, array Initialization
I. array basics 1. array type:

Element type name [number of elements]

2. array definition:

Element type name array name [number of elements] defines an array containing ten int elements. The array name is arr:

Int arr [10]; // int indicates the element type. It is not an array type, but an array is of the int [10] type.
3. array Initialization

Array Initialization is divided into full initialization and partial initialization.

Int arr [5] = {1, 2, 3, 4, 5}; // fully initialized. // Full initialization assigns a value to each element of the array. Int arr2 [5] = {1, 2}; // partial initialization. // Some elements are initialized to assign values to some elements of the array, while others are random values. Int arr3 [5] ={}; // The array is set to zero, that is, the values of all elements are zero.
4. array Traversal

The compiler does not check whether the array is out of bounds to Improve compilation efficiency.

Array traversal Input

Void scfarr (int arr [], int len) {int I = 0; for (I = 0; I
 
  
Array traversal output
  
Void prtarr (int arr [], int len) {int I = 0; for (I = 0; I
   
    
5. array name as left and right: array name arr as right represents the first address of the first element. & Arr indicates the first address of the array, which is equal to the value of arr and has different meanings. (& arr + 1) indicates the last item of the entire array. Left value: the left value represents the memory space corresponding to the variable. The array is a whole and no matching value corresponds to its space. Therefore, the array cannot be used as the left value, but can only be initialized. 6. If the array name is called as a parameter, the number of elements must be added. The array name is the first address of the array element as a parameter. When the array name is used as a parameter, the size of the array is lost and degrades to a pointer. The sub-function's operation on the array changes the value of the element, but does not change the address of the element. The changed address in the sub-function is destroyed as the form parameter at the end of the sub-function. 2. Array application 1. Bubble Sorting
    
Void mppx (int arr [], int len) // arr is the name of the sorted array, and len is the array length. {Int I = 0, j = 0, tem = 0; for (I = 0; I
     
      
Arr [j + 1]) {tem = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = tem ;}}} return ;}
     
2. Select sorting
Void xzpx (int arr [], int len) // arr is the name of the sorted array, and len is the array length. {Int I = 0, j = 0, tem = 0; for (I = 0; I
     
      
Arr [j]) {tem = arr [j]; arr [j] = arr [I]; arr [I] = tem ;}} return ;}
     
3. hexadecimal conversion
Int fun (void) {char arr [100] = {0}, I = 0; int a = 0, B = 0; printf ("number of inputs! \ N "); scanf (" % d ", & a); printf (" in hexadecimal notation? \ N "); scanf (" % d ", & B); do {arr [I] = a % B; a = a/B; I ++ ;} while (a> 0); for (I = I-1; I> = 0; I --) {if (arr [I] <10) printf ("% d ", arr [I]); else printf ("% c", arr [I]-10 + 'A ');}}
4. Binary Search for the badge of data
#include 
     
      int search_arr_element(int arr[], int left, int right, int data);void main(void){    int arr[5] = {1, 2, 3, 4, 5};    printf("%d\n",find_data(arr, arr[0], arr[4], 5));    return;}int find_data(int arr[], int left, int right, int data){    int mid = 0;    while (left <= right)    {           mid = (left+right) / 2;        if(data > arr[mid])        {            left = mid + 1;        }        else if(data < arr[mid])        {            right = mid - 1;        }        else        {               return mid;        }    }    return -1;}
     
5. Move the loop right
# Include
     
      
# Include
      
       
Void right (char arr [], int wei); void main (void) {char arr [] = "qaujetg"; int a = 0; printf ("how many digits? \ N "); scanf (" % d ", & a); right (arr, a); printf (" % s \ n ", arr); return ;} void right (char arr [], int wei) {int I = 0, j = 0, tem = 0, len = strlen (arr); for (I = 0; I
       
        
0; j --) arr [j] = arr [J-1]; arr [0] = tem;} return ;}
       
      
     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.