Thread: Newbie Qn

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    Newbie Qn

    Code:
    #include<stdio.h>
    int x=21;
    int y=25;
    main()	{
    	clrscr();
    	funct(&x);
    		}
    		funct(int *py)
    		{
    		++*py++;
    		printf("&#37;x %d\n",py,*py);
    		getchar();
    		}
    
    
    The output is 196 and 25.
    My qn is for :
    ++*py++; /*This statement means the add py is pointing to increments. but does the value of what py is pointing to get increment too? I was thinking why output isnt 196 and 26 instead.
    The output shows that it isnt. please correct me if im wrong about my statement*/

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by robertlee View Post
    Code:
    #include<stdio.h>
    /* Don't use global variables */
    int x=21;
    int y=25;
    int main() /* Main should return int, see FAQ */
    { 
    	clrscr();
    	funct(&x);
    	return 0; /* Main should return 0 unless specified otherwise */
    }
    
    void funct(int *py) /* All functions must have a return type. If none is required, use void */
    {
    	/*++*py++;*/ /* Bad! */
    	++*py /* Is the same as ++(*py), increments *py by 1 */
    	*py++ /* Extremely bad! First dereferences py, then increments the pointer by sizeof(*py) */
    	printf("&#37;x %d\n",py,*py);
    	getchar();
    }
    Use proper indentation! This is almost the worst indentation I've seen! I fixed it, so it looks decently.
    Stay away from tricky things such as ++*py++. Very bad use. See comments.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    ++*py++;
    Is it me or does this appear to have wings on it?

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    2
    Thanks for your alteration!
    actually its not me who wants to use ++*p++;
    the source code is from a tutorial from sch actually. and i'm scratching my head on how come *py didnt get incremented.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can't say, but it's really bad form and should be split up, if possible.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When was this tutorial written, 1980?

    You should definitely specify return values for your function. You have no prototype for funct, so the compiler has to "guess" what you are up to there. clrscr() is not a portable function [meaning it only works on some systems].

    *py is incremented. Before py is incremented. So the value you see when you print *py is the value of "y", whilst the value of x is the one that gets incremented. If you do this:

    Code:
    #include <stdio.h>
    
    void funct(int *py);
    
    int x=21;
    int y=25;
    int main()	
    {
      funct(&x);
      printf("x = %d y = %d\n", x, y);
      return 0;
      getchar();
    }
    
    void funct(int *py)
    {
      ++*py++;
      printf("%p %d\n",py,*py);
    }
    You will see that "x" is 22 instead of 21. I've also changed the %x for printing the pointer to a %x - as modern compilers don't like pointers being printed with %x without warning about converting pointers to integers.

    --
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    ++*py++ is undefined behaviour, meaning it could do anything (set *py to zero, throw an exception, launch notepad, erase your hard disk)

    You were also missing the function prototype, matsp added it:
    Code:
    void funct(int *py);
    It shouldn't compile without that.

    matsp, you accidentally put getchar after the return
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    ++*py++ is undefined behaviour, meaning it could do anything (set *py to zero, throw an exception, launch notepad, erase your hard disk)

    You were also missing the function prototype, matsp added it:
    Code:
    void funct(int *py);
    It shouldn't compile without that.

    matsp, you accidentally put getchar after the return
    That's what you get when you edit the code after you compiled and tested it

    Actually, most compilers only give a warning for a missing prototype in C. C++ is slightly different, as it will probably make assumptions with regards to the function signature, and then get a linker error if/when the actual function has a different. It is of course wise to heed the warning in this case, as you can get very "funny" results from mismatched prototype/definitions [or the compilers guess of what the prototype is].

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  3. Have no idea how to compile c++, please help (newbie qn)
    By rholloway in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2008, 08:31 AM
  4. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM