Thread: problem with pointer

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    18

    problem with pointer

    Hi all,

    I have short program with pointer
    Code:
    #include <stdio.h>
    
    main(void)
    {
    	int b[] = {2,2,2,2,2,2};
    	int i;
    	int sumints(int *p);
    	
    	int c = sumints(b);
    	printf("%d\n",c);
    }
    
    int sumints(int *p)
    {
    	int sum = 0;
    	while(*p != 0)
    	{
    		sum += *p;
    		*p++;
    	}
    	return sum;
    }
    the program's result is out of my expectation. the return sum is 1628704768

    I have try to trace in while statement, there are 7 loops instead of 6 loop

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You probably want to increment p, not *p. (Right now, you're just incrementing b[0] over and over again until overflow happens.)

    And then, there's no reason that your pointer will stop at the end of your array. In fact, your pointer is not guaranteed to stop ever.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by tabstop View Post
    You probably want to increment p, not *p. (Right now, you're just incrementing b[0] over and over again until overflow happens.)

    And then, there's no reason that your pointer will stop at the end of your array. In fact, your pointer is not guaranteed to stop ever.
    No i want to the *p point to b[0] and then b[1] continue to b[5], i add some printf to show the exactly what the problem
    Code:
    #include <stdio.h>
    
    main(void)
    {
    	int b[] = {2,3,4,2,3,6};
    	int i;
    	int sumints(int *p);
    	
    	int c = sumints(b);
    	printf("%d\n",c);
    }
    
    int sumints(int *p)
    {
    	int sum = 0;
    	while(*p != 0)
    	{
    		printf("%d\n",*p);
    		sum += *p;
    		*p++;
    	}
    	return sum;
    }
    and the result is
    Code:
    $ ./a.exe
    
    2
    3
    4
    2
    3
    6
    1628704768
    1628704788
    i believe because this is because the loop run 7 times instead of 6 times

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay, so apparently I have order of operations backwards and *p++ does increment p. So that seems to work.

    The other bit:
    And then, there's no reason that your pointer will stop at the end of your array. In fact, your pointer is not guaranteed to stop ever.
    is still in full force. I'm surprised it only goes one extra step; I would expect it to go, on average, 2147483648 extra steps. The last number in your array isn't 0, the number directly after your array (which you don't even know if there is one, necessarily) doesn't have to be 0, so why would your loop stop?

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by tabstop View Post
    Okay, so apparently I have order of operations backwards and *p++ does increment p. So that seems to work.

    The other bit:

    is still in full force. I'm surprised it only goes one extra step; I would expect it to go, on average, 2147483648 extra steps. The last number in your array isn't 0, the number directly after your array (which you don't even know if there is one, necessarily) doesn't have to be 0, so why would your loop stop?
    I don't know either, but in my understanding the last array should be the value '\0'. Anyway could you show me the running example for this problem ?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is no \0 anywhere in your code, why would you expect there to be any in the program? (\0 only ends strings, it doesn't end random arrays of integers.)

    So p will iterate over b[0], then b[1], then b[2], then b[3], then b[4], then b[5], then b[6] which is probably i, then b[7] which is whatever memory comes after that, then b[8] which is whatever memory comes after that, and so on.

    You have to get lucky to get 32 consecutive 0 bits in a row where your pointer is looking.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by tabstop View Post
    There is no \0 anywhere in your code, why would you expect there to be any in the program? (\0 only ends strings, it doesn't end random arrays of integers.)

    So p will iterate over b[0], then b[1], then b[2], then b[3], then b[4], then b[5], then b[6] which is probably i, then b[7] which is whatever memory comes after that, then b[8] which is whatever memory comes after that, and so on.

    You have to get lucky to get 32 consecutive 0 bits in a row where your pointer is looking.
    Hi tabstop,

    I still curious about his, i think when the p point to out of array of b, i this case because i have put
    Code:
    b[] = {2,3,4,2,3,6}
    , the maximum array that can be pointed by p is b[5], and if the p tries to point b[6], the loop will be terminated. am i right?

    Any body can repair my code?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by unikgila View Post
    Hi tabstop,

    I still curious about his, i think when the p point to out of array of b, i this case because i have put
    Code:
    b[] = {2,3,4,2,3,6}
    , the maximum array that can be pointed by p is b[5], and if the p tries to point b[6], the loop will be terminated. am i right?
    Absolutely positively not. Your function has no way of knowing it's been handed an array; all it knows it has is a pointer. If you want the function to know about the length of the array, you'll have to pass that in as a second parameter.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by tabstop View Post
    Absolutely positively not. Your function has no way of knowing it's been handed an array; all it knows it has is a pointer. If you want the function to know about the length of the array, you'll have to pass that in as a second parameter.
    Thanks tabstop,

    I add second parameter for the number of array in b. it solve the problem. And again thank you for your explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM