What's it that I am doing wrong?

This is a discussion on What's it that I am doing wrong? within the C++ Programming forums, part of the General Programming Boards category; I am trying to change value of a variable by sending pointe to that variable as argument but I still ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    What's it that I am doing wrong?

    I am trying to change value of a variable by sending pointe to that variable as argument but I still get the same value in main(). What is it that I am doing wrong? Thanks a lot

    #include <iostream.h>
    #include <conio.h>
    #include <string.h>
    #include <stdio.h>


    void modify(int *);

    void main()
    {
    clrscr();
    int num = 0;
    int *aptr = &num;
    modify(aptr);
    cout << "Value of num is: " << num;

    getch();

    }

    void modify(int *Num)
    {
    *Num = 10;


    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use code tags please

    And read about void main

    However, your pointer usage looks OK. The program prints "Value of num is: 10" as you'd expect.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since you are using C++, you may as well use a reference:
    Code:
    #include <iostream.h>
    
    void foo( signed short int &bar )
    {
        bar = 10;
        cout << "foo:bar is " << bar << endl;
    }
    
    int main( void )
    {
        signed short int bar(0);
    
        foo( bar );
    
        cout << "main:bar is" << bar << endl;
    
        return 0;
    }
    For basic stuff like this, pointers are pretty much useless in C++, with the addition of references.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    15
    I am sorry, actually I made a mistake in the post. Function modify() looks like this:

    void modify(int *Num)
    {

    *Num++;

    }

    Now, shouldn't this print 1 instead of the original value, which was zero?
    arj

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>*Num++;
    This increments the pointer, not what it's pointing to. Use this instead:
    >>(*Num)++;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

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