Results 1 to 5 of 5

Thread: Pointers in C and C++

  1. #1
    Join Date
    May 2008
    Posts
    188

    Pointers in C and C++

    Pointers in c and c++
    Most programming like pointer in C and C++, because it gives the address of the memory which is being allocated. That is, it is the the value of the address. Through pointer you can access the memory directly which is being allocated. But if you do not use pointers properly there is a possibility that your program might crash. Therefore, the Java programming language do not use the pointers. That is, there is no way in java to access the memory allocation directly. This tutorial will show you how the pointers work internally. So, lets start

    Introduction
    Most new programmer are afraid to use the pointer in C and C++, because they are not sure if they miss something in their code and the pointer would effect the program. It aims to help beginners in C / C + + pointers to address with the least possible apprehension. (It is considered that you know the basics of the C and C++ programming language such as the variables, because to know the pointers internal working you have to know how the variable behaves in your code.

    Pointer
    A pointer is a variable, it is intended to contain a memory address, i.e a value identifying a location in memory. To distinguish a pointer to an ordinary variable, we precede the name of the sign '*' in his statement. Just have a look at the following example
    Code:
    int * px; 
    px = &x;
    The first line reserve a location for storing a memory address. The second line writes the address of x in this spot. Ok, We will try to understand this in more details.

    When it is declare as int *px;, it means that


    Here it reserves a storage place for the pointer px (box number 96 in the above diagram). At this stage there is no difference with this and an ordinary variable.

    When it is assigned as px = &x;, it means that


    It writes the address of x in the space reserved for the pointer px. Remember one pointer is used to store a memory address.
    In the space reserved for the pointer px, we now have the address of x. This pointer has value as the address, so we can use this pointer to fetch (read / write) the value of x. For that we precede the name of the pointer with the de-reference operator '*'
    So the following means,
    Code:
    printf ( "% d", * px);
    Displays the value of x by pointer de-reference (10 in the case of the diagram).


    We can similarly change the value of x
    Code:
    * px = 20;
    Now, value of x is 20

    Pointers must be initialized with an address valid, i.e that has been reserved in memory (allocated) by the program to be used. Imagine the previous statement, if we had not initiated the pointer with the address of x, the writing would be in an undisclosed location memory.
    In the example above you have noticed that we gave a type the pointer (integer i.e int *), even if in a given system a pointer always has the same size (4 bytes for a system with 32-bit addressing). If you are unaware of the type of data the pointer will point to then you can give type void to pointer. It is customary to prefix the name of the pointer variable type the letter "p" this for better readability.

  2. #2
    Join Date
    May 2008
    Posts
    188

    Pointers and arrays

    Pointers and arrays
    In C / C + +, there are cases where we can not do without the use of array. The name of a table without decoration returns the address of the first array element. Consider the following table test
    Code:
     int tab [10] = (4,2,4,5,9,7,5,2,3,4);
    The following statement displays the property value of the first array element by pointer dereferenced.
    Code:
    printf ( "% d", *test);
    This shows us that the name of a table may very, but used as a pointer to its first element. We can then fully declare a pointer and initialize it with the table name. Provided of course that is similar, pointer to integers in our case.
    Code:
          int * ptest;
          ptest = test;
    We can use this pointer as if it were a table and operators brackets [] to access its elements:
    Code:
          printf ( "% d", ptest [0]) ;
    But this is not another array is the same as test, it references the same memory location:
    Code:
          ptest [0] +;
          printf ( "% d", test [0])
    When incrementing the first element of the array using pointer this is the first element of the original array is incremented.
    What happens if we write:
    Code:
          printf ( "% d", test [10]);
    Its display a value not part of my table. This is because it reads a value outside the bounds of the array without the system reports an error.
    Code:
          printf ( "% d", test [10]);
    This is same as
    Code:
          printf ( "% d", * (test 10));
    A pointer and then a table can easily access to anywhere in memory without any warning, until the application is crashed.

  3. #3
    Join Date
    May 2008
    Posts
    188

    Pointer arithmetic

    Pointer arithmetic
    Let consider the case of our table test. As ptest, initializes with test, is a pointer to its first element. If the pointer increments ptest it does not contain the address of the first element Table 1+, but the address of the next element. The value of the address contained will be incremented by the size of type it references. This is one reason which should give a pointer type.
    So, if we write
    Code:
           printf ( "% d", * (test 1))
    Now, the second element of the array.
    Code:
    ptest + +;
           printf ( "% d \ n", ptest [0]);
    It also displays the second element of the original array, since the pointer has been incremented by one, it now contains the address of the second array element.
    We can similarly decrement a pointer, to add or remove a full numerical value (not to leave memory allocation areas).
    So, it we
    Code:
    ptest test = 4;
           printf ( "% d \ n", ptest [0])
    It display the 5th element of the array as ptest is now a pointer on the 5th element.

  4. #4
    Join Date
    May 2008
    Posts
    188

    Pointers and dynamic allocation of memory

    Pointers and dynamic allocation of memory
    A very common use of pointers is the dynamic allocation memory. When you make a dynamic allocation of memory you get in return a pointer to the allocated memory area. Here is an C sample code showing this
    Code:
    int * x = malloc (10 * sizeof (int));
            if (x)
              (
                x [0] = 5;
                printf ( "% d", x [0]);
                free (x);
              )
    In the above example, the direction malloc returns a pointer to a memory area size of 10 integers (in C + +, the malloc returns a pointer type void (void *), but we must cast for using with integers). We can now use this pointer as if it were an array of 10 integers (malloc returns NULL if the allocation to be failed). The locations dynamically allocated memory must be freed explicitly. Using the free instruction in the case of allocation by malloc.
    Here is an C++ example performing the same task
    Code:
    int * x = new int [10];
            x [0] = 5;
            std:: court <<x [0];
            delete [] x;
    Note however that it does not change the pointer because it is necessary to free memory. You can declare this case as constant pointer to avoid the the following line
    Code:
          const int * x = new int [10];

  5. #5
    Join Date
    May 2008
    Posts
    188

    The function of pointers

    The function of pointers
    The value returned by name (only), a function is the address of its code. We can assign this value to a pointer. In the example below, we create a function pointer.
    Code:
     int (* pmax) (int *, int);
         pmax = max;
    
         printf ( "% d", pmax (test 10));
    A pointer must be declared with the signature of the function, i.e in this case, the pointer pmax is a pointer to function receiving as parameter a pointer to an integer then an integer and returning a whole. In our case we assign the pointer to address a function. The pointer is used when the same syntax as function.

    Conclusion:
    This tutorial is just intended to address pointers. Most of the topic is of course and use them only when necessary. An additional knowledge, the Windows API uses plenty of pointers as parameters features. The function pointers are used to fetch functions in the dynamic library, that is the DLL's. Fee free to reply.

Similar Threads

  1. What are the advantages of pointers?
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 12:39 PM
  2. Pointers VS Arrays in C++
    By Rum in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 02:06 PM
  3. Pointer to an array of pointers
    By pushpendra in forum Software Development
    Replies: 3
    Last Post: 29-12-2009, 12:45 PM
  4. Are pointers and arrays equivalent
    By KALINDA in forum Software Development
    Replies: 3
    Last Post: 16-10-2009, 06:41 PM
  5. Arrays and Pointers in C
    By Ivy00 in forum Software Development
    Replies: 1
    Last Post: 10-11-2008, 05:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,570,278.82333 seconds with 17 queries