Explain functions arrays and Pointers in C

  • 2020-05-17 05:55:55
  • OfStack

1. Function: when the program is small, we can use an main function to solve the problem. However, when the program is large, it is beyond the range of human brain and the logic is not clear.

Function is the basic component of C language code. It is a small module, and the whole program is composed of many modules (functions) with independent functions. This is the basic differentiation of programming;

(1) the key to writing a function:

Function definition: the definition of the function is the realization of the function, the function definition contains the function body, the code segment in the function body determines the function function;

Function declaration: function declaration is also known as function prototype declaration. The prototype of a function consists of three parts: function name, return value type, and function parameter list. The declaration of a function tells the user of the function what parameters should be passed to him when the function is used.

What type of return value does it return? These are all things that the person who wrote the function says in the definition of the function, and if the person who is using the function doesn't use the prototype, it's going to go wrong, and it's not going to look like what you thought it would;

Function call: function call is to use the function name to call the function to complete the function. When calling, you must refer to the prototype to pass parameters to the function, and then get the appropriate return value from the function as the result.

(2) function parameters: the process of function call is actually a process in which arguments are passed to formal parameters. It's like a copy, the argument itself doesn't go into the function, it just copies its value and passes it to the parameter in the function,

In the function to participate in the operation, this method, is called pass value call;

Formal parameters: formal parameters, parameters in the function definition and function declaration in the parameter list are formal parameters;

Arguments: the actual arguments that are passed in a function call.

(3) return value (keyword return) : after the function is executed, a value will be returned to the place where the function is called. The type of this value is the return value type in the function declaration, which is the last sentence in the function body, return xxx; The value returned;

(4) function name: take the function name to pay attention to the following points:

First, the name can not be arbitrary, to comply with the rules, and this rule has two levels, namely, the first layer is legal, the second layer is reasonable. Legal is the name of a variable in C, reasonable is the name of a variable,

One can see what this means, and one can see what this function does;

Second, in C, all symbols are case sensitive;

Third, the naming convention of C language function names. There is no fixed conclusion to this, but there are a variety of widely used naming methods as follows:

Naming conventions for linux: student_agestr_to_int

Camel nomenclature: studentAgestrToInt

Note: you can refer to Lin rui's "guide to high quality programming" for further information.


//  Simple calculator 
#include <stdio.h>
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int main(void)
{
  int a, b, c;
  a = 54;
  b = 32;
  c = add(a, b);
  //c = sub(a, b);
  //c = mul(a, b);
  //c = div(a, b);
  printf("c = %d.\n", c);
  printf("a - b = %d.\n", sub(a, b));
  return 0;
}
int add(int a, int b)
{
  return a + b;
}
int sub(int a, int b)
{
  return a - b;
}
int mul(int a, int b)
{
  return a * b;
}
int div(int a, int b)
{
  return a / b;
}

2, array: array is a number of Numbers composed of a group, the number is a specific data type of variables, the group is said to be most placed in 1;

(1) definition of array:

int a [10]. Array name of element type in array [number of array elements];

Note: all elements in an array have the same data type. It is not possible to have two data types in an array.

(2) the use of array: the array is defined as a whole, but it cannot be used as a whole when it is used, and each element in the array must be separated when it is used;

For example, the array int a[10] USES 10 of its elements and a[0]... a[9], where [] is the symbol of the array, the number in [] is called the index of the array (index, index), and the index is our guide to access the elements in the array,

The subscript 0 represents the first element in the array, the subscript 1 represents the second element in the array, and so on. If the array length is n, the last subscript is n-1.

Note: when accessing the array, pay special attention to the subscript, which starts from 0. If the subscript exceeds n-1, it will generate an out-of-bounds access, and the result is unpredictable.

(3) array initialization: initialization is to make the object have a predetermined initial state, and there are two types of array initialization:

Type 1: full initialization. Assign values in turn;

Type 2: incomplete initialization. The values in the initializer start with a[0] and are assigned back, with the default of 0 filling in the assignment

