Introduction to pointers and arrays in C/C ++ (1)

Source: Internet
Author: User

 

Pointers are the essence of C/C ++, and pointers and arrays are a happy family. In many cases, we cannot distinguish between pointers and arrays, few new students in the computer department are familiar with pointer and array usage and differences. This cause may be related to university teaching and many popular C or C ++ tutorials on the market. Although these tutorials are easy to understand, however, in many key aspects, we have avoided talking about it or made it unclear at all. In many cases, we have made a wrong point of view. Generally, this type of tutorials are used when I first learned C/C ++, and the learning effect can be imagined. It is really important for beginners to choose a good tutorial, because first-in-first-class, once you accept the wrong point of view or idea, even if you know it later, it is very difficult to correct it (I have a deep understanding ), here I recommend three tutorials suitable for beginners:

The C Programming Language Brian W. Kernighan and Dennis M. Ritchie (K & R Bible)

C ++ Primer Stanley B. Lippman, Jos é e Lajoie, Barbara E. Moo C ++

Pointers on C, kenth A. Reek

In many cases, someone may say "pointing to the same array is not completely correct. In a certain context, pointers and arrays are equivalent, not in all cases. However, many people naturally ignore the conditions for such a situation and assume that this is true in all cases. The following describes the differences between pointers and arrays.

I. Definition of pointers and Arrays

Pointers are pointers, and pointer variables store an address for indirect data access. In a 32-bit system, a pointer variable (including void pointers) always occupies 4 bytes of space. Pointers can point to any memory space, but not any memory space can be accessed through pointers.

An array is an array. After defining an array, the compiler will open up a continuous space in the memory to store data based on the type and number of the array elements, so as to directly access the data.

The following is an example.

The following code is available in file1.c:

char p[100]="abcdef";

The following code is available in file2.c:

#include<stdio.h>extern char *p;int main(void){    printf("%c\n",p[1]);    return 0;}

Can it be compiled but executed correctly? Debugging found: this error occurs and the value of p [1] cannot be calculated. The cause will be explained later.

From this we can see that the pointer and the array are not equivalent, and the definition of the array is not equivalent to the external Declaration of the pointer (note the difference between the Declaration and definition, the definition is to allocate memory space for a variable or object, and the declaration is only a description type ).

Ii. Differences between pointer and array access

References to the array objects:


Pointer Reference:

From the figure above, we can see that pointers and arrays are totally different. For arrays, since the compiler knows the address of each symbol during compilation, if you need an address to perform some operation, you can directly perform the operation, you do not need to add a command to obtain the specific address first, as for arrays. For pointers, you must first obtain the specific value of the pointer at runtime before reference. From this point, we can explain why the above program cannot be correctly executed, because the p defined in file1.c is an array, but it is declared as a pointer in file2.c. Therefore, in file2.c, p is a pointer variable by default, and any data in the pointer variable is processed as an address. Therefore, the first four bytes of the original array are obtained: 0x61 0x62 0x63 0x64 constitute an address (not considering the size of the end) 0x61626364, and then read the content in the address 0x61626364 according to the char type, but this address may not be a valid address, even if it is valid, it is not what we want. If we define p as the pointer type in file1.c and declare p as the array type in file2.c, what will happen?

The solution to the above problems is to maintain consistency between definitions and declarations at any time.

Test procedure:

File2.c

# Include <stdio. h> extern char p []; extern void print (); int main (void) {printf ("% x \ n", p [0]); printf ("% x \ n", p [1]); printf ("% 08x \ n", p ); // note that the p value is the first address of the memory unit storing the original pointer p (p in file2.c) print (); return 0 ;}

File1.c

#include<stdio.h>char *p="abcdef";void print(){    printf("%08x\n",p);    printf("%08x\n",&p);}

3. Notes

1. What is the difference between sizeof computing space.

For an array, sizeof calculates the space occupied by the entire array. In a 32-bit system, the value of the sizeof pointer is always 4.

2. When the array name is used as the left value, it cannot be modified, and when the pointer is used as the left value, it can be assigned a value.

3. the pointer can perform auto-increment (auto-subtraction) Operations (except for the void pointer, because the void pointer cannot know the step size), but the array cannot perform auto-increment or auto-increment operations.
4. Understand the differences between char * p = "abcde" and char str [] = "abcde.

 

Author: Haizi

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.