C Language Pointers and arrays

Source: Internet
Author: User

C Language Pointers and arraysBinsearch else-if ShellsortInsertsortPointers and addresses

A pointer is a class of data types in a programming language and their objects or variables that represent or store a memory address that directly points to the value of the object (points to) where the address exists.
The value operation *p returns the value stored in memory space where the memory address is p. The FETCH address &p operation returns the memory address of the operand p
The C language is passed the value of the parameter value to the called function, the called function cannot directly modify the value in the key function, need to modify the value in the key function needs to use the pointer

Pointers and Arrays

The array name represents the address of the first element of the array, for a[i" reference can be written * (a+i) or * (pin + i) or pin[i] , expressions implemented by array name and subscript can be equivalent by pointer and offset

int a[10];int *pin;pin = &a[0];pin = a;pin = pin + 1;pin = &a[1];pin = a + 1;

differentis between the array name and the pointer, the array name represents a constant, and the opposite pointer represents a variable, sopin = aAndpin++is legal, anda = paAnda++It's wrong.

Address arithmetic operations

Pointers can be used to compare operations: pointers to the same array can be== <= >= !=such as
The same pointer can beintegerAdd or subtract operations, such asp+nwhichnThe length is not fixed and will be scaled as the pointer scales, subtraction is the same
Two pointers to the same array can be subtracted or compared asp-q+1Represents the number of elements between two pointers
Pointers are not allowed to perform operations other than those mentioned above, such as the addition of two pointers, the multiplication operation, the addition and subtraction of double or float, and the casting operation

character pointers and functions

" I am a String ", in the internal representation of the characters and \0 composition

char amessage[] =" Now is the time "; Defines an array of char *message = "Now is the time"; Define a pointer  

For the first case, an array is defined, which amessage points to the first character of the character array, fixed, and the individual characters in the array can be modified. In the second case, message pointing to a string constant, just assigning a pointer to the string constant, message and not copying the string, but referring to the operation of the pointer, C does not provide an operator that handles the entire string.

Pointer arrays and pointers to pointers

The difference between a pointer array and a pointer array is an array of pointers, and each element of the array is a pointer, and the pointer is a pointer to an array

int a[10];int *p[10];int (*p)[10] = &a; //数组指针的使用


Enter description here

aAnd&aThe difference, foraThe value of the array name is a pointer constant, which is the first element of the array (representing the address of the first element).&aPoint to the entire array, however&aAndaThe values are the same, the difference is&a+1Anda+1The meaning of the representative is different,&a+1Increases the length of the entire array,a+1Increases the length of an element (a[1]

Multidimensional arrays

daytab[i][j] represents a two-dimensional array, if you want to pass a two-dimensional array as a parameter to the function, you must indicate in the function declaration the number of columns in the array (not much related to the number of rows in the array). What's the explanation? Passing a pointer to a function is the passing of a j the line vector of the elements, which is the previously mentioned array pointer

can be derived from the multidimensional array above: the length of each row of the array pointer is the same. This is also different from the pointer array, where each pointer to the array of pointers can be the same length as the

char *name[] = {"illegal month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}

The above definition is the initialization of an array of pointers, each element is different in length, name[0](或者name) pointing to the first element

Command-line arguments

ARGC , for parameter count) represents the number of arguments in the command line, and the second parameter (habit is ARGV , for parameter vectors) is a pointer to an array of strings, where each string corresponds to a parameter
int Main (int argc, char *argv[])

argv[0]Represents the function name, so argc the value is at least 1, the first optional argument is argv[1] , and the last optional argument isargv[argc - 1]

C Language Pointers and arrays

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.