Thread: pointer

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    6

    Red face pointer

    if I declare a int variable a, and a int *p, let p point to a, p = &a;
    can I say *p == a; at the moment?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    p = &a; //put the address of 'a' into 'p'

    *p = a; // assign the value of a to whatever p points at.

    Example:

    Code:
    #include <stdio.h>
    int main ( void )
    {
        int x, a, *p;
    
        a = 20;
        printf("A is %d.\n", a );
    
        p = &a;
        *p = 40;
        printf("A is now %d.\n", a );
    
        x = 10;
        *p = x;
        printf("A is now %d.\n", a );
    
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    6

    Wink another quesiton

    if I say int a, *p;
    a = 20; p = &a; and
    *p == a ?

  4. #4
    Unregistered
    Guest
    I think that would work, have you tried it?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well the double equal sign is for conditions such as:

    if(*p == 10)
    //then execute this...


    Also, if you have an array of ints, you can use pointer arithmatic after assigning a pointer to it:

    int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int *p;

    p = array;

    while(*p) // loops till it reaches end of "array"!
    {
    if(*p==5)
    printf("Equals five\n");
    else if(*p < 5)
    printf("Less than five\n");
    else
    printf("Greater than five\n");

    p++; //increments through "array"
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: another quesiton

    Originally posted by pnxi
    if I say int a, *p;
    a = 20; p = &a; and
    *p == a ?
    Yes. You are dereferencing p (which points to a) and comparing it with a.......therefore you are comparing a with itself.....

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Hi!

    p points to a
    then p stands for the address the pointer is pointing to, itīs the address of a and *p stands for the value in that address, itīs the value of a.

    klausi
    When I close my eyes nobody can see me...

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Bingo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM