pointers

This is a discussion on pointers within the C Programming forums, part of the General Programming Boards category; ok so i dont think i know how to use pass by reference this is right? Code: int main(){ a ...

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    pointers

    ok so i dont think i know how to use pass by reference

    this is right?
    Code:
    int main(){
    a = 10;
    rval = func(&a);
    }
    
    int func(*a){}
    Then if i pass the variable in
    Code:
    int main(*a){
    a = 10;
    rval = func(&a);
    }
    
    int func(*a){}
    and if I wanted to void type cast it?
    Code:
    int main(){
    a = 10;
    rval = func((void)&a);
    }
    
    int func(*a){}
    Im sorry im really confuzed

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Im not quite sure what you mean

    Code:
    #include <stdio.h>
    
    int squareMe ( int* );
    
    int main()
    {
       int data = 5;
    
       squareMe ( &data ); /*pass by pointer reference*/
    
       printf("Value is: &#37;d\n", data);
    
       return 0;
    }
    
    int squareMe ( int *pX )
    {
       return *pX * *pX;
    }
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int main(*a){
    a = 10;
    rval = func(&a);
    }
    is incorrect, as a = 10 is probably not a good thing to do on a pointer, and &a will make one more indirection (it is now a pointer to pointer) [and the input to main() - aside from main having specific parameters - should have a type, e.g. int *a].

    Likewise, int func(*a) should have a type before the *.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    so would it look like
    Code:
    int main(int *a){
    
    rval = func(*a);
    }
    
    int func(int** a){
    .......... = .......... /5;
    }
    and what about void pointers
    would that look like
    Code:
    int main(int *a){
    
    rval = func((void *)a);
    }
    
    int func(int a){}

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Its best to not place arugments in the main function parathesis.

    Leave it as main() or main ( void )
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by swgh View Post
    Its best to not place arugments in the main function parathesis.

    Leave it as main() or main ( void )
    Unless you are using command line arguments obviously!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    but was that right? wiat soemthing crazy happened
    Code:
    int main(int *a){
       rval = func(*a);
    }
    
    int func(int** a){
       **a = **a /5;
    }
    PS if i type .......... i get a bunch of ......

    PPS damnit if i type a ** (with no space) i get a bunch of ......
    Last edited by chico1st; 05-21-2008 at 10:18 AM.

  8. #8
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    wait no it would have to be:
    Code:
    int main(int *a){
       rval = func(a);
    }
    
    int func(int** a){
       **a = **a /5;
    }
    Also lets pretend im sending values into main for a good reason

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Refer to swgh's example in post #2. Do you understand it?

    Also lets pretend im sending values into main for a good reason
    Instead of pretending, come up with test code that you can actually compile and run.
    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

  10. #10
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    yes but passing something that is allready a pointer confuzes me... and void typecasting confuse me

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    yes but passing something that is allready a pointer confuzes me
    Consider a simple example:
    Code:
    #include <stdio.h>
    
    void foo(int **p)
    {
        **p = 1;
    }
    
    int main(void)
    {
        int x = 0;
        int *p = &x;
        printf("&#37;d\n", x);
        foo(&p);
        printf("%d\n", x);
        return 0;
    }
    So yes, you are on the right track with your use of **a in func(), but in main() you should have passed the address of a, not a, since a is a pointer, but func() takes a pointer to a pointer.

    Of course, in the above example it is not useful to pass a pointer to a pointer since passing a pointer will do.
    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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    A breakdown of how pointer syntax works:
    int* p;
    This defines a variable that holds an address to a variable of type int. Remember that the variable holds an address, not the actual value. In order to make the program access the contents at the memory location specified by the address in the pointer, you dereference it via *.
    So,
    Code:
    int x = 0;
    int* p = &x; /* Put address of x in p */
    *p = 5; /* Dereference p with *, then assign 5 at that memory location */

    A pointer to pointer is a pointer to type*, or in other words, it holds the address of a type* variable. Since that type* is a pointer in turn, it can also be dereferenced to get the actual value pointed by that pointer. Consider:
    Code:
    int x = 10;
    int y = 20;
    int* p = &x; /* Store address of x */
    int** p2 = &p; /* Store address of p */
    *p2 = &y; /* Assign a new address to the variable p */
    **p2 = 30; /* Assign 30 to y */

    You can take the address of a variable with &. Understanding the last example will make you a master.
    The basics of how pointers work.
    Good luck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    Quote Originally Posted by laserlight View Post
    Consider a simple example:
    Code:
    #include <stdio.h>
    
    void foo(int **p)
    {
        **p = 1;
    }
    
    int main(void)
    {
        int x = 0;
        int *p = &x;
        printf("&#37;d\n", x);
        foo(&p);
        printf("%d\n", x);
        return 0;
    }
    So yes, you are on the right track with your use of **a in func(), but in main() you should have passed the address of a, not a, since a is a pointer, but func() takes a pointer to a pointer.

    Of course, in the above example it is not useful to pass a pointer to a pointer since passing a pointer will do.
    so could I just pass p to foo?
    making:
    Code:
    void foo(int *p)
    {
       *p = 1;
    }
    
    int main(void)
    {
        int x = 0;
        int *p = &x;
        printf("%d\n", x);
        foo(p);
        printf("%d\n", x);
        return 0;
    }
    And thank you very much elysia .. that was good //yours was good too laserlight but i just wanted to mention her
    Last edited by chico1st; 05-21-2008 at 12:58 PM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Yes, that would work since the types match.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    so could I just pass p to foo?
    Yes, that is correct.
    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

Page 1 of 2 12 LastLast
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, 06: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

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