Like Tree2Likes
  • 2 Post By Salem

Pointers: Please help to understand the code

This is a discussion on Pointers: Please help to understand the code within the C Programming forums, part of the General Programming Boards category; Can anyone please explain the code: Don't understand why the author declared "z", even though it seems not being used? ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    Pointers: Please help to understand the code

    Can anyone please explain the code: Don't understand why the author declared "z", even though it seems not being used?

    Code:
    #include <stdio.h>
    void func(float *px, float *py) 
    {   *px = 3 + 2 * *px;   *py = *py  - 21; }
    int main()
    {
     float x=0, y=4, z= 1; 
    int i;
     func(&x, &y); 
    func(&y, &z); 
    printf("%f\n",y);
     return 0; 
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    But it is used. It is passed in the second call to func().
    Code:
    #include <stdio.h>
    
    void func(float *px, float *py) 
    {   *px = 3 + 2 * *px;   *py = *py  - 21; }
    
    int main()
    {  
        float x=0, y=4, z= 1;
        int i;   
    
        func(&x, &y);   
        func(&y, &z);   
    
        printf("%f\n",y);
    
    return 0; 
    }

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Output I get is -31.000000. Do you know how the program gives it?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    5,826
    Yes, I do.

    And, no, I am not going to explain. Apart from the fact this looks like a homework exercise (and this site's homework policy amounts to "You should do your OWN homework, we won't"), you have demonstrated no effort to understand on your own - which says a lot about your laziness, as only a minute or two of your effort would be needed.

    Try working it out for yourself. People here can then check, and give constructive advice. If you get it right, you will learn in the doing. If you make mistakes, you will learn by having them corrected. Either way, you will learn more by TRYING.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    It's not a homework I'm trying to learn about the pointers which is complicated to me as Im a beginner to C. Just trying to understand the program. Value your advice.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Learn to add print statements and run the code.
    Code:
    #include <stdio.h>
    void func(float *px, float *py)
    {
      *px = 3 + 2 * *px;
      printf("New *px = %f\n", *px );
      *py = *py - 21;
      printf("New *py = %f\n", *py );
    }
    
    int main()
    {
      float x = 0, y = 4, z = 1;
      int i;
      printf("Old x=%f, y=%f\n", x, y );
      func(&x, &y);
      printf("New x=%f, y=%f\n", x, y );
      func(&y, &z);
      printf("%f\n", y);
      return 0;
    }
    Or learn to use a debugger and step the code.
    Code:
    $ gcc -g foo.c
    $ gdb ./a.out 
    (gdb) break main
    Breakpoint 1 at 0x400546: file foo.c, line 10.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    
    Breakpoint 1, main () at foo.c:10
    10	  float x = 0, y = 4, z = 1;
    (gdb) s
    12	  func(&x, &y);
    (gdb) 
    func (px=0x7fffffffe184, py=0x7fffffffe188) at foo.c:4
    4	  *px = 3 + 2 * *px;
    (gdb) 
    5	  *py = *py - 21;
    (gdb) 
    6	}
    (gdb) print *px
    $1 = 3
    (gdb) print *py
    $2 = -17
    (gdb) s
    main () at foo.c:13
    13	  func(&y, &z);
    (gdb) print y
    $3 = -17
    (gdb) print z
    $4 = 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    283
    +1 on learning to use gdb. It is actually really intuitive and easy to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understand pointers
    By Aphex in forum C Programming
    Replies: 3
    Last Post: 08-12-2010, 09:07 PM
  2. i cant understand something in pointers
    By nik2 in forum C Programming
    Replies: 2
    Last Post: 02-12-2010, 12:26 PM
  3. Trying To Understand Pointers!
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 05-08-2008, 01:28 AM
  4. Do I understand pointers correctly?
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 01-05-2006, 07:30 AM
  5. Something I still don't understand about pointers
    By Extol in forum C++ Programming
    Replies: 11
    Last Post: 03-01-2003, 05:20 AM

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