Be a pointer and an array of C + + Masters

Source: Internet
Author: User

Pointer Preliminary

Define a pointer variable: int * a=NULL; int is the type of pointer, which is actually the type of data that the pointer points to.

When the program runs, the data is placed in memory, and since it is in memory somewhere in memory, that is the address of the data. This is the address that is stored in the pointer. So no matter what type of pointer, there is no difference between the pointer variable itself, the value is an integer value. The integer in the 32-bit program is 32 bits, and the integer in the 64-bit program is 64 bits. In fact, there is no difference between the pointer variable and the integer variable, it depends on how you explain it. An integer you can interpret as a pointer can also be interpreted as an integer, so pointers and integers can be easily converted.

You can simply assign an integer value to the pointer when you define it, indicating that the pointer points to the address location represented by the integer, and how does the data in this location explain it? Just look at the type of pointer. But generally we can't do this, so it's easy to crash, because the location you randomly specify is not a valid location. So before using the pointer, let the pointer point to a valid position, for example, a=&b; b is an int variable. "&" is the address of b , at which point A is directed to a valid address.

Using the data pointed to by the pointer requires a "*a" approach, such as it *a=100; looks like using variable B in an indirect way. The scanf () function used in our calculator program is a pointer to the parameter that receives the user's input value, but the pointer must point to valid memory to receive the data, or it will cause a crash.

Distinguish between a good pointer and the data that the pointer points to. The pointer itself occupies 4 bytes or 8 bytes, and the data pointed to by the pointer does not necessarily account for the number of bytes. For example, pointing to a char type of data, the pointer points to only one byte of the content. The pointer may also point to a chunk of memory, such as the one requested by malloc on the heap.

The destruction of the pointer variable itself does not affect the pointer to memory, which means that their lifetimes are completely irrelevant. The general pointer is a temporary variable (allocated in the stack), if the pointer to the memory is allocated in the heap, when the function returns the pointer variable will be automatically destroyed, and its point of memory is not released, then the block of memory can no longer be freed, so it becomes a memory leak.

The relationship of the pointer to the content you are pointing to is as follows:

The middle red is the value of the pointer D , which is exactly the address of the first byte of the variable a , which is the address of a . This is the case with 64-bit programs, and you can see that the address occupies 8 bytes.

That's what it looks like in VS , inqtcreator :

Note that this is a two program, so a does not have the same position in memory, so the value of D is different.

The array variable itself is actually a pointer. But the compiler treats it as an array, such as when sizeof () evaluates a pointer for 4 or 8, and when an array is computed, it gets the total number of bytes in the array. At the same time, the array variable can be used as a pointer entirely.

For a detailed explanation of pointers, please refer to video tutorial: http://edu.csdn.net/course/detail/2318

Array

An array is a lump of the same type of data arranged together, because each element occupies the same number of bytes, so according to the ordinal of an element can be calculated in the memory of its position, so the array elements can be accessed by ordinal.

    //定义一个float型数组,数组的类型表示其每一项都是一个float型值    //数组有19项,这个数组 占19x4 个字节。没有初始化,数组每一项的值都是随机值    float farr[19];    //没有在中括号中指定数组的数量,但对数组进行了初始化,从大括号中可以    //数出数组的项数。    float farr1[] = {55.344.2533.3};

Why do we use arrays? For example, to write a function that asks for an average of 10 numbers, then this function will have 10 parameters. What if we ask for an average of 100 numbers? It's hard to give it 100 parameters? This is certainly unrealistic. If we put these 100 numbers in an array and then pass the array to the function, then just one parameter is enough. As follows:

#include <stdio.h>//front-facing statementfloatAverageintNumbers[]);//Entry functionintMainintargcChar*argv[]) {//Define an array, give 10 elements    intArr[] = {1, A,3,4, the,443, +, $, the, at};//Call averaging function    floatAvg = average (arr);//Print average    printf("%f\n", avg);return 0;}//Averaging function, parameter is an arrayfloatAverageintnumbers[Ten]){///first to find the elements in the array and    intHe =numbers[0]+numbers[1]+numbers[2]+numbers[3]+ numbers[4]+numbers[5]+numbers[6]+numbers[7]+ numbers[8]+numbers[9];//Averaging    floatRET = he/10.0f;//Return results    returnRET;}

Previous: Become a C + + Master of the final version of the calculator

Be a pointer and an array of C + + Masters

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.