Arrays and pointers

Source: Internet
Author: User
Tags sprintf

The end of this section. ==================================================================

Array type: is a complex type, (composed of array element types and arrays of length) as follows; The array type is: (int []);

The nature of a type is the alias of a fixed-size memory block. is the mold.
I. For one-dimensional array C language provisions: int c[10];

1. One-dimensional array name: C is the address of the first element of the array, the c+1 step is 4 bytes, and the size of an int element.
&c is the address of the entire array, with a &c+1 step of 40 bytes and 10 int element size
Although the values of C and &c are the same, they represent different steps.
2. The element I elements of the array means: c[i] or * (c+i); Assign a value to it. C[i] = 5 or * (c+i) = 5;

The end of this section. ==================================================================

Two. Multidimensional array (int a[3][4])
1. Multidimensional array Name: A is an array pointer to a one-dimensional array, the step of a is one-dimensional length (that is, +1 jumps a row at a time) (16 bytes, four int element element size)
2. (a+i) represents the address of the entire line I, (representing a level two pointer)
* (A+i) represents the first line element address (representing the first level pointer)
* (A+i) +j or (&a[i][j]) on behalf of the address of the first line J element
* (* (a+i) +j) or (A[i][j]) represents the value of the first J element of line I.

The end of this section. ==================================================================

Three. Degradation of function parameters in multidimensional arrays
"The array will degenerate into a pointer when it makes a function parameter": sizeof (a) = = 4;sizeof (b) = = 4; is a pointer size, not an array size.
"Only each degenerate array (for pointers) may have a different step size."
All of the data is stored linearly in memory. Include multidimensional arrays
1. The argument passing of one-dimensional array (the argument passes the value to the formal parameter):
int fun (char a[20],int N);(fun is the function being tuned)
int fun2 (char b[3][4]);
sizeof (a) is 4 bytes, and sizeof (a)/sizeof (*a) is 4 (because *a is a char-type element, one byte);
1) Efficient
2) C language Processing A[n] when (formal parameter), it has no way to know that N is a few, it only know &a[n] (that is, the address of a) is how much, its value as a parameter passed in.

2. Equivalence Relationship:
Pointer parameters equivalent to array parameters
1) One-dimensional array char a[30] Pointer char *
2) pointer array char *a[30] Pointer to pointer char **a
3) pointer char (*A) of the two-dimensional array char a[10][30] array [30];

The end of this section. ===================================================================

Four. The miscellaneous point of the pointer:
1. "The nature of the pointer: the address that points to memory", "no memory, no memory address, no memory address, no pointer".
2. "Indirect assignment is the greatest meaning that a pointer exists."
3

1. The nature of the data type: Aliases that account for fixed memory size. Type is the mold, the variable is the mold produced by the physical
2. The nature of the variable: (a contiguous) the alias of the memory space (is a house number)
3. Contents of the array: such as the address of the first element of the Arr[5];arr array space, &arr represents the first address of the entire array,
arr+1; The address of the second element of the array, moving down by four bytes.
The &arr+1; represents a 20-byte downward movement, which is the size of an array of memory space. &arr and arr have the same values, but +1 of the results are different because they are essentially different data types
4. Where the pointer is pointing, assign the address to the pointer
5. The pointer variable and the memory space variable it points to are two different concepts ("don't understand")
6. Note: Return is not the memory block 64 bytes to return, but the memory block of the first address (memory block designator 0x23124) to return out.
(The following code: 17 rows and 27 rows output the same value)
#include <stdio.h>
#include <string.h>

Char *func1 ()
{
Char arr[10];
strcpy (arr, "sdqwyg");
printf ("arr:%s\n", arr);
printf ("&arr:%p\n", arr);
return arr;
}
int main (void)
{
char *ch = NULL;
printf ("&ch:%p\n", ch);

ch = func1 ();
printf ("ch:%s\n", ch);
printf ("&ch:%p\n", ch);
return 0;
}

