Learning experience------C language complex data types

Source: Internet
Author: User

---restore content starts---

In the process of learning C, the most complicated knowledge is the complex data type in the title, because it includes arrays, strings, pointers and other data types, in this link, we inevitably have to touch the memory, analyze the data type of memory storage method, Can be more clear understanding of the operation of this type of principle, the following analysis of my study in this video learning experience it.

One, array


1. An array, literally, is the meaning of a set of data, and yes, the function of an array is to store a set of data. Arrays are characterized by the same type of data, such as int such as Double.

2. Array format, element type array name [number of elements] such as int age[3];

3. Simple use of arrays, arrays initialized to: int age[3]={0,1,2}; It is worth noting that the subscript of an array starts at 0 instead of 1, so the subscript of the last element of the array is always the array element-1.

1#include <studio.h>2 intMain ()3 {4         intage[3]={1,2,3};5        intage[3]={1,2};//You can assign only the preceding elements6        intage[]={1,2,3};//can not write the number of elements, the system automatically according to the number of elements defined7       intage[3]={[1]=1,[2]=2};//This is a direct assignment to the 2nd and 3 elements8  //These are the correct wording .9 Ten    /*Common Errors One int a[]; A Int[3] A; - int a[b]; - a = {ten, one}; the A[3] = {10,9,8,}; -  -  - */ +           return 0;  -}

4. (1) Memory analysis of an array, the amount of space allocated by the array is determined by the number of bytes * elements that the data type occupies.

(2) partition of storage space (memory allocation is from high address to low address, but an array of internal elements from low to high)

(3) Array name is the address of the array

(4) If the number of array elements is calculated: You can use sizeof (array name)/sizeof (the data type of the array element) to get the number of elements

5 When the array is passed as a function, because it is the delivery of the address, so it is very different from the value of the previous basic data type, we can use the following code to understand.

1 voidChangeintarray[])2 {3printf"array==%p\n", array);//the address of the output array is not & The array name represents the address of the array4array[0]= -;5 }6 7 voidChange2 (intN)8 {9n= -;Ten } One  A intMain - { -       intage[5]={1,2,3,4,5}; the      intA=Ten; - Change (age); - Change2 (a); -printf"age[0]==d%", age[0]);//The output here is 100 because the passing of the array is address passing, and the data value changed within the function corresponds to the data value that changed the address +printf"a==d%", a);//here the output is 10, and the data passing village of the basic data type is purely a value pass.  -  +         return 0; A}

Here is an example of an integrated practice of arrays, designing a function to find the minimum value of an integer array

//Design a function to find the minimum value of an integer array element#include <stdio.h>/** Define function requires two parameters, one is an array, one is an array length*/intMinofarray (intArray[],intlength) {    //define a variable to store the minimum value (default is the first element)    intMin = array[0]; //Traverse all elements to find the minimum value     for(inti =1; i<length; i++)    {              if(Array[i] < min)//determines whether the current element is less than min{min= Array[i];//if the current element is less than min, the current element overrides Min.         }           }    //returns the minimum value    returnmin;}intMain () {//an array of integral types    intAges[] = { +, A,103, the, the, -, -}; intLen =sizeof(Ages)/sizeof(int);//calculating the length of an array    intmin = Minofarray (ages, Len);//Call the Minofarray functionprintf"The minimum value is:%d\n", Min);//Output Minimum Value    return 0;}

---restore content ends---

Learning experience------C language complex data types

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.