"IOS development" Object-c array using the detailed

Source: Internet
Author: User
Tags array definition

.



I. one-dimensional arrays



1. One-dimensional array definition



(1) Array definition


Array definition Format : type Arrayname[len];

-- Default initialization : Note that after the array is set, if it is an int array is initialized to 0 by default, if it is a floating-point default element of 0.0, if it is a pointer-type array the default type is null;



(2) Digital address calculation


array element Address Properties : Array elements are stored continuously;

-- Array header address : An array variable is a pointer that holds the first address of an element in an array;

--element address Calculation Formula : Element address = array First address + array element occupies memory size * element index value;




2. Initialization of arrays


Specify the number of array elements :

-- All initialization : Initialize all elements of the array;

-- Partial initialization : Initialization of a subset of elements in an array;


do not specify the number of arrays: If you do not specify the number of arrays, the definition must be initialized, the length of the array is automatically inferred based on the number of elements initialized;


array length calculation : sizeof (arrayname)/sizeof (arrayname[0]);



3. Code Examples


code example :

-- Code content :

/*************************************************************************    > File name:12-onearray.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created time: Day 12/7 16:30:31 2014 ******************* /#import <foundation/foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {//definition array format type Arrayname[length]int array[5];//definition length, fully assigned int array1[5] = {0, 1, 2, 3, 4};/ /definition length, incomplete assignment int array2[5] = {0, 1, 2};//does not define length, must initialize int array3[] = {0, 1, 2};//defines C-language string array char * array4[] = {"Csdn", "Octo Pus "," hello "};//definition object-c string array nsstring *array5[] = {@" csdn ", @" Octopus ", @" Hello "};//traverse array2for (int i = 0; i < si Zeof (array2)/sizeof (array2[0]); i + +) {NSLog (@ "array2[%d] =%d", I, Array2[i]);}}
-- Execution Results:

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 12-onearray.m octopus-2:oc octopus$./a.out 2014-12-07 16:38 : 58.238 a.out[379:507] array2[0] = 02014-12-07 16:38:58.241 a.out[379:507] array2[1] = 12014-12-07 16:38:58.242 a.out[379 : 507] array2[2] = 22014-12-07 16:38:58.242 a.out[379:507] array2[3] = 02014-12-07 16:38:58.243 a.out[379:507] array2[4] = 0




three. Multidimensional Arrays



1. Two-d array definition


Two-dimensional array definition format : type arrayname[rowlength][columnlength];

-- Array length : The number of the left rowlength is the number of one-dimensional arrays, that is, a few lines;



2. Multidimensional Array initialization


several initialization methods : Example int array[3][5];

-- Initialize All level two arrays : 3 One-dimensional arrays can be initialized, and the number of elements in a one-dimensional array is arbitrary;

-- Initialize part of the second series group : it can initialize less than 3 one-dimensional arrays and initialize only part of it;

-- omit two-dimensional array length : If you initialize when declaring an array, you can omit the first array length of the array;

--- omit the two-level array bracket when initializing : The parentheses of a one-dimensional array can be omitted, and the system automatically fills in the default initialized part automatically;



3. Example


code example :

-- object-c code :

/************************************************************************* > File name:12-multiarray.m > Auth Or:octopus > Mail:octopus_truth.163.com > Created Time: 412/11 00:59:14 2014 ***************************** /#import <foundation/foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {printf ("1. Initialize partial contents of all arrays \ n "); int array1[3][5] = {{1, 2},{5, 7, 7},{2, 4}};for (int i = 0; i < sizeof (array1)/sizeof (array1[0]); i + + {for (int j = 0; J < sizeof (Array1[0])/sizeof (Array1[0][0]); j + +) {printf ("%d\t", Array1[i][j]);} printf ("\ n");} printf ("\n2. Initialize partial contents of the partial array \ n "); int array2[3][5] = {{1, 2, 4, 6, 7},{1}};for (int i = 0; i < sizeof (array2)/sizeof (array2[0]); i + +) {fo R (Int j = 0; J < sizeof (Array2[0])/sizeof (Array2[0][0]); j + +) {printf ("%d\t", Array2[i][j]);} printf ("\ n");} printf ("\n3. Omit the length of the array \ n "); int array3[][5] = {{1, 2, 4, 6, 7},{1}};for (int i = 0; i < sizeof (ARRAY3)/sizeof (array3[0]); i + +) {foR (Int j = 0; J < sizeof (Array3[0])/sizeof (Array3[0][0]); j + +) {printf ("%d\t", Array3[i][j]);} printf ("\ n");} printf ("\n4.  Omit the curly brace of the array \ n "); int array4[][5] = {1, 2, 4, 6, 7,1};for (int i = 0; i < sizeof (ARRAY4)/sizeof (array4[0]); i + +) {for (int j = 0; J < sizeof (Array4[0])/sizeof (array4[0][0]); J + +) {printf ("%d\t", Array4[i][j]);} printf ("\ n");}}}

-- Execution Results :

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 12-multiarray.m octopus-2:oc octopus$./a.out 1. Initializes part of the entire array of content 1200057700240002. Initializes part of the partial array of contents 1246710000000003. Omits the length of the array by 12467100004. The curly brace of the omitted array 1246710000



three. strings, character arrays and string-related functions



1. NSLog vs. printf


NSLog vs. printf :

-- output string format is different : printf output C language format string, such as "string", NSLog output object-c string, such as @ "string";

-- NSLog has redundant operation : NSLog will automatically add the date and time line and other operations;


Example :

/*************************************************************************    > File name:12-nslogvsprintf.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created Time: 412/11 01:18:17 2014 ************* /#import <foundation/foundation.h>int Main (int ARGC, char * argv[]) {@autoreleasepool {NSLog (@ "This is the string NSLog output");p rintf ("This is the string of printf Output");}}

-- Execution Results:




2. Character Arrays and strings


define the character array : The following two methods are equivalent;

-- using string initialization : char array[] = "fuck", using this method will be automatically added after the "" ";

-- Initialize with array mode : char array[] = {' F ', ' u ', ' C ', ' k ', ' n '};


about the length of the character array : a 4-character array with a length of 5, followed by a ' plus ';


code example :

/*************************************************************************    > File name:12-chararray.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created Time: 412/11 01:27:12 2014 ************* /#import <foundation/foundation.h>int Main (int ARGC, char * argv[]) {@autoreleasepool {char char_array[] = "fuck"; char char_array1[] = {' F ', ' u ', ' C ', ' k ', ' + '};p rintf (" Char_array size:%lu, char_array1 Size:%lu\n ", sizeof (Char_array), sizeof (CHAR_ARRAY1));}}

-- Run Results:

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 12-chararray.m octopus-2:oc octopus$./a.out Char_array Size: 5, Char_array1 Size: 5



3. Commonly used string handling functions



code example :

-- Code :

/************************************************************************* > File name:12-strmethod.m > Autho R:octopus > Mail:octopus_truth.163.com > Created Time: 412/11 01:34:08 2014 ****************************** /#include <string.h> #import <foundation/foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {char c = ' 1 '; char c1 = ' a '; char c2 = ' a '; char c3 = ' \ t '; NSLog (@ "character correlation function");p rintf ("C =%c is a number or number:%d \ n", C, Isalnum (c));p rintf ("C =%c is a letter:%d\n", C, Isalpha (c));p rintf ("C =%c is a control character:%d\n ", C, Iscntrl (c));p rintf (" c =%c is control number:%d\n ", C, IsDigit (c));p rintf (" C =%c is a printable character:%d\n ", C, ISG Raph (c));p rintf ("C1 =%c is lowercase:%d\n", C1, Islower (C1));p rintf ("C1 =%c is uppercase:%d\n", C1, Isupper (C1));p rintf ("C1 = Whether%c is a punctuation mark:%d\n ", C1, ISPUNCT (C1));p rintf (" C3 =%c is a white space character:%d\n ", C3, Isspace (C3));p rintf (" C1 =%c is a hexadecimal character:%d\n ", C1, Isxdigit (C1)); NSLog (@ "string correlation function"); char array[20]= "Fuck"; char array1[] = "bitch";//Note string array to have enough space for the subsequent array1printf ("%s splicing string \ n", strcat (array, array1));p rintf (" Array appears c's position%s \ n ", STRCHR (array, ' C '));p rintf (" compare array with array1 size: array is greater than Array1:%d\n ", strcmp (array, array1) );}}


-- Execution Results :

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 12-strmethod.m octopus-2:oc octopus$./a.out 2014-12-11 02:1 1:08.608 a.out[2164:507] Character-related function c = 1 is  a number or number: 1 c = 1 is a letter: 0c = 1 is a control character: 0c = 1 is the control number: 1c = 1 is a printable character: 1c 1 = A is lowercase: 0c1 = A is uppercase: 1C1 = A is a punctuation mark: 0c3 =  Whether it is a white-space character: 1c1 = A is a hexadecimal character: 12014-12-11 02:11:08.610 a.out [2,164:507] string correlation function fuckbitch concatenation string array appears c position ckbitch Compare array and array1 size: array is greater than array1:4



"IOS development" Object-c array using the detailed

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.