Thread: ! C question: simple integer function passing

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    ! C question: simple integer function passing

    Could someone clarify this for me?

    I dont understand why case 1 works and why case 2 doesn't.
    the compiler gives a warning, complaining about code has no effect for case 2

    case1:

    ------------------------------------
    void func1( void )
    {

    int s_count;

    func2( &s_count );

    }

    void func2( int *count )
    {
    count++;
    }
    ------------------------------------

    case 2:
    ------------------------------------
    void func1( void )
    {

    int s_count;

    func2( &s_count );

    }

    void func2( int *count )
    {
    *count++;
    }
    ------------------------------------
    Last edited by aaronc; 04-28-2004 at 08:34 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> the compiler gives a warning, complaining about code has no effect for case 2
    Run it and see what happens...

    gg

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I dont understand why case 1 works and why case 2 doesn't.
    Case 1 works? Are you sure? I don't think either of them work.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    thank you for your reply

    i ran and output the integer data on the lcd

    it was still zero
    even though i incremented it in func2

    HOWEVER i think i may have found something,

    the compiler complains when i use

    count++;

    but it is happy when i use

    count += 1;

    BAH! STUPID COMPILER :P

    give me a few seconds i think it may be solved, just a fussy compiler.
    Last edited by aaronc; 04-28-2004 at 09:18 PM.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    yes it works now, weird.

    it is happy with += but not with the ++ when i call in a int pointer.

    damn compiler

    instead of doing *count++;

    i had to do *count += 1;

    for it to work.

    i'm working on an embedded env.

    case 2 works, this is what i have:
    ------------------------------------------------------------------------------------
    void func1( void )
    {
    int s_count;

    func2( &s_count );

    puts_lcd( s_count ); // <-- puts_lcd() does the output to lcd
    }

    void func2( int *count )
    {
    *count += 1;
    }
    ------------------------------------------------------------------------------------
    Last edited by aaronc; 04-28-2004 at 09:18 PM.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I don't think either of them work.
    Neither do I.....even with "+="

    You are incrementing the pointer in both cases, not what the pointer points to.

    gg

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by aaronc
    yes it works now, weird.
    No it's not. Reread all of your posts. Some times you put:
    Code:
    count++;
    Then you put:
    Code:
    *count++;
    As stated by Codeplug, they are definately not the same thing. In your first code example, you don't dereference. Thus, you don't change the value of what's being pointed at, you change what you're pointing at. (Which has no effect, because that goes out of scope at the end of the function call.)

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    sorry my wording was incorrect in the first post.

    what i meant was "works" as in "compiles with no warnings".

    not "works" as in "compiles with warnings".

    my apologies,

    this particular compiler complains about using post increment (*count++) when passing an int as a pointer. So i had to use *count += 1; instead
    Last edited by aaronc; 04-29-2004 at 12:11 AM.

  9. #9
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    You should use the post increment this way.

    Code:
    (*count)++;

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    thanks dante

    now why didn't i think of that earlier... the dreaded brackets

    is there any advantage to using
    Code:
    (*count)++;
    in comparison to
    Code:
    *count += 1;
    , or vice versa?
    Last edited by aaronc; 04-29-2004 at 12:23 AM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    None that I imagine.
    *count += 1;
    has the desired effect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. C Beep Function Simple Question
    By Matt3000 in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 06:43 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM