Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Intro to pointers in C/C++ tut

  1. #11
    Senior Member
    Join Date
    May 2002
    Posts
    344
    some of you might have already realized it, but http://www.cprogramming.com/advtutorial.html has some good tutorials about C++ programming. Also, nebulus, i dont quite understand what you are saying. I am pretty sure that if i create a code in C++ it will not compile:

    #include <iostream>
    using namespace std;

    int function()
    {
    int j,k,l;
    int *ptr_pointer;

    j=1;
    k=2;
    l=0;

    ptr_pointer = j + k;
    return ptr_pointer;
    }

    int main ()
    {
    int x=0;
    x=function();
    cout << x;
    }

    again i didnt compile this and i didnt think at all when i wrote this in here, so there might be errors. Anyways, the value of x will not equal 3...right?
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  2. #12
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    It wouldn't work because you are trying to assign integers to pointers.

    #include <iostream>

    int *function(int i)
    {
    int j = 1;
    int k = 2;
    int l = 0;

    l = i + j + k;
    return &l;
    }

    int main()
    {
    int *x;
    x = funtion(2);
    cout << x;
    }

    Should print out 5.

    It might require monkeying around, but you have to careful that you are returning things that make sense...you were trying to return a pointer and then assign to an int, god only knows what you would get. And by the way, this application is pretty pointless. It is more useful once you start using struct's or C++ objects.

    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  3. #13
    Senior Member
    Join Date
    May 2002
    Posts
    344
    now i understand what you are saying...ok, but still lol sorry this is taking forever, the return type cannot be a pointer... or can it because right now your are just returning the address of a variable ( return &l; ) Thanks for the explination
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •