C and pointers--different c from Tan Haoqiang

Source: Internet
Author: User
Tags mathematical functions

Nothing to do, look at the "C and the pointer."



If someone tells you that Mario can use C to write a matrix collision judgment, you can also write your own Mario when the power comes.


In fact, C can write this.



C Chaotic Coding contest works, Don Yang, USA (feeling is a anime curtilage).


A C is a free form of language, a better coding standard is:

two = one + one;

In fact, the following freedom is also possible, so the avatar is not strange, you can:

Two
= one
+
one

So here's the same thing:

int* A;  int *a;


(ii) three letter words

int main (int argc, char *argv[])
{
	printf ("Hello??"). (:");
	return 0;
}

Do you think he's going to show a cry, no, look, face:

Hello [: Please press any key to continue ...

This is the three-letter word (TRIGRPH),?? (together to represent [.


Tan Haoqiang Teacher's book has not programmed me into the programming world, laying the groundwork is very good, but back to look at, there is really a shortage of places.


In fact the program appears many int a,b,i,j. The problem exists with the variable name of the random, resulting in later code to look at the time to forget what variables are used, so name to note. However, the book as a demonstration, there is no need to pay attention to so much.


(c) + = and =

int number;
Number = number +1;
Number = 1;
The efficiency of two is basically the same.
int number;
while (1)
  number++,
  number++;

However, encountered

number[3f (x)]=number[3f (x)]+1;
number[3f (x)]+=1;
There is a difference, the compiler does not know the result of f (x), also do not know each time the result is not the same, so he will calculate two times, so that + + two times than the calculation of f (x) efficient.


(iv), the magical

int number;
while (1)
  number++,
  number++;

Do not use {}, but not recommended, because difficult to distinguish, or to have a good coding style.


(v) Left value right value problem

The so-called L-value and r-value,l=r, but L can also refer to location, addressable value, R to read, readable value.

A = B + 1;

B+1 is a value, a has a specific hosting address and can be assigned a value. But

B + 1 = 1;

B+1 as a value that can be determined, is stored in one place, but unknown, the computer does not allow you to put the value in a place you don't know.


(vi) Pointers

In a computer, each byte (byte) has 8 bits (bit), and one bit is 0 and 1.

int a = 1;
int *b = &a;
If A's address is 100, then &a is the address of a.

The second statement can actually be assigned the same value:

int *b;
b = &a;
b is a pointer variable that points to the shape, and assigns A's address to B, then B stores the address of a, *b is the value that a address points to, that is, 1. After you know this, it's easy to understand later.

* (b+1)
Refers to the next value after the B pointer.

int A;
*&a = 1;
A = 1;
Is the third statement the same as the second one? Yes, the same, but the second one is trouble writing.

&a,a address, *&a,a's address points to a value of 1, that is, a value of 1.

#include <stdio.h>

int main (int argc, char *argv[])
{
	int c[5] = {1,2,3,4,8};
	int *d = &c[4];
	int *e = &c[1];
	int f = &c[4];
	int g = &c[1];
	printf ("%d\n", &c[4]); 
	printf ("%d\n", &c[1]); 
	printf ("%d\n", *d);
	printf ("%d\n", *e); 
	printf ("%d\n", d-e);
	printf ("%d\n", &c[4]-&c[1]); 
	printf ("%d\n", f-g); 
	printf ("%d\n", c[4]-c[1]); 
	return 0;
}

Run Result:

1245000
1244988
8
2
3
3
6
Please press any key to continue ...

The subtraction of pointers and pointers requires two pointers to the same array, in fact &c[n] is a pointer, the result of the two pointer subtraction is the memory distance of two pointers divided by the length of the array type, in fact, is the difference between the subscript. After the cast of the integral type, it becomes the difference of two integers, and it does not have any relation to the pointer subtraction.

+ + and Pointers

The *cp++  //++ priority is higher than the value that the copy of the *,CP pointer points to, and the CP next time points to
the next *++CP  //To the value pointed to by the copy of the next pointer to CP.


(vii) Dynamic memory allocation, data structure and so on.

Figure out the portion of the C memory allocation first. Stacks (compiler automatically assigns release, holds function parameter values, local variable values), heap (released by itself), global area, literal constant area, program code area.
Why the data structure is used malloc, is because they want to allocate how much, control the size of the space, relatively free. Understand this and pointers, the data structure is good to do.
(eight) preprocessor
The role of define can be replaced with text in a program. So-called macros, define allows you to replace the arguments with the text, such as:

#define DOUBLE (x)  x*x
int a = 1;
printf ("%d", double (a+1));
Double will replace the above expression, but the result here will be a problem, because based on the argument, then it will be equal to a+1*a+1, see no, the result is not what we want. So, you should include parentheses on the expression.
There is also a good place to use.
#define MAX (A,b) (  (a) > (b)? ( A):(B))
There is no type limit, because macros are replaced, which is equivalent to inserting code all in, if the macro is simpler, it is efficient.
(ix) input/output stream
Tell me something about EOF, which I haven't seen before. http://blog.csdn.net/ltx19860420/article/details/5526828
Turn one, in fact before in writing GetChar and Putchar when ye feel very strange, why not we lose a character to print a character, you have to return it to show.
(10) The standard function library stdlib.h is an int rand (void) with a random number function; There are also call libraries for mathematical functions: MATH.H, Time functions: Time.h. The details are not detailed.

The original Tan Haoqiang C when the Enlightenment book is very good, C and the pointer is a strengthened version.

Recommended after the book: "C + + meditation Record" "Expert programming," "C Traps and defects", C + + Templates


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.