Pointer array and main function with parameter, pointer array parameter main

Source: Internet
Author: User

Pointer array and main function with parameter, pointer array parameter main
(1) pointer Array

A pointer array stores an address for each element, which is equivalent to a pointer variable.
For example, int * p [4]
Pointer arrays are suitable for pointing to several strings, making processing strings more flexible.
For example, you need to output several strings in alphabetical order from small to large.

1 # include <stdio. h> 2 # include <string. h> 3 int main () {4 void sort (char * name [], int n); 5 void print (char * name [], int n ); 6 char * name [] = {"Follow me", "BASIC", "Great Wall", "FORTRAN", "Computer design"}; 7 8 int n = 5; 9 sort (name, n); 10 print (name, n); 11 return 0; 12} 13 14 void sort (char * name [], int n) // sort the string by 15 {16 char * temp; 17 int I, j, k; 18 for (I = 0; I <n-1; I ++) {19 k = I; 20 for (j = I + 1; j <n; j ++) 21 if (strcmp ( Name [k], name [j])> 0) k = j; 22 if (k! = I) {23 temp = name [I]; name [I] = name [k]; name [k] = temp; // change the value of the array element pointing to the I string to the value of the array element pointing to the k string to 24} 25} 26} 27 28 void print (char * name [], int n) {// output string 29 int I; 30 for (I = 0; I <n; I ++) 31 printf ("% s \ n ", name [I]); 32}

Through the example above, try to compare
If (strcmp (name [k], name [j])> 0) and if (strcmp (* name [k], * name [j])> 0) are different:
If (strcmp (name [k], name [j])> 0) is an element of a comparison string from start to end.
While if (strcmp (* name [k], * name [j])> 0) Only compares the first element pointing to the string
Note: strcmp only compares the size of the ascii code from start to end, while stricmp is case-insensitive.

(2) pointer to pointer

The pointer variable char ** p pointing to the pointer data;
The element of the pointer array can point to a string, or to integer or real data.
A small example pointing to a string

1 # include <stdio. h> 2 int main () {3 char * name [] = {"Follow me", "BASIC", "Great Wall", "FORTRAN", "Computer design "}; 4 char ** p; 5 int I; 6 for (I = 0; I <5; I ++) {7 p = name + I; // first point p to name [0] of the name array. * p is the value of name [0], that is, the address of the first character of the first string, output 5 strings in sequence: 8 printf ("% s \ n", * p); 9} 10}

Small Example of pointing to integer data

1 # include <stdio. h> 2 int main () {3 int a [5] = {1, 3, 5, 7, 9}; 4 int * num [5] = {& a [0], & a [1], & a [2], & a [3], & a [4]}; 5 int ** p, I; 6 p = num; 7 for (I = 0; I <5; I ++) {8 printf ("% d", ** p); // p is & num [0], * p is the value of num [0], that is, a [0]. ** p is the value of 9 p ++; 10} 11} of a [0}

Using pointer variables to access another variable is called indirect access
The pointer to the pointer data uses a second-level inter-address. The popular point is to access a variable through two addresses. When a variable is accessed through multiple (greater than 2) addresses, it is called multiple pointers.

(3) array of pointers as main function parameters

The main function with parameters is prototype:
Int main (int argc, char * argv []);


Argc and argv are the main function parameters, which are the command line parameters of the program. Argc refers to the number of parameters, and argv refers to the parameter vector. Each element points to a string in the command line.
The main function is called by the operating system, so real parameters can only be provided by the operating system. In the command state, the real parameter is provided together with the command of the execution file. The command line includes the command name and parameters to be passed to the main function.


For example, $./echo computer and tom
Echo is the command name and computer is a parameter. In this example, argc is 4, because the command name is also a parameter.

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.