C12---Array

Source: Internet
Author: User
Tags array definition

////main.c//Basic concepts of arrays////Created by Xiaomage on 15/6/9.//Copyright (c) 2015 Itcast. All rights reserved.//#include<stdio.h>intMainintargcConst Char*argv[]) {            //arrays are used when you need to keep a lot of "same type" data//because if you use variables, it becomes very complex and very technical.    intScore = About; intScore1 =98; ...    intscore998 = the; printf ("%i\n", score); /*Array definition Format: Data type variable name;     Data type array name [number of data];     Element type array name [number of elements]; Element type: Is the data type that needs to be stored in the array, and once specified, the array can only store the number of data elements of that type: the number of data (elements) that can be stored in the array*/    intnum; Num= A; printf ("num =%i\n", num); intscores[3];//defines an array of names called scores, which can hold 3 data of type int//scores = n;//The system can't figure out who to assign to .//as long as a C-language array is defined, the system automatically assigns a number to each piece of storage space in the array.//This number starts at 0 and increments once.//The number that the system automatically binds in the array, which we call an indexscores[0] = A; scores[1] = the; scores[2] = -; printf ("Scores[0] =%i\n", scores[0]); return 0;}
////main.c//initialization and traversal of arrays////Created by Xiaomage on 15/6/9.//Copyright (c) 2015 Itcast. All rights reserved.//#include<stdio.h>intMainintargcConst Char*argv[]) {    //need to keep 101 people in the class//element type array name [number of elements];    /*//define re-initialize int scores[5];    Scores[0] = 99;    SCORES[1] = 88;    SCORES[2] = 77;    SCORES[3] = 66;     SCORES[4] = 100; */    /*int num;        num = 10;     int NUM1 = 10; */    //assigns each value in the {} to each element in the array in turn//and assign values starting from 0//also known as the initialization of arrays (fully initialized)    intscores[5] = { About, the, the, the, -}; //Partial Initialization//Initialize starting from 0, assigning values in turn//Note: If "in partial initialization" the corresponding memory is not initialized, then the default is 0    intscores1[3] = { One, A}; printf ("0 =%i\n", scores1[0]); printf ("1 =%i\n", scores1[1]); printf ("2 =%i\n", scores1[2]); printf ("-------\ n"); //Note: If there are no arrays initialized (full and departmental), then do not use the data in the array, possibly a piece of junk data (random value)    intscores2[3]; printf ("0 =%i\n", scores2[0]); printf ("1 =%i\n", scores2[1]); printf ("2 =%i\n", scores2[2]); printf ("-------\ n"); //Note: When defining an array, the number of elements in the array cannot be used, and if a variable is used, then there are some random values in the array.    intnum =Ten; intScores3[num]; printf ("0 =%i\n", scores3[0]); printf ("1 =%i\n", scores3[1]); printf ("2 =%i\n", scores3[2]); //Note: It is not recommended to use a variable to define an array, if the variable is used to define an array, as the number of elements of the array, the uninitialized case is a random value, if the initialization will directly error//int num2 = ten;//int scores4[num2] = {one, one};printf"-------\ n"); //Note: If the definition is initialized at the same time, then the number of elements can be omitted//after omitting, the initialization of the assignment of several data, then the length of the array is a few. That is, the array will store several data in the future.    intScores5[] = {1,3}; printf ("0 =%i\n", scores5[0]); printf ("1 =%i\n", scores5[1]); printf ("-------\ n"); //Note: If an array is defined without initialization, the number of elements cannot be omitted//int scores6[]; //0 1 2) 3 4//int socres7[101] = {0, 0, 0, 1, 3};//int socres7[101];//socres7[99] = 1;//socres7[100] = 3; //You can assign a value to an element of the specified index by means of [indexed] =    intsocres7[101] = {[ About] =1, [ -] =3}; printf ("3 =%i\n", socres7[ About]); printf ("4 =%i\n", socres7[ -]); //Note: The {} can only be initialized at the same time as defined, and cannot be initialized with {} If it is defined first//If you define it first, you can no longer assign a whole value//int scores8[3];//Scores8 = {1, 4, +};//scores8[0] = 1;        return 0;}
////main.c//traversal of an array////Created by Xiaomage on 15/6/9.//Copyright (c) 2015 Itcast. All rights reserved.//#include<stdio.h>intMainintargcConst Char*argv[]) {    //Take out all the values in the array, called Traversal    intscores[6] = {1, at, -, the, in, the, About,2}; /*printf ("scores[0] =%i\n", scores[0]);    printf ("scores[1] =%i\n", scores[1]);    printf ("scores[2] =%i\n", scores[2]);    printf ("scores[3] =%i\n", scores[3]);    printf ("scores[4] =%i\n", scores[4]);     printf ("scores[5] =%i\n", scores[5]); */    //Note: When iterating through an array, try not to write the number of times//how many times to traverse should be determined by the array, that is, how many times the traversal should be computed by the array    /*printf ("scores =%lu\n", sizeof (scores));//Calculates the total number of bytes occupied by the array printf ("scores[0] =%lu\n", sizeof (Scores[0]));//Calculation    The number of bytes occupied by an element in the array printf ("A number of elements:%lu\n", sizeof (scores)/sizeof (scores[0)); */    //dynamically computes the number of elements in an array    intLength =sizeof(scores)/sizeof(scores[0]);  for(inti =0; i < length; i++) {printf ("scores[%i] =%i\n", I,scores[i]); }    return 0;}

C12---Array

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.