c Improve the first day of class ===============================
1. Structure byte alignment reason: Use space to change time.
2. The most important role of the operating system: resource allocation.
3. Handle: struct-body variable
4. Wild pointer Characteristics: The operation of the field pointer itself is not a problem, but cannot manipulate the memory space pointed to by the wild pointer
That is, you can assign any address to the pointer variable (int *p) p. such as (p = NULL; p = 0xaabb; p = 0x9988;)
However, you cannot manipulate the memory space pointed to by the wild pointer (this is the case in most cases). (*p = 100;*p = "SDB"; This situation is not possible.)
5. Bubbling: for (int i = 0; i < n-1; i++)
for (j = 0;j < N-1-i; j + +)
if (A[j] <a[j+1])
6. Select: for (int i = 0; I <n-1; i++)
for (j = i+ 1; j< N; j + +)
if (A[i] <a[j])
7. Real parameter group name is an array, sizeof (array name) is the total size of the array
Parameter group name, which is the pointer. sizeof (array name) is the pointer size (the size of each operating platform differs).

C Raise the next day =======================================
1. Using the pointer to manipulate the memory that the pointer points to, two features
1) "First address"; 2) "Step (determined by the space the pointer is pointing to)"
"The most important meaning of pointers is indirect assignment"
2. If the value is passed, (that is, the variable itself is passed), any modification of the formal parameter does not affect the argument (except for the array name, because the array name is the first address of the array)
Formal parameters (variables inside the function of the being tuned); Arguments (variables inside the keynote function)
3. If it is a pass-through, (that is, the variable address is passed), any modification of the formal parameter will affect the argument
1) address passing, 2) the pointer to the parameter and the argument in the process of the parameter of the argument 3) the inner part of the modulated function is indirectly assigned by
4. Pointers do function parameters: have input and output characteristics
1) input attribute: the memory of the argument is determined, the memory is allocated in the main function
2) Output characteristics: Allocate space in the function
5. Character array initialization: (three cases)
1) char str1[] = {' A ', ' B ', ' C ', ' d '};
printf ("STR1:%s\n", str1);//Print out abcd+ garbled, because there is no Terminator ' n ' in the array;
2) char str2[100] = {' A ', ' B ', ' C ', ' d '};
printf ("str2:%s\n", str2);//print out ABCD, because this is equivalent to the partial initialization of the array, there is Terminator '


6. Through [] and through the * effect is equivalent to the operation of the pointer refers to the memory,
int a = 10; int *p = &a; printf ("%d,%d\n", *p, P[0]); The print effect is the same

C increases the third day ======================================================
1. Normal array (char p[4]); is an array, the element is a character.
Pointer Array (char *P[4]); An array that is a pointer to an element.
Their two arguments: 1) "Pass": Normal array: func (P,len) "Receive" void func (char *p,int len), or void func (char p[],int len); or void func (char p[4],int len);
2) "Pass" pointer array: func (P,len) "Receive" void func (char **p,int len), or void func (char *p[],int len); or void func (char *p[4],int len);
When you are unsure of the parameter type, if it is "value passing": You can put the prototype of the argument on it, then optimize it or not optimize it. such as "pointer array"
If it is "address pass", need to add a "*" on the argument prototype, and then optimize the live is not optimized.
2. Pointers and arrays If you do common variables, there are essentially differences between them. The
pointer and array are equivalent if they are the parameters.

3.sprintf usage: char buf[100]; format a string with the number 0 in the middle:

1) The string comes with a terminator (not formatted); sprintf (buf, "sadd\0dada\0sads\0"); then print the content: printf ("%s\n", buf); content is "Sadd";
2) format a string with a number 0 in the middle, with%d matching, because the Terminator (' + ' or ' 0 ') occupies one byte because it occupies four bytes;
sprintf (buf, "sadd%ddada%dsads%d", 0,0,0);p rintf ("%s\n", buf); Content is "SADD0DADA0SADS0"
3) format a string with the number 0 in the middle, match with%c; sprintf (buf, "sadd%cada%csads%c", 0,0,0);
printf ("%s\n", buf);
printf ("%s\n", buf);
int len = sprintf (buf, "sadd%cada%csads%c", 0,0,0); its return value is important: the length of the entire string;

C Raise the fourth day =================================
1. Features of XOR (same 0, different 1): 1) Self and own XOR or 0;2) a number per bit and 0 XOR: Unchanged
3) A number per bit and 1 XOR: Take the reverse

2. Move left <<: (left Discard, 0 on right). Shift left N-bit ("No cross-border case"), which is equivalent to the number of times N of the origin multiplied by 2
Move right >>: (Discard on right, 0 on left).
3. Mask: Through the continuous composition of 0 and 1, 0x0000110,
For example (0X00FF00), the value of certain parts can be changed by a mask.

4. One-dimensional array name: int a[10]; printf ("a:%d,a+1:%d\n", a,a+1);
printf ("&a:%d,&a+1:%d\n", &a,&a+1), two different printing conditions
Although the values of a and &a are the same, their array types are not the same.
A: Represents the first element address, the No. 0 element address, &a[0], and the No. 0 element is int, +1, or 4 bytes.
&a: Represents the address of the whole one-dimensional array, the length of the entire array;


Array type

5. "Pointer array": is an array, each element is a pointer. int *p[10];
"Array pointer": is a pointer to an array of pointers. int (*p) [10];
There is a "typedef" is a type, no "typedef" is a variable
Three ways to define array pointers:
1) define the array type first, and then define the pointer variable (not commonly used) based on the array type
int a[10]; typedef int ARR[10]; ARR *P1 = &a;
P1, &a address of first line address: *P1;
The address of the first element *p1 +i; Take the contents of the elements of the first element: * (*p1+i);
2) Direct definition of array pointer variables (common)
int a[10]; int (*p) [10]; p = &a;
3) Define the array pointer type and then define the variables according to the type (common)
int a[10]; typedef int (*arr) [10]; ARR p; p = &a;
6. Multidimensional arrays
int a[3][4];
1) Multidimensional array name "A", representing the address of the No. 0 element (equivalent to the first element address of the entire multidimensional array or the first line element address)
"A+1" jumps four elements (Int[4]), 16 bytes.
2) Multidimensional array name "&a", representing the address of the entire array.
"&a+1" jumps the entire array size (int [3][4]), 48 bytes.
sizeof (a) = = 48; sizeof (a[0]) = = 16; sizeof (a[0][0]) = 4;
3) A multidimensional array name is equivalent to an array pointer, as above; (int (*P) [4] = A or int (*Q) [4] = NULL, q = a);
4) Special case, when (array pointer) is different from (the step of array name of multidimensional array);
""

5) "Level two pointer and two-dimensional array is not the same type";


C Raise the fifth day =================================================================

"When defining a struct variable, the definition statement is written in the definition area of the function, and if written elsewhere, because of compiler problems, it causes the definition variable to be unsuccessful and the compiler does not know."

"There can be direct assignment between struct variables, but you need to be aware of deep copy and shallow copy problems" (explained below)
1. Structure:
Stu *s1 = NULL;
Stu p = {, "Nihao", "Sada"};
S1 = &p;//Here is the attention to the wording, the structure of the pointer variable overall assignment. (Can not be written as *s1 = P, the reason *S1 is already started to take the structure of the content, because S1 is not assigned, equivalent to the wild pointer, can not be taken value);
2. The name of the struct is a normal variable (no difference from a in int a);

3. Assignment operations to pointers in structs and assignment operations for arrays. (Special attention)
ypedef struct STU
{
int age;
Char name[20];
Char *p;
Char *size;
}stu;

int main07 (void)
{
Stu *s1 = NULL;
Stu p = {, "Nihao", "Sada"};
S1 = &p;//Here is the attention to the wording, the structure of the pointer variable overall assignment. (Can not be written as *s1 = P, the reason *S1 is already started to take the structure of the content, because S1 is not assigned, equivalent to the wild pointer, can not be taken value);

printf ("%d,%s,%s\n", S1->age, S1->name, s1->p);

printf ("\ n");
System ("pause");
return 0;
}

Arrays and 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.