pointer help

This is a discussion on pointer help within the C Programming forums, part of the General Programming Boards category; i need help with pointer iam just starting out can someone hep me. my program doesnt run [ Code: #include<stdio.h> ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    pointer help

    i need help with pointer iam just starting out can someone hep me. my program doesnt run

    [
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int f(int a, int b);
    main()
    {
          int a=2,b=4;
          int t;      
          
       t=  f(&a,&b);
         
         printf("%i %i",a,b);
         getchar(); 
    
    }
    int f(int *a, int *b)
    {
        int t;
       a=5;
       b=10;
       t=a+b;
       return t;
         
    }

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    EDIT:

    Arghhh! I screwed this up again! See cph's answer, it's better than mine.
    At least I got the de-reference part right.
    Last edited by Char*Pntr; 09-19-2010 at 11:06 AM.

  3. #3
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    Code:
    #include <stdio.h>
    
    void func (int *pa, int *pb)
    {
        int tmp = *pa;
    
        *pa = *pb;
        *pb = tmp;
    }
    
    int main (void)
    {
        int a = 2, b = 4;
    
        printf("%i %i\n", a, b);
        func(&a, &b);
        printf("%i %i\n", a, b);
    
        return(0);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Quote Originally Posted by mouse666666
    i need help with pointer iam just starting out can someone hep me. my program doesnt run
    What is your program supposed to do in the first place?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Apparently swap two variables.

  6. #6
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by mouse666666 View Post
    i need help with pointer iam just starting out can someone hep me. my program doesnt run

    [
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int f(int a, int b);
    main()
    {
          int a=2,b=4;
          int t;      
          
       t=  f(&a,&b);
         
         printf("%i %i",a,b);
         getchar(); 
    
    }
    int f(int *a, int *b)
    {
        int t;
       a=5;
       b=10;
       t=a+b;
       return t;
         
    }
    I take back what I said in my edit. The solution provided may work, but it also completely
    rewrote his function.

    Re-writing his code will not help him to learn, especially in this case when there
    was a very simple solution.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Quote Originally Posted by Syscal
    Apparently swap two variables.
    I suspect that "assign 5 and 10 to a and b in main from f and then print a and b in main" is more likely to be the answer. The fix I would then suggest is to dereference a and b in f. Still, we should wait and see what mouse666666 really had in mind.

    Incidentally, mouse666666: you should explicitly declare the main function as returning an int, and then return 0 at its end (optional in C99). It looks like you do not need the non-standard <conio.h> at all. Your indentation could be improved; look at cph's example in post #3 for inspiration.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Woops, I was reading cph's code, not the OP's. To fix his code:


    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int f(int *a, int *b);
    int main(){
    
        int a=2,b=4, t;
    
        t = f(&a,&b);
    
        printf("%i %i",a,b);
        getchar();
    
        return 0;
    
    }
    int f(int *a, int *b){
      
       int t;
    
       *a=5;
       *b=10;
       t = *a + *b;
    
       return t;
    }
    OP: Your prototype wasn't the same as the formal parameters at the bottom. Also, you weren't dereferencing the pointers in your function.
    Last edited by Syscal; 09-19-2010 at 11:29 AM.

  9. #9
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    Quote Originally Posted by Char*Pntr View Post
    I take back what I said in my edit. The solution provided may work, but it also completely
    rewrote his function.

    Re-writing his code will not help him to learn, especially in this case when there
    was a very simple solution.
    I'm sure I was only providing the hint to mouse666666 (I hope I didn't overdo the hint )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  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

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