C-language arrays (one-dimensional, character)

Source: Internet
Author: User
Tags array definition strcmp

Main.m

1-16 class Notes

Lecturer: Xiao Hui

Author: Wang Xuewen

Created by Lanouhn on 15/1/16.

Copyright (c) 2015 Lanouhn. All rights reserved.

Array (one-dimensional, character)

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

Arrays: Ordered queues for storing the same data types

Format of the array definition

Data type variable name [number of elements] = {value 1, value 2, Value 3, ...}

Array of integral type

int array[5] = {1, 2, 3, 4, 5};

//

Floating-point arrays

Float Array1[5] = {1.0, 2.0, 3.0, 4.0, 5.0};

//

Array out of bounds, very dangerous

int a[2] = {1, 2, 3};

//

How arrays are defined

int b[4] = {1, 2, 3};//1, 2, 3, 0

//

int c[4] = {1};//1, 0, 0, 0

//

int d[4] = {0};//is all 0

//

The number of array elements is omitted, but the initial value must be assigned, otherwise there is no way to determine the number of elements

int e[] = {1, 2, 3, 4};//has four elements

//

int g[] = {0};//has an element

//

int h[] = {};//0 elements, meaningless

//

sizeof (); Calculate the number of bytes that the data type or variable occupies

/*

Calculate the number of bytes in a data type

unsigned long length = sizeof (short);

printf ("short:%lu\n", length);

Calculate the number of bytes in a variable

int i = 0;

printf ("int:%lu\n", sizeof (i));

printf ("short:%lu\n", sizeof (short));//value range: -2^15 to 2^15-1

printf ("int:%lu\n", sizeof (int));//value range: -2^31 to 2^31-1

printf ("char:%lu\n", sizeof (char)); Range of values: -2^7 to 2^7-1

printf ("long:%lu\n", sizeof (long));//value range: -2^63 to 2^63-1

printf ("float:%lu\n", sizeof (float));//value range: -2^31 to 2^31-1

printf ("double:%lu\n", sizeof (double));//value range: -2^63 to 2^63-1

Number of bytes in the array = number of bytes in the data type + number of array elements

int a[5] = {1, 2, 3, 4, 5};

printf ("%lu\n", sizeof (a));

Use of arrays

int k = 2, j = 3;

K + J;

Arrays cannot participate directly in operations, and elements in an array can participate in operations

int arr1[2] = {1, 2};

int arr2[2] = {4, 5};

ARR1 + arr2

Gets the elements in the array

Format: array name [element subscript]

(*) element subscript starts from zero

The range of the original acid subscript {0, number of array elements-1}

int a[5] = {1, 2, 3, 4, 5};

printf ("%d\n", a[0]);//The first element of an array

Array out of bounds

A[5]

Define the difference between an array and an array element

1. Defining an array must have a data type

2. When defining an array, the number of elements can be omitted; When you take an array element, the subscript of the element cannot be omitted.

Iterate through each element in the array and print

for (int i = 0; I <= 4; i++) {

printf ("a[%d] =%d\n", I, a[i]);

}

Assign a value to an array of elements

A[2] = 6;

Defines an integer array with 20 elements, with each element having a value range of 30-70

int a[20] = {0};

int sum = 0;

for (int i = 0; i <; i++) {

int temp = arc4random ()% 41 + 30;

int n = 0;

if (n < 20) {

A[n] = temp;

n++;

//}

A[i] = temp;

Sum =sum + temp;

printf ("a[%d] =%d\n", I, a[i]);

}

printf ("sum =%d\n", sum);

Copy an array, which is two array capacity, to copy the elements of one of the arrays into another array

int b[20] = {0};

for (int i = 0; i <; i++) {

B[i] = A[i];

printf ("b[%d] =%d\n", I, b[i]);

}

Generates 2 arrays, each with 10 elements, an element with a value range of 20-40, an array of corresponding elements added, and placed in another array

int a[10] ={0};

int b[10] = {0};

int c[10] = {0};

for (int i = 0; i < i++) {

A[i] = arc4random ()% 21 + 20;

B[i] = arc4random ()% 21 + 20;

C[i] = A[i] + b[i];

printf ("a[%d] =%d b[%d] =%d c[%d] =%d\n", I, a[i], I, b[i], I, c[i]);

//    }

Bubble sort

int a[5] = {10, 2, 6, 8, 4};

The number of times the outer loop controls the comparison (number of array elements-1)

for (int i = 0; i < 5-1; i++) {

Inner Loop controls the number of times each trip is compared, 22 comparison

for (int j = 0; J < 5-1-I; j + +) {

if (A[j] > a[j + 1]) {

int temp = A[j];

A[J] = a[j + 1];

A[j + 1] = temp;

}

}

}

Print array elements

for (int i = 0; i < 5; i++) {

printf ("a[%d] =%d\n", I, a[i]);

}

Randomly generates 10 [20,40] numbers and sorts 10 numbers from small to large

int a[10] = {0};

for (int i = 0; i < i++) {

int temp = arc4random ()% 21 + 20;

A[i] = temp;

printf ("a[%d] =%d", I, a[i]);

}

printf ("\ n");

for (int i = 0; i < 9; i++) {

for (int j = 0; J < 10-1-I; j + +) {

if (A[j] > a[j + 1]) {

int temp = A[j];

A[J] = a[j + 1];

A[j + 1] = temp;

}

}

}

printf ("Post-order \ n");

for (int i = 0; i < i++) {

printf ("a[%d] =%d", I, a[i]);

}

printf ("\ n");

Character array

The wording of a

Char string[6] = {' I ', ' P ', ' h ', ' o ', ' n ', ' e '};

Two

Char string1[6] = "iphone";

Live: Two types of writing are completely equivalent

No assignment when defined

int a[5] = {1}; The integer type defaults to 0

Char s[5] = {' I '}; Character type default is ' + '

Use of character arrays

STRING[2] = ' s ';

Iterate through each element of the array and print

Char String[8] = {' I ', ' P ', ' h ', ' o ', ' n ', ' e ', ' 5 ', ' s '};

for (int i = 0; i < 8; i++) {

printf ("string[%d] =%c\n", I, string[i]);

}

%s Print character array (usually add ' + ' at the end of the array)

String has 6 elements that need to be manually added at the end of the array

The string1 has 7 elements, and the system automatically adds '/' at the end of the array

If you print a string with%s, the array will be out of bounds because there is no '. '

Char string[] = {' I ', ' P ', ' h ', ' o ', ' n ', ' e '};

Char string1[] = "1phone";

Char string3[6] = {' I ', ' P ', ' h ', ' o ', ' n ', ' E '};//have 6 elements

Char string4[6] = "4phone";//There are 6 elements

printf ("String:%s\nstring1:%s\nstring3:%s\nstring4:%s\n", String, string1, string3, String4);

%s stops when it encounters '/'

STRING[2] = ' + ';

printf ("String:%s\n", string);

strlen (const char *)

Calculating the length function of a character

It's over, and ' I ' don't count into length.

Char string[10] = "I love U";

STRING[2] = ' + ';

unsigned long length = strlen (string);

printf ("Length =%lu\n", length);

strcpy (String 1, String 2)

Copy of the string

Copy each element in string 2 to string 1 until the string 2 encounters the end of

(*) string 1 to be large enough to prevent cross-border

Char str1[10] = "I love";

Char str2[10] = "You";

strcpy (str1, str2);

printf ("str1:%s\n", str1);

printf ("str2:%s\n", str2);

*/

strcat (String 1, String 2)

string concatenation function

Start with string 1 from the first ' + ' to stitch each element of string 2 into string 1 until the string 2 encounters the end

Note: The space for string 1 must be large enough to prevent out-of-bounds

Char str1[10] = "I love";

STR1[2] = ' + ';

Char str2[10] = "You";

strcat (str1, str2);

printf ("str1:%s\n", str1);

printf ("str2:%s\n", str2);

//

strcmp (String 1, String 2)

string comparison function

Subtract from the ASCII value of the element in string 1 for each element and the corresponding position string 2, if 0, to continue the comparison, and if not 0, to stop the comparison and return the difference; when the corresponding element is ' + ', the comparison stops and the result is 0.

Char str1[10] = "ABC";

Char str2[10] = "ABC";

int result = strcmp (str1, str2);

printf ("result =%d\n", result);

//

return 0;

}

C-language Array (one-dimensional, character)

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.