Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Are spaces allowed when initializing an array?
Are spaces allowed when initializing an array?

When initializing the array, you are also allowed to add a comma (,) at the end of the array, for example:

int month_lengths[] = {'31','29','31 ','30','31','30','31','31','30','31','30','31',}; ??

Because in the branch In the case of , the compiler passes the comma as a flag, so this is legal.

The above structure can be written in the following form:

int month_lengths[] = {'31','29','31','30','31',' 30','31','31','30','31','30','31',}; ??

There is another point that must be noted, the expressions of characters and strings Not the same. Single quotes are used for character expressions, while double quotes are used for string expressions. This is a mistake that many people, including C veterans, love to make. Writing a newline statement will

be written as:

printf('\n'); , but when performing character judgment operations, it will be written as:

int c;

[...] 

if((c = getchar()) != "\n"){

do something here 

}

[...]

This kind of low-level mistake is easy to avoid, but if you are not careful, you will make mistakes, and there are many of them. It often appears in textbooks, reference books, and some literature and online texts. You can find it as long as you are careful. This may not seem like a big problem, but

If it is an embedded system, especially a real-time system, it may cause the system to crash, so don't take any chances when writing programs, and you must be cautious about every detail. Programming is not a technology, but an art.

Let’s take a look at the pointer.

A pointer is a variable that stores a memory address. From this definition, we can have a concept like this: The most direct element related to pointer operations is the address, the memory address. The address here refers to the location of another variable in memory.

A variable is said to point to a second variable if it contains the address of a variable.

Variables intended to store pointers must be declared before use. The declaration of a pointer consists of a base type, an asterisk (*), and the variable name. The general form is:

type * name;

Among them, type is the base class type of the pointer, which can be any valid data type, and name is the name of the pointer variable. Of course, type *name and type * name are equivalent, and general compilers will ignore the spaces in the middle when compiling the program.

The pointer's base class defines the types of variables that the pointer can point to. Technically, any pointer type can point to any location in memory. However, pointer operations are based on the type of the pointer, which means that the type of the pointer variable and the variable at the address it points to must be compatible. That is, the following operation is wrong:

int *p;

char ch = 'A';

p = &ch;

This is wrong because the types don't match. Actually, this sentence is not entirely correct. This is just a standard, the specific implementation depends on your compiler. The above operation can be passed on many compilers, such as Win-TC, LCC, etc. (It passes directly under Win-TC, but there is only a warning under LCC). However, the portability of a program designed in this way must be very poor. Also, if you want to store the pointer address to an array element during operation, if the types do not match, then you have to cast the data type of the pointer address, and direct assignment is impossible. Therefore, we advocate the adoption of standard regulations for operation. The following operation is also wrong, although the types match:

int *p;

int i;

p = &i; < /p>

This is wrong because i was only declared by the user, but no memory was allocated. A variable can only be allocated to memory when it is initialized (in most cases, it is allocated to memory after initialization, but if there is insufficient memory, it is impossible to meet this requirement). There is also a misunderstanding in pointer declaration, that is, it is mistakenly believed that three pointer variables are declared in the expression int *p,m,n; In fact, only the first (p) is a pointer variable, and the other two Each (m, n) is just an int type variable.

Only in this way can several pointer variables be declared at the same time: int *p, *m, *n, ...;

Pointers can also be initialized like ordinary variables, but you cannot give each pointer one by one. Direct assignment. For example:

int *p;

p = 10; is just wrong. However, you can assign the pointer to null, that is,

int *p = 0; or

int *p;

p = NULL;

Because the macro NULL is defined in many C language header files, it is a null pointer constant, so our expression is legal and has the same effect as the one above.

We can assign an array variable to a pointer, because they are just a mapping of addresses. This is our theme. For example:

char str[100], *pointer;

pointer = str;

Here, pointer points to the first element of the array str, which is the same as str[0 ] or accessing the variable str is essentially the same, operating on the first element of the array, the element at position 0. If you want to access the 10th element of the str array, then the operation is as follows:

str[9] or *(pointer + 9) This is equivalent. There are also ++, -- operations in pointers, especially when pointers and arrays interoperate, they are most likely to be used because the operations are very flexible. For example:

#include 

int main(){

char hello[] = "Hello, world!";

char *p;

p = hello;

do{

printf("%c", *p);

p++;

} while(*p);

printf("\n");

return ;

}

This is a typical example of using the "increment" operation of a pointer, and is also a good example of array combination pointer interoperation. Your operation can also be:

p = &hello[3];

*p = hello[3];

*&hello[3] = * p;

printf("%d\n",*&hello[3]);

hello[3] = *p;

printf(" %d\n",&hello[3]);

hello[3] = *p;

printf("%d\n",hello[3]); < /p>

And so on, the values ????of *&hello[3] and &hello[3] are the same, and they both read the address of element 3 (the fourth element) in the hello array. The statement p = &hello[3]; assigns the address of element 3 to the pointer variable, and hello[3] = *p;

assigns the value of pointer p to hello[3] . This is also a way of interoperating.

Note: *++P, ++*p and *p++ and *--p, --*p and *p-- represent different results. Because the priority of ++ and -- is higher than the priority of *, the previous expressions are equivalent to *(++p), *(++p) and *(p++) and *(--p), * (--p) and *(p--). It is recommended to remember the priority of operators firmly, because if you don't pay attention to these details, problems will arise that we don't expect.

Both arrays and pointers can be treated as parameters, but the probability of using pointers is higher because pointers are more flexible than arrays.

For example, you can pass an array or pointer variable as follows:

#include

int main(){

char hello[] = "Hello, world! \n";

char *p;

p = hello;

printf(hello);

printf(p);

printf("%s", p);

printf("%s", hello);

do{

printf("%c", *p);

p++;

} while(*p);

printf("\n"); < /p>

Return ;

}

It is completely passed in LCC, and the output result is:

Hello, world!

Hello, world!

Hello, world!

Hello, world!

Hello, world!

But in On some compilers, the statement:

printf(hello);

printf(p);

may only output the first element of the string. Because some compilers process strings by "intercepting" them, the result may be:

HHHello, world!

Hello, world!

Hello, world!

p>

Hello, world!