C language array and pointer

Source: Internet
Author: User

C language array and pointer

One-dimensional array and pointer

int a[10];int *pa;

The array name of a one-dimensional array represents the address of the first element of the array. Therefore, the copy statement pa = & a [0] is equivalent to pa =. The reference to array element a [I] is equivalent to * (a + I ). When calculating the value of array element a [I ],*(a+1)Then evaluate the program, so the program written with the pointer is faster than the program written with the array subscript.

There is a difference between the array name and pointer. the pointer is a variable, and pa = a and pa ++ are both valid. However, the array name is not a variable. Therefore, statements similar to a = pa and a ++ are invalid.

When the array name is passed to a function, the address of the first element of the array is actually passed. In the called function, this parameter is a local variable. Therefore, the array name parameter must be a pointer.

In function definition, the formal parameters char s [] and char * s are equivalent. The latter is usually used because it is more intuitive than the former to show that this parameter is a pointer.

Character array and character pointer

char a[] = "Hello";char *p = "Hello";

A is a one-dimensional array that can only store initialization strings and null characters '\ 0. A single character in the array can be modified, but a always points to the same storage location.
P is a pointer. Its initial value points to a String constant. After that, it can be modified to point to another address, but the content of the string cannot be modified.
Char * p = "Hello", "Hello" is saved in the static data zone, and the data cannot be modified. Pointed by the pointer m, you cannot use the pointer m to modify the value of the static data zone.
Char a [] = "Hello", "Hello" is saved in the stack space array. The array name is a and the variable name is the first address of the array. You can modify the array content in the form of a [I] = 'A' or * (a + I) = 'A.

Pointer and multi-dimensional array

Compare two definitions

int a[10][20];int *b[10];

In terms of syntax, both a [3] [4] and B [3] [4] are legal references to the int object. However, a is a real two-dimensional array, which allocates 200 int-type storage spaces, in addition, the position of a [row] [col] is obtained by using the formula 20 * row + col for the Regular Matrix subscript calculation.
However, for B, this definition only allocates 10 pointers and does not initialize them. Assuming that each element of B points to an array with 20 elements, the compiler will allocate 200 int-type storage space and 10 pointers to it.

Advantages of pointer Array

Each row of the array can have different lengths. Each element of B may not necessarily point to an array with 20 elements.

Two-dimensional array and two-dimensional pointer

The concepts of two-dimensional arrays and two-dimensional pointers are very confusing. Please refer to the following code.

#include 
  
   void print_str(char** strs, int i){    printf("strs[%d]=%s", i, strs[i]);}int main(int argc, char **argv){    char strs[10][100] = {"Hello", "World"};    print_str(strs, 0);    return 0;}
  

When this program is executed, Segmentation Fault may occur.
Segmentation Fault is because the program tries to access the memory that shouldn't be accessed. For details, see Wikipedia.
Https://en.wikipedia.org/wiki/Segmentation_fault

The variable strs is actually of the typechar (*)[100]Pointer, pointing to the first element of a two-dimensional array, that is, an array containing 100 char. the pointer value is actually equal to the address of the first char element of the two arrays. The parameter declaration of the print_str function ischar**That is, the second-level pointer. parameter passing does not change the value of the pointer. Therefore, the strs value is the same as the strs value defined in main, but strs in print_str is a level-2 pointer. Str [0] is a level-1 pointer, strs [0] = * (strs + 0 ), the value of strs [0] is the address corresponding to the value of the four bytes occupied by the string "Hell". The ASCII code of "Hell" is 0x48 0x65 0x6C 0x6C, the address is 0x6C6C6548 (little endian), and access to the memory is prohibited, so Segmentation Fualt occurs.

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.