Thread: Pointers

  1. #1
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24

    Pointers

    how do i get a pointer to "point" to a variable in another function?

    this is what i have so far.


    Code:
    # include <conio.h>
    # include <stdio.h>
    
    void calculations(int,int);
    
    
    void main (int sum,int diff)
    {
       int num1,num2;
       int * pointer1;
       int * pointer2;
    
    
       printf ("Enter num1: ");
       scanf("%d",&num1);
    
       printf ("Enter num2: ");
       scanf("%d",&num2);
    
       calculations(num1,num2);
       pointer1 =&sum;
       pointer2 =&diff;
    
    
       printf ("The sum is : %d\n",pointer1);
       printf ("The sum is : %d\n",pointer2);
    
    
       getch();
    }
    
    void calculations (int num1,int num2)
    {
        int sum,diff;
    
       sum = num1  + num2;
       diff = num1 - num2;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't. The variables sum and diff cease to exist when the function ends, and therefore you can't have pointers to them later (since there aren't any variables for them to point to). You can pass in pointers to a function, to have the function change the values those pointers point to; or you can have a function return a pointer (but not a pointer to a local variable!).

  3. #3
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    The parameter passing you have use doesn't make sense. e.g. main is always the first function executed, where do u suppose it would get sum & diff from? These variables would likely contain completely different values to what u expect (even if they were the right type).

    Try this for calculations; void calculations (int *num1, int *num2, int *sum, int *diff). Note that you would have to declare sum & dif as int inside main, and pass the address of each variable to the function. Note that num1 & num2 are inputs and sum & diff are outputs.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  4. #4
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    so instead i shud just make the variables pointers and i'll be able to use em and get the correct value anywhere?

  5. #5
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    ok, say i have a function that does the basic arithmetic + - / *.


    how do i get to use the results in that function without using global variables?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Doc.
    how do i get to use the results in that function without using global variables?
    You can have the function return a value, or you can pass a pointer to the function and store the result in the object that is pointed to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    Quote Originally Posted by laserlight View Post
    You can have the function return a value, or you can pass a pointer to the function and store the result in the object that is pointed to.


    i know how to return a value but could you show me how to "you can pass a pointer to the function and store the result in the object that is pointed to."?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Doc.
    i know how to return a value but could you show me how to "you can pass a pointer to the function and store the result in the object that is pointed to."?
    An example:
    Code:
    #include <stdio.h>
    
    void multiply(int* result, int x, int y);
    
    int main()
    {
        int n;
        multiply(&n, 4, 5);
        printf("%d\n", n);
        return 0;
    }
    
    void multiply(int* result, int x, int y)
    {
        *result = x * y;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Or you can return a value like
    Code:
    # include <conio.h>
    # include <stdio.h>
    
    calculations (int num1,int num2)
    {
        int sum ,diff;
    	char result;
    	fputs("For sum press s and for diff press anything else: ",stdout);
    	fflush(stdin);
    	scanf("%c",&result);
    	if(result=='s')
    	return sum = num1  + num2;
    else
    return diff = num1 - num2;
    }
    int main (void)
    {
       int num1,num2;
       int lol;
       fputs ("Enter num1: ",stdout);
       scanf("%d",&num1);
       printf ("Enter num2: ");
       scanf("%d",&num2);
       lol=calculations(num1,num2);
       printf ("The diff is : %d",lol);
    getchar();
    return 0;
    }

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    @lolguy, Don't fflush(stdin) -- See the FAQ.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lolguy
    Or you can return a value like
    If you actually read the thread, in particular post #7, you would know that your example is redundant. Besides what zacs7 mentioned, there are other areas in which you could improve:
    • <conio.h> should not be included both because it is non-standard and because you use nothing from it.
    • The calculations function should be declared with a return type of int instead of relying on the return type to default to int.
    • The sum and diff local variables are redundant.
    • The indentation is inconsistent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    Quote Originally Posted by zacs7 View Post
    @lolguy, Don't fflush(stdin) -- See the FAQ.

    sometimes when i want a user to enter a string with " " (spaces) in it and i use gets, and sometimes it skips that input unless if i put a fflush infront of the gets.

    why is that?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Doc. View Post
    sometimes when i want a user to enter a string with " " (spaces) in it and i use gets, and sometimes it skips that input unless if i put a fflush infront of the gets.

    why is that?
    If you had previously used scanf to read in input, then there is still an enter-key in the input stream, which gets will read. The same FAQ has several methods for dealing.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Doc.
    sometimes when i want a user to enter a string with " " (spaces) in it and i use gets, and sometimes it skips that input unless if i put a fflush infront of the gets.

    why is that?
    Since fflush(stdin) results in undefined behaviour, it is possible that an implementation may actually cause it to "flush" the input buffer.

    Oh, and don't use gets().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Yes, that's why I try to just stick to a couple ways of reading input, if in C, fscanf and if in C++ cin or getline.

    Also, if you need extra help in understand pointers, try this C / C++ pointer tutorial it may help you understand them and remember them better.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM