pointer problem - please help

This is a discussion on pointer problem - please help within the C++ Programming forums, part of the General Programming Boards category; I'd like to make one variable always equal to 1/3rd of another variable. I've tried the following... Code: int x; ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    3

    pointer problem - please help

    I'd like to make one variable always equal to 1/3rd of another variable. I've tried the following...

    Code:
    int x;
    int *p;
    
    p = &x/3;
    But I get "error C2296: '/' : illegal, left operand has type 'int *'"
    How do I get this to work? Thanks in advance

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Thats not the way pointers work, they store memory addresses,
    not actually the data. The code you posted attempts to divide
    the physical memory address of the variable x by 3, and assign
    it to the pointer p. Obviously that won't do so here's what you
    want:

    Code:
    int x = 3;
    int *p;
    
    p = &x;
    
    cout <<"x/3 is equal to: "<<*p/3;
    if you dont understand that then suggest reading up a bit on
    pointers, i found them tricky until i actually took a minute to
    sit down with them and i found that they're not too bad at all.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    To see why your code doesn't work, try this:
    Code:
    int x = 20;
    cout<<&x<<endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21