(4) arrays of different data types:


 int a[3];      //  The integer array 
    float b[3];     //  Single precision floating point array 
    double c[3];    //  Double-precision floating point array 
    char d[3];     //  Character array 

(5) character array: when a single character is referenced in C, it should be enclosed in single quotation marks, such as 'a';

Character array initialization: when you define an array and initialize it at the same time, you can omit the length in the array definition []. The C compiler automatically deduces its length based on the number of initialized elements in the initializer.

Reference string: when referring to a string in C, you should enclose it with "", such as "abcde", where "abcde" actually has six characters: 'a' 'b' 'c' 'd' '\0';

'\0' is the symbol at the end of a string defined in C. This character is the first character in the ASCII code table. Its encoding value is 0 and the corresponding character is null (invisible).


//  Demonstration of arrays 
#include <stdio.h>
int main(void)
{
  int a[4];
  a[0] = 23;
  a[1] = 54;
  a[2] = 98;
  a[3] = 74;
  printf("a[1] = %d, a[3]= %d.\n", a[1], a[3]);
  return 0;
}

//  A demonstration of a character array 
#include <stdio.h>
int main(void)
{
/*
  int i = 0;
  //char a[5];
  //char a[5] = {'a', 'b', 'c', 'd', 'e'};
  //char a[5] = {97, 98, 99, 100, 101};
  //char a[] = {97, 98, 99, 100, 101};
  char a[] = "abcde";
  for (i=0; i<5; i++)
  {
    printf("a[%d] = %d  %c\n", i, a[i], a[i]);
  }
*/
  int i = 0;
  char a[] = {97, 98, 99, 100, 101};
  char b[] = "abcde";
  printf("sizeof(a) = %d, sizeof(b) = %d.\n", sizeof(a), sizeof(b));
  return 0;
}

3, pointer: the full name is pointer variable, its essence is a variable of C language. This kind of variable is special. Usually, its value is assigned to the address value of a variable (p = &a), and then we can use *p to indirectly access the variable pointed to by p;

(1) the significance of pointer: it can be indirectly accessed. Now that we have Pointers, we don't have to access the variable a just by the name of a. And you can do it through p = &a; * p = xxx; Such a way to indirectly access the variable a;

(2) two important operators: & and *

& : take the address character and add it before a variable, then the combined symbol represents the address value of the variable;

Such as: int a; int * p; p = & a; Assign the address value of the variable a to p

a stands for the variable a itself

p stands for the pointer variable p itself

&a represents the address value of the variable a

*p stands for the variable that the pointer variable p points to, which is the variable a

&p represents the address value of the pointer variable p itself. The symbol is legal but meaningless to the topic

*a treats a as a pointer, *a represents the variable to which the pointer is pointing, and this symbol is illegal

* : pointer symbol. Pointer symbol in the pointer definition and pointer operation, the parsing method is different;

int * p; Define the pointer variable p, where *p does not mean the variable to which the pointer variable p refers, but * means to tell the compiler that p is a pointer

* p = 0 x24; When using Pointers, *p represents the variable to which the pointer variable p refers

(3) definition and initialization of pointer:

Type 1: define and assign first

int * p; // define the pointer variable p

p = & a; // assign a value to p

Type 2: define and initialize at the same time

int * p = & a; // the effect is equivalent to the above two sentences

(4) pointer of various types: pointer variable is essentially a variable, and the type of pointer variable belongs to pointer type. int * p; A pointer type variable p is defined. The variable that p refers to is int.

int * pInt; // pInt is a pointer variable that is of type int

char * pChar; // pChar is the pointer type, and the variable to point to is of type char

float * pFloat;

double * pDouble;

Note: the various pointer types and the variable types they point to must match, otherwise the results are unpredictable.

(5) two ways to understand pointer definition:

int * p;

Type 1 (recommended) : first see p, which is the variable name; Secondly, p is preceded by a *, indicating that this variable p is a pointer variable; Finally, *p is preceded by an int, indicating that the pointer variable p refers to an int data;

Type 2: first see p, which is the variable name; Second, see int * before p, and understand int * as a whole. int * is a type (composite type), which represents a pointer to int data.

(6) preliminary combination of pointer and array:

Array name: when doing the right value, the array name represents the first address of the first element of the array, so it can be directly assigned to the pointer;

Such as: int a [10]. Where a and &a[0] both represent the first address of the first element of the array, a[0], and &a represent the first address of the array.

Note: the first address of the first element of an array is different from the first address of an array. The former is the address of the array element, while the latter is the address of the array as a whole. They have different meanings, but they're the same numerically;

According to the above, we know that we can use a pointer to point to the first element of an array, so that we can indirectly access the elements of an array one by one. Thus, there are two ways to access an array:

There are int a [5]; int * p; p = a;

Array access: a[0]a[1]a[2]a[3]a[4]

*p*(p+1)*(p+2)*(p+3)*(p+4)

(7) operation between pointer and ++ -- symbol: the pointer itself is also a variable, so it can be operated. However, since the pointer variable itself stores the address value of some other variable, it is meaningless to carry out * / % operation on this value.

Therefore, the addition of two pointer variables is meaningless, but the subtraction is meaningful. The pointer variable plus 1, minus 1 makes sense, plus 1 means the cell that the pointer is pointing to moves one space back, minus 1 means the cell that the pointer is pointing to moves one space forward.

*p++ : ++ combines with p first, but when ++ is put after, it means to add 1 after operation (operation refers to the operation between p++ and the previous *; The addition of 1 refers to p+1), so in fact *p++ is the overall external representation of the symbol

The value is *p, p plus 1.

*p++ equals: *p; p + = 1;

*++p = : p += 1; * p;

(*p)++, use () to force the combination of * and p, can only be calculated first *p, and then the value of *p as a whole ++

++(*p), first *p is evaluated, and then before ++, which is then used as the value of the whole expression


//  Pointer definition, assignment, and initialization 
#include <stdio.h>
int main(void)
{
  int a = 23;
  int *p = &a;
  *p = 111;
  printf("a = %d\n", a);
  return 0;
}

//  Use the pointer to access the array 
#include <stdio.h>
int main(void)
{
  int a[5] = {555, 444, 333, 222, 111};
  int *p;
  //p = &a;
  //p = &a[0];
  p = a;
  //p += 1;
  //printf("*p = %d.\n", *p);
  //printf("*p++ = %d.\n", *p++);
  //printf("*++p = %d.\n", *++p);
  //printf("(*p)++ = %d.\n", (*p)++);
  printf("++(*p) = %d.\n", ++(*p));
  return 0;
}

4. Supplement: the essence of variables and data types

(1) when a program is running in an environment, it needs a certain amount of resources, including: CPU(computing power), memory, etc. These resources are usually provided by the runtime environment (operating system), for example, when we run the program on linux system.

The linux system provides us with computing power and memory.

(2) the larger the program is, the more resources it consumes at run time. For example, the memory is rated. If the program is larger, the more memory it consumes.

(3) in the C program, the essence of the variable is a cell in memory. When we define a variable, we get a cell in memory, and the cell's name is the variable's name,

Later access to the memory grid will only use the variable name, that is the nature of variables;

(4) the essence of data type is the different types of cells in memory. For example, on a 32-bit machine:

The short shaping lattice (short) takes up 2 bytes, or 16 bits of space

The reshaped lattice (int) takes up 4 bytes, or 32 bits of space

The single-precision floating point lattice (float) takes up 4 bytes, or 32 bits of space

The double-precision floating point lattice (double) takes up 8 bytes, or 64 bits of space

The character grid (char) takes up 1 byte, or 8 bits

(5) sizeof operator: returns the memory occupation length of 1 variable or 1 data type, in bytes;


// sizeof A demonstration of the operator 
#include <stdio.h>
int main(void)
{
  int len;
  //len = sizeof(int);
  //len = sizeof(float);
  //len = sizeof(double);
  //len = sizeof(char);
  //double d;
  //len = sizeof(d);
  int a[5];
  //len = sizeof(a);
  len = sizeof(a) / sizeof(a[0]);
  printf("len = %d.\n", len);
  return 0;
}

Related articles: