Thread: Help with pointers!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    Help with pointers!

    Hi,
    I'm having a bit of trouble with pointers:

    Code:
    void My_func(float A)
    {
    float D = 2;
    float TEMP;
    
    TEMP = D + A;
    A = TEMP;
    }
    
    main()
    {
    float originalA = 5;
    My_func(originalA);
    printf("%f", originalA);
    }
    Obviously, the code above does not include any pointers. But it shows the basic form of what I'd like to achieve. After being passed to My_func, the value of originalA should become 7. How do I put pointers into this?
    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What do you know of pointers? Do you know how to declare or define a pointer? Do you know how pointers work? Do you know how to pass an address to a function so it can take a pointer?

    Also, main returns int.
    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.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    You're passing the originalA by value so the function cannot change the value of A within My_func. You can change it via, void My_func( float *A ). Then *A = TEMP;. And then My_func( &originalA );. Just some syntax you need to get used to.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, not just syntax. The fundamentals. Understanding what you are doing.
    Does the OP know this? If not, the OP must know before learning the syntax.
    And does the OP know the syntax? Throwing a solution at them will not help.
    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.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Wah wah. Semantics.

    All the OP needs to know is the syntax. If they don't know the syntax they can't understand the fundamentals.

    int *pNum = #

    There is no intuitive way someone could magic this knowledge. They're told the syntax. Then shown how to use it. Then they learn through experience (of theirs and others). It's applied to the function when it's called. Through knowing the syntax comes understanding.

    Chicken or egg? For some it's the chicken, for others it's the egg. I don't think it matters much which teachingmethod one uses at a high level, but might do at an individual level.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    I do understand the fundamentals of passing by ref and was only looking for the syntax. The reason I didn't include some indication of pointers in my original post was because I thought if I wrote them wrong it would only be more confusing. Though I appreciate your concern and agree that there is not much point in seeing the answer without understanding the idea. Thanks

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Declare a pointer: put the * after the type you want it to point to: T* pointer (pointer to T). T** pointer (pointer to T*).
    Dereference a pointer: put the * before the pointer: *pointer. Of course, if the pointer points to another pointer, you can dereference the pointer it points to: **pointer.
    To create a pointer, it must point to something. Taking the address of something is done by prefixing & before the variable to take the address of: T variable. T* pointer = &variable;

    And I completely disagree that taught the syntax and learning for themselves is the wrong approach. They should learn the basics first, then told the syntax and how it connects to the basics, so they can get a deep understanding and become smart programmers. It's important in this branch.
    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.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> And I completely disagree that taught the syntax and learning for themselves is the wrong approach.
    Couldn't agree more, but I'm not their teacher. I'm not in charge with the progression of their learning. I made an assumption that they were familiar with using pointers within main or a function and extended that to work for function arguments.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Myself, I wanted to be sure before handing out any information.
    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.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    a pointer is an address to another address to a memory(some variable type)

    how you want to use this property in your program?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A pointer is a variable that contains a memory address, pure an simple.
    You may store the address of another variable in a pointer.
    You may also store the address of a pointer in a pointer (pointer to pointer).
    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.

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