pointer arrays and array pointers in C language __c language

Source: Internet
Author: User
Array Pointers(also called row pointers)
define INT (*p) [n];
() high priority, the first point is that P is a pointer to a one-dimensional array of integers, and the length of the one-dimensional array is n, which can be said to be the step of P. That is to say, when the p+1 is executed, p crosses the length of n integer data.

To assign a two-dimensional array to a pointer, you should assign a value like this:
int a[3][4];
int (*p) [4]; The statement defines an array pointer that points to a one-dimensional array of 4 elements.
P=a; Assigns the first address of the two-dimensional array to p, i.e. a[0] or &a[0][0]
p++; After the statement is executed, that is, P=p+1;p crosses the line a[0][] points to the row a[1][]

So the array pointer also refers to a pointer to a one-dimensional array , also called a row pointer .

Array of pointers
define int *p[n];
[] high priority, combined with p to an array, and then int* that this is an integer pointer array that has an array element of n pointer types . Executing p+1 here is wrong, so the assignment is wrong: p=a because p is an agnostic representation , only exists p[0], p[1, p[2]...p[n-1], and they are pointer variables that can be used to store variable addresses . But can be so *p=a; Here *p represents the value of the first element of the pointer array, the value of the first address of a.
To assign a two-dimensional array to an array of pointers:
int *p[3];
int a[3][4];
for (i=0;i<3;i++)
P[i]=a[i];
Here int *p[3] represents a one-dimensional array containing three pointer variables, respectively p[0], p[1], p[2]
So you want to assign values separately.

So the difference between the two will be enlightened,Array PointersJust apointer VariableThat seems to be the C language specifically used to point totwo-dimensional array, which occupies the storage space of a pointer in memory. An array of pointers isMultiple pointer variables, in the form of an array of memory, holding multiple pointers to the storage space.
Another point to note is that when used to point to a two-dimensional array, theReferenceAndreferencing with an array nameAre the same.
For example, to represent an element of the I row J column in an array:
* (P[I]+J), * (* (p+i) +j), (* (P+i)) [j], P[i][j]
Source: the difference between an array name and a group name address In C, in almost all expressions that use arrays, the value of the array name is a pointer constant, which is the address of the first element of the array. Its type depends on the type of the array element: if they are of type int, then the type of the array name is "constant pointer to int“。
in the following two situations, the array name is not represented by a pointer constant, when the array name is used as the operand of the sizeof operator and the monocular operator &. sizeof returns the length of the entire array, not the length of the pointer to the array. The address of an array name produces a pointer to an array, not a pointer to a constant. So the pointer returned after the &AMP;A is a pointer to an array, and a is different from the type of the pointer. Array names and arrays name addresses are identical in numbers, representing the address of the first element of the array.         But the particle size of the two is different. When the array isone-dimensional arrays, the array name is the granularity of an array element, expressed as "when the array name plus 1 o'clock, here 1 represents an array element unit", the example of the array element is an integer, so the array name plus 1 o'clockaddress plus 4, and the array name address & the entire array as a particle size, the expression is "when the array name takes address & plus 1 o'clock, here's 1 represents the entire array unit", the example array is an integer array with 5 elements, so the array name is address & plus 1 o'clock, add 20. When the array istwo-dimensional array, array name array, array[0], &array[0][0], and array name address & are the same in numerical values, and the granularity varies from one to the other. where array[0] and &array[0][0] have the same particle size, are the granularity of an array element, so they add 1, the address plus 4, and array names and arrays of names address & granularity, the former with a row of elements of granularity, the latter with the entire array of particles as the particle size, So the former plus 1, address plus 3*4, the latter plus 1, address plus 6*4.
Source:

the difference between sizeof and strlen:

	char* PTA;
	Char Arraya[6] = {"123"};
	PTA = Arraya;
	int num;
	num = strlen (Arraya);
	num = strlen (PTA);
	num = sizeof (Arraya);
	num = sizeof (PTA);

High num, respectively, is:
3 3 6 4 strlen represents the length of the array from the beginning to the first , and the array has the same granularity as the pointer, representing the result of the traversal of the address. The sizeof represents the length of the element occupied, and when the array's first address is passed in, the length is obtained, and the first address of the array is assigned to the granularity of the pointer passing in, and the length of the 32-bit address (4 bytes) is obtained, which is the special aspect of sizeof, Need attention.
An array that is directly assigned without initialization, an exception occurs when strlen:

int main ()
{
	char arraya[6] = {"12345"};
	Char arrayb[] = "12345"; 
	Char arrayc[] = {' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '};
 	int num1,num2,num3,num4,num5,num6;
	NUM1 = strlen (Arraya);
	num2 = strlen (Arrayb);
	num3 = strlen (ARRAYC);
	NUM4 = sizeof (Arraya);
	NUM5 = sizeof (ARRAYB);
	NUM6 = sizeof (ARRAYC); 
	
	return NUM1;
}
The NUM1 to NUM6 values are: 5 5 6 6 5 strlen and sizeof differ in that the strlen looks for the most recent one and records the length between the beginning and the first. Arraya and Arrayb Use string assignment, default with ' "" end, so the correct length when using strlen, and ARRAYC using a separate character to assign value, so use the strlen to find the closest ' "", this ' 0 ' is not reliable, so the value 13 is meaningless, and the correct length is obtained when sizeof. The best way to solve strlen is to use Memset to initialize the array position to 0

So it can be explained as follows:
	char Const *keyword[] = {"If", "Else", "while"};
	int num1,num2;
	NUM1 = sizeof (keyword);
	num2 = sizeof (keyword[0]);
NUM1 and num2 are: 12,4 keyword[] Array contains a number of pointers to the string, is a typical array of pointers, so the use of sizeof to obtain the length of the pointer length (that is, 4) times the number of pointers, so NUM1 = 4*3 =12, num2 = 4*1 = 4
\ the difference between before and after parameters are passed in:When you pass an array as a parameter to a function, the function uses the array in a formal parameter, which, although it exists as an array, is actually a pointer. Need to be aware






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.