C-Language programming learning notes under Linux

Source: Internet
Author: User

Video See CSDN College Mr Wang and the following Linux environment under the C Language Programming Foundation quite classic, wherein Mr Wang video speaks very well, quite classic

Compiling the hellogcc.c depends on the header file in the/home directory

In order to avoid the same file being include multiple times, there are two ways in C/s + +, one is #ifndef and the other is #pragma once mode. There is not much difference between the two compilers that can support both, but there are still some subtle differences.

Way One:

#ifndef __somefile_h__

#define __somefile_h__

...//Declaration, definition statement

#endif

Way two:

#pragma once

...//Declaration, definition statement

#ifndef的方式受C/c++ language Standard support. It does not only guarantee that the same file will not be included more than once, but also that two files (or snippets) with exactly the same content will not be included accidentally.

The above defines a define_test.h header file

use extern variables and functions that reference external file definitions

using the static declared variables and functions that use extern outside the is inaccessible. ,

The shaping variable stores the shaping data,

The pointer variable stores the address

+ bit corresponding to the pointer variable is 4 of bytes

- bit corresponds to the pointer variable when 8 of bytes

PValue is a pointer variable, in bit of the computer on sizeof is always 4 a self, in the bit on the result is 8 a byte.

*pvalue The value is 0x01

pvalue+1 address 0x00012b40+sizeif (char) * * = 0x00012b41

* (pvalue+1) The value is 0x02

0x00102000 This address is not malloc . application, may be the address of the system, we write to the address of the value of the space, will cause the system to collapse

Above is a kind of wild pointer, may cause the program to crash.

above Pvalue can access four bytes, but as long as the 0X00012B40 It's been applied, 0x00012b41 . , 0x00012b42 , 0x00012b43 The address space inside may store very important data, you stored inside, it can cause the system to crash.

Above this pattern is a large segment of storage

Use the following code to:

Signal is a function that has two parameters, one is an int , one is handle function pointer, the return value of the function is an address * , such as an address of AA . it becomes int (*AA) (int y)

The pointer compares the size of the address number, not the value in the comparison.

Like a mailbox, the mailbox has a number, each number of mailboxes stored in the value of different

The above-mentioned pointer to the content is read-only, it is not possible to send a change

Is wrong, cannot assign a value to the pointer, the pointer is a constant

Execute p1++ First The value in the execution pointer to the space.

The result of the output is 300.

The pointer to an array is the array name or the address of the first element of the array, and the array name is constant and cannot be added.

There's a problem with that, because P2 The pointer has moved the end of the string and should be P2 the B

The first case: the contents of the array are Hello , the length of the array is 6 , the string ends with '/0 '

The second case: the contents of the array are Hello , the last one '/0 ' cannot be stored, the length of the array is 5

The third case: the contents of the array are hell0 , the length of the array is ten

The above analysis is wrong, run the code when the error

Error when running, so this place array name cannot represent the address of the first element

The result of the above output is 4. and , if the array represents the address of the first element, then an array is an address, the result of the output should be 4 rather than , but output here , description array represented in this place is the address of the entire array

In this case, the array name represents the entire array

Summarize:

the above array name is in sizeof and & after that, the description of the array name represents the entire array

The first case is to ask for the entire array of storage space

The second is to take the entire array of addresses, to ensure that the entire array of space

this scenario, array represents the address of the first element

Above is the right quite the classic

Example 2 :

Ptr represents the address of the first element in a two-bit array, which is an array with three elements

ptr+1 represents the address of the second element in the array

3 :

above the *ptr the address of the first element is the array name. , the value of accessing the first element is **ptr

the address of the second element is *ptr+1, get the value of the second element is * (*ptr+1)

above *ptr=array[0],* (ptr+1) equals array[1]

Lvalue Store left the variable address 0x000012b4 address for which storage space is allocated during compilation

The right value is running when you know

The program generates the above symbol table at compile time, recording the array and Temp the corresponding memory address

so the array is directly accessible, because the pointer is stored for 2 times, so called indirect access

If the array is accessed as a pointer, the following error occurs:

A the ASCLL is the value of B it corresponds to the

Extern Examples of

in A.C an array is defined in the B.C use extern in declare this array and print out the contents of the array

However, the following situation will cause an error, and an array as a pointer to access

C-Language programming learning notes under Linux

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.