pointers

Discussion in 'Programmer's Corner' started by semiconductor, Nov 29, 2003.

  1. semiconductor

    Thread Starter New Member

    Oct 16, 2003
    8
    0
    :D hi there i just want to ask you if you can help me understaing the term of pointers in C++ programing and how to apply it in my programing code. thank you loooking forward to hearing from you

    yours semiconductor :unsure:
     
  2. Graham

    Member

    Sep 10, 2003
    22
    0
    When you declare a variable in a C program, the compiler sets aside a memory location with an address to store the variable. The compiler will associate the address with the variables name. A pointer is a variable that contains the address of another variable.

    A pointer is like all other variables and must be declared before it can be used, as well as follow all the same rules as other variables.

    A pointer declaration takes the following form:

    typename *ptrname;

    where typename is any variable type and indicates the type of the variable that the pointer points to. The * is the indirection operator, and it indicates that ptrname is a pointer to the type typename and not a variable of type typename.

    Like regular variables, uninitialized pointers can be used but it is not recommended as results can be unpredictable. The address gets stored in the pointer by using the address-of operator, the &. When the & is placed before the name of the variable the address of the variable is returned. You initialize a pointer with a statement of the form:

    pointer = &variable;

    The statement assigns the address of variable to pointer.

    Accessing the contents of a variable by using the variable name is called direct access. Accessing the contents of a variable by using a pointer to the variable is called indirect access or indirection. When the * operator precedes the name of a pointer, it refers to the variable pointer to. Consider the following statement:

    variable = *pointer;

    This statement assigns variable with the value of whatever variable pointer points to.
     
  3. t bone

    New Member

    Jan 5, 2004
    1
    0
    Functions will make your program run much more effecient. However, the problem soon arises on how to make one function use what another function does. The easiest way is to store it in a variable...

    void function1()
    {
    int I ;
    I = function2() ;
    }

    int function2()
    {
    J = 5 + 5 ;
    return J ; //The value of I in function1() is now 10
    }

    However, you may realize that you can only store one variable this way. The next solution are pointers. Put simply, pointers will let you change the value of a variable in a function from another function.

    Here is an example of a program using pointers in C...

    #include <stdio.h>

    int main(void)
    {
    int I = 5 ; //sets the integer I to 5
    hello(&I) ; //calls the function 'hello'
    return 0 ;
    }

    void hello(int *J)
    {
    *J = *J + 5 ;
    /*J is a pointer to I, the value of I is now 10 */
    }
     
  4. LDC3

    Active Member

    Apr 27, 2013
    920
    160
  5. amilton542

    Active Member

    Nov 13, 2010
    494
    64
    Work with something like a char array of pointers then all will become obvious.
     
Loading...