Good use of your C (2)

zhaozj2021-02-16  59

Previous article discussed the expression of expressions, many of whom pointed out that the last example had problems A & 0x80 A is 16 bits. I am

It is also negligent, but it is not a problem if the value of the value is between -127 and 127, because in addition to the last 8

The bit and symbolic bits should be the same, but if you are more convenient for maintenance, you should not write this way. ??? The following we have entered the theme of this article - the discussion of pointers. The pointer is one of the most prominent functions in C. If it is not a flexible use of pointers, it cannot be

Write a good C program. ??? The pointer is a storage unit that stores the memory address. Let's discuss the pointer variables and pointer constants. Let's take an example: #include

void main ()

{

? int A = 5;

? INT * P = & a;

PRINTF ("P =% D / N", P);

? Printf ("& a =% d / n", & a);

? p ; // compile

? (& a) ; // error

}

We can see that the value of P and & A is equal, this shows that P and & A are the same address, but why is it wrong (& a) ? This is because & a only

It is a constant that it is not stored in the variable, so we can't assign him. If you want a pointer variable, you can not change as long as it is defined.

Plus Const is ok. In addition, we will want to use the pointer to change the value of the arguments directly, for example: #include

Void Add (INT * A, INT B);

void main ()

{

INT A = 5, b = 2;

Add (& A, B);

? Printf ("p =% d / n", a); // a equal to 7

}

Void Add (int * a, int b)

{

? * a = * a b;

}

This can directly change the value of A. However, if we don't want to change the value by the pointer, you can use const, this situation is in the array as a parameter.

It is often used. #include

Void Add (int * a);

void main ()

{

INT A [2] = {5, 2};

add (a);

? Printf ("a [0] =% D, A [1] =% D / N", A [0], A [1]); // a [0] is equal to 7

}

Void Add (int * a)

{

? * a = * (A 1);

}

Here we did not use const, the value of array elements changed. Many times we don't want to change the value of the array, we only need to write:

#include

INT Add (int const * a);

void main ()

{

Int a [2] = {5, 2}, result;

result = add (a);

PRINTF ("Result =% D / N", Result);

? Printf ("A [0] =% D, A [1] =% D", A [0], A [1]);

}

INT Add (const * a)

{

RETURN * A * (A 1);

}

This const can prevent us from modifying the elements in the array.

Since the array is mentioned above, we come to the relationship between arrays and pointers. Array is a series of storage spaces adjacent. The array is indicating

Use?? [Subscript]?, This representation is not limited to arrays, and any continuous storage space can be used to access it.

. For example: #include

#include

void main ()

{

? INT * P = malloc (8);

? p [0] = 5;

? p [1] = 2;

? Printf ("p [0] =% D, P [1] =% D / N", P [0], P [1]) ;? Printf ("P =% D, P 1 =% D" , & P [0], & P [1]); // From here you can see that the two pointers are adjacent

? free (p);

}

From this example we can see that P [1] is equivalent to * (p 1).

??? Finally, let's talk about the usage of a string in C. String is actually an array of characters, using a string and using an array. word

Strings can also be accessed by subscript: #include

void main ()

{

CHAR STR [] = "this is a string";

INT i;

? for (i = 0; i <17; i )

?? Printf ("% C", STR [I]);

PRINTF ("/ n");

? for (i = 0; i <17; i )

?? Printf ("% C", "this is a string" [i]);?

}

??? Well, the introduction to the pointer is here, the next article we discuss the polymorphism in C.

??????? 2004.7.1

转载请注明原文地址:https://www.9cbs.com/read-18356.html

New Post(0)