Why use pointers?

I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages.

Basically I have three questions:

  • Why use pointers over normal variables?
  • When and where should I use pointers?
  • How do you use pointers with arrays?

  • Why use pointers over normal variables?
  • Short answer is: Don't. ;-) Pointers are to be used where you can't use anything else. It is either because the lack of appropriate functionality, missing data types or for pure perfomance. More below...

  • When and where should I use pointers?
  • Short answer here is: Where you cannot use anything else. In C you don't have any support for complex datatypes such as a string. There are also no way of passing a variable "by reference" to a function. That's where you have to use pointers. Also you can have them to point at virtually anything, linked lists, members of structs and so on. But let's not go into that here.

  • How do you use pointers with arrays?
  • With little effort and much confusion. ;-) If we talk about simple data types such as int and char there is little difference between an array and a pointer. These declarations are very similar (but not the same - eg, sizeof will return different values):

    char* a = "Hello";
    char a[] = "Hello";
    

    You can reach any element in the array like this

    printf("Second char is: %c", a[1]);
    

    Index 1 since the array starts with element 0. :-)

    Or you could equally do this

    printf("Second char is: %c", *(a+1));
    

    The pointer operator (the *) is needed since we are telling printf that we want to print a character. Without the *, the character representation of the memory address itself would be printed. Now we are using the character itself instead. If we had used %s instead of %c, we would have asked printf to print the content of the memory address pointed to by 'a' plus one (in this example above), and we wouldn't have had to put the * in front:

    printf("Second char is: %s", (a+1)); /* WRONG */
    

    But this would not have just printed the second character, but instead all characters in the next memory addresses, until a null character () were found. And this is where things start to get dangerous. What if you accidentally try and print a variable of the type integer instead of a char pointer with the %s formatter?

    char* a = "Hello";
    int b = 120;
    printf("Second char is: %s", b);
    

    This would print whatever is found on memory address 120 and go on printing until a null character was found. It is wrong and illegal to perform this printf statement, but it would probably work anyway, since a pointer actually is of the type int in many environments. Imagine the problems you might cause if you were to use sprintf() instead and assign this way too long "char array" to another variable, that only got a certain limited space allocated. You would most likely end up writing over something else in the memory and cause your program to crash (if you are lucky).

    Oh, and if you don't assign a string value to the char array / pointer when you declare it, you MUST allocate sufficient amount of memory to it before giving it a value. Using malloc, calloc or similar. This since you only declared one element in your array / one single memory address to point at. So here's a few examples:

    char* x;
    /* Allocate 6 bytes of memory for me and point x to the first of them. */
    x = (char*) malloc(6);
    x[0] = 'H';
    x[1] = 'e';
    x[2] = 'l';
    x[3] = 'l';
    x[4] = 'o';
    x[5] = '';
    printf("String "%s" at address: %dn", x, x);
    /* Delete the allocation (reservation) of the memory. */
    /* The char pointer x is still pointing to this address in memory though! */
    free(x);
    /* Same as malloc but here the allocated space is filled with null characters!*/
    x = (char *) calloc(6, sizeof(x));
    x[0] = 'H';
    x[1] = 'e';
    x[2] = 'l';
    x[3] = 'l';
    x[4] = 'o';
    x[5] = '';
    printf("String "%s" at address: %dn", x, x);
    /* And delete the allocation again... */
    free(x);
    /* We can set the size at declaration time as well */
    char xx[6];
    xx[0] = 'H';
    xx[1] = 'e';
    xx[2] = 'l';
    xx[3] = 'l';
    xx[4] = 'o';
    xx[5] = '';
    printf("String "%s" at address: %dn", xx, xx);
    

    Do note that you can still use the variable x after you have performed a free() of the allocated memory, but you do not know what is in there. Also do notice that the two printf() might give you different addresses, since there is no guarantee that the second allocation of memory is performed in the same space as the first one.


    One reason to use pointers is so that a variable or an object can be modified in a called function.

    In C++ it is a better practice to use references than pointers. Though references are essentially pointers, C++ to some extent hides the fact and makes it seem as if you are passing by value. This makes it easy to change the way the calling function receives the value without having to modify the semantics of passing it.

    Consider the following examples:

    Using references:

    public void doSomething()
    {
        int i = 10;
        doSomethingElse(i);  // passes i by references since doSomethingElse() receives it
                             // by reference, but the syntax makes it appear as if i is passed
                             // by value
    }
    
    public void doSomethingElse(int& i)  // receives i as a reference
    {
        cout << i << endl;
    }
    

    Using pointers:

    public void doSomething()
    {
        int i = 10;
        doSomethingElse(&i);
    }
    
    public void doSomethingElse(int* i)
    {
        cout << *i << endl;
    }
    

  • Pointers allow you to refer to the same space in memory from multiple locations. This means that you can update memory in one location and the change can be seen from another location in your program. You will also save space by being able to share components in your data structures.
  • You should use pointers any place where you need to obtain and pass around the address to a specific spot in memory. You can also use pointers to navigate arrays:
  • An array is a block of contiguous memory that has been allocated with a specific type. The name of the array contains the value of the starting spot of the array. When you add 1, that takes you to the second spot. This allows you to write loops that increment a pointer that slides down the array without having an explicit counter for use in accessing the array.
  • Here is an example in C:

    char hello[] = "hello";
    
    char *p = hello;
    
    while (*p)
    {
        *p += 1; // increase the character by one
    
        p += 1; // move to the next spot
    }
    
    printf(hello);
    

    prints

    ifmmp
    

    because it takes the value for each character and increments it by one.

    链接地址: http://www.djcxy.com/p/5412.html

    上一篇: C ++:对象的矢量与指向新对象的向量的矢量?

    下一篇: 为什么要使用指针?