How does it work?

This is a discussion on How does it work? within the C Programming forums, part of the General Programming Boards category; Got this code to analyze. Code: int i,m,*g_p; int g[10]={1,5,3,4,0,22,8,0,-53,7}; main() { m=0; g_p=g; for (i=0;i<10;g_p++,m++,i++) { printf ("first loop: ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    How does it work?

    Got this code to analyze.
    Code:
    int i,m,*g_p;
    int g[10]={1,5,3,4,0,22,8,0,-53,7};
    main()
    {
    	m=0;
    	g_p=g;
    	for (i=0;i<10;g_p++,m++,i++)
    	{
    			printf ("first loop: m=%d  *g_p=%d\n",m, *g_p);
    			printf ("\n");
    		for (;*g_p;g_p++,i++);
    			printf ("second loop: m=%d  *g_p=%d\n",m, *g_p);
    			printf ("\n");
    	}
    	printf ("end of program: m=%d  *g_p=%d\n",m, *g_p);
    }
    Figured out that it calculates the numbers of series, delimited by 0. I don’t understand:
    1. How does the pointer *g_p “jump” directly to 22 & then to -53.
    2. Why does it get the value of 0?
    3. What means the
    Code:
    *g_p
    in
    Code:
        for (;*g_p;g_p++,i++);
    output:
    first loop: m=0 *g_p=1

    second loop: m=0 *g_p=0

    first loop: m=1 *g_p=22

    second loop: m=1 *g_p=0

    first loop: m=2 *g_p=-53

    second loop: m=2 *g_p=0

    end of program: m=3 *g_p=0
    Last edited by ronenk; 08-16-2004 at 09:02 AM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The behaviour of the program is undefined.

    It might help you understand the code if you change:
    Code:
    		for (;*g_p;g_p++,i++);
    to:
    Code:
    		for (;*g_p;g_p++,i++) /* Do nothing */;
    and analyse what this loop is doing.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Did that with out further understanding.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Would it help you if I said:
    Code:
    for (;*g_p;g_p++,i++);
    is equivalent to:
    Code:
    while (*g_p != 0)
    {
        g_p++;
        i++;
    }
    Try running through the code step by step.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    the style of that code looks ancient.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    OK now all understood!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

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