The meaning of the arguments in int main (int argc, const char * argv[0]); pointer arrays and arrays pointers

Source: Internet
Author: User
Tags array definition

Well, some compiler initialization will produce such parameters.

ARGC is the total number of arguments for the command line, argv[] is a argc parameter, where the No. 0 parameter is the full name of the program

1. Several common types of C + + parameters

    • int main (void
    • int main (;
    • int main (int argc Char **argv) ;  //equivalent to int main (int argc, char *argv[]), is it equivalent? is not the previous one can represent any number of arbitrary length of the array, the latter is just a fixed length of any number of arrays? see
    • int main(int argc, char *argv[]);
    • int main (int argc, char arg[][]);
    • Argc=3, which indicates that there are 2 parameters in addition to the name of the program.    Argv[0] points to the program path and name of the input.    ARGV[1] points to the parameter para_1 string. ARGV[2] points to the parameter para_2 string.

2. Description of the parameter: int ARGC represents the number of arguments to the main function, and the main function has at least one argument, that is, the first parameter is the value of the program name of the main function: that is, argv[0], argc>=1

char * * ARGV represents the parameter table entered at run time by the program where the main function is located, with or separated by a space: the first parameter exists in argv[1] each second-dimensional array cell in a single number, for example Biji 12546 is argv[1][0] = 1, argv[1][2]=2, Argv[1][3]=5, Argv[1][4]=5, argv[1][6]=6, and argv[1]=12546

The second parameter exists in argv[2], and so on.

3. For argv deposit is actually a character array, if you want to enter an integer string into the number of shaping, you can call the STL function Atoi (&argv[1][0])//note, cannot write &argv[1] compile the report type mismatch error Error:cannot convert 'char**' to 'const char*' for argument ' 1 ' to ' int atoi (const char*)'

&argv[1][0] is the first address of the character array that takes the initial argument.

pointer arrays and array pointer problems

These two names are different, of course, the meanings they represent are different. I just started to see this scare, mainly Chinese is too broad and profound, the whole such abbreviation is too professional, people are around dizzy. It is easier to understand from the English explanation or the Chinese full name.

Pointer array: Array of pointers, which is used to store pointers to arrays, which are arrays of elements that are pointers

Array pointer: A pointer to an array, which is a pointer to the array

Also note the differences in their usage, as illustrated below.

int* a[4] Pointer array

: The elements in array A are all int pointers (if there is no *, then the data is all int type in array a)

Element means: *a[i] * (A[i]) is the same, because [] priority is higher than *

int (*a) [4] Array pointer

Indicates: pointer to array a (if int (*a), then pointer to integral type a)

Element representation: (*A) [i]

Note: In practical applications, for pointer arrays, we often use this:

typedef int * pInt; pInt a[4];

This is the same as the meaning expressed in the pointer array definition above, except that the type transformation is taken.

The code demonstrates the following:

#include <iostream> using namespace std; int main() { int c[4]={1,2,3,4}; int *a[4];  //指针数组 int (*b)[4];  //数组指针 b=&c; //将数组c中元素赋给数组a for ( int i=0;i<4;i++) { a[i]=&c[i]; } //输出看下结果 cout<<*a[1]<<endl;  //输出2就对 cout<<(*b)[2]<<endl;  //输出3就对 return 0; }

Note: The array pointer is defined, the pointer to the first address of the array, the pointer must be given an address, it is easy to make the mistake is, do not give B address, directly with (*B) [i]=c[i] to the array B element assignment, when the array pointer does not know where to point, debugging may be correct, but the runtime must have problems, Be aware of this when using pointers. But why does a not have to give him the address, the element of A is a pointer, in fact the for loop has already given the element in the array a address. But if you write *a[i]=c[i in the For Loop], this also goes wrong. In short, the definition of the pointer must know where the pointer points, or tragedy.

Similar to the pointer function and function pointers, we have encountered.

The meaning of the arguments in int main (int argc, const char * argv[0]); pointer arrays and arrays pointers

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.