Thread: C coding questions

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    C coding questions

    Hi,

    I having a some trouble to figure out some questions my instructor gave me to answer.

    I'm not sure how to get this answer can someone explain why?
    -I thought the answer was -1120-2
    -but the answer is -112
    Code:
    int main()
    {
    	int a[] = {-2,-1,0,1,2,2,1,0,-1,-2};
    	int *p;
    	for(p=a+1; p<a+10 && *p; p+=2)
    		printf("%d", *p);
    	return 0;
    }
    I think the answer is 4, but the compiler gives me an answer of 3. Is there a reason why?
    Code:
    int main()
    {
    	int a[]={-6. -4, -2, 0, 2, 4, 6};
    	int *p = a+4;
    	while(*p++)
    		putchar('*');
    	return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    For your first block of code, look at the condition:

    Code:
    p<a+10 && *p
    It's really a combination of two conditions:

    Code:
    p<a+10
    Code:
    *p
    Remember in C, boolean expressions are really resolved to numbers which are then resolved to a true or false value. All numbers in C are true except 0. Zero is the only way to achieve false.

    The first condition:

    Code:
    p<a+10
    Now a is going to be taken as the address of the first element of the array. p is obviously going to be taken as the address it points to. So as long as p points to an address lower than (a + 10), then this condition is true. The thing to realize, though, is that this isn't straight math. Since pointers are involved, the expression is taken to mean (a + (sizeof(*a)*10)). This basically means as long as p isn't pointing to something more than or equal to 10 ints away from a[0], the condition is true. If the pointer math is confusing, you'll probably cover it later, so don't worry about it for now until you have to cover it.

    The second condition is this:

    Code:
    *p
    This means that you take whatever p points to and use it as a boolean value. If you remember about the rules of how C boolean conditions are evaluated, you should realize that this expression is true if and only if p points to a number that is not 0.

    To answer your original question, the loop stops because p contains an address where the value inside that address is 0, which is resolved as false.

    Now regarding your second block of code:

    This example demonstrates undefined behavior, which is a fancy way of saying your program is broken before it starts. This is the issue:

    p is initialized to point to a[4], which is the element with the 2 in it. The condition is this:

    Code:
    *p++
    This causes p to be incremented by one (actually because of pointer arithmetic it's incremented in this case by p+sizeof(*p), but again, ignore this if you don't need it now. Just realize that it points to the next int in the array). After p is incremented, it is dereferenced (the value where it points to is taken), and the value is then compared for true or false.

    So what's the ending condition? The condition is that the loop will end when p points to a variable that is equal to 0. Well, looking at your array you can easily see that this won't happen before p hits the end of the loop.

    I compiled your code with MinGW, and ran it with some minor changes. The code resulted in 2 stars. Why? One thing I noticed was that there was a '.' instead of a comma, separating the -6 from the -4. Changing that resulted in 15 stars.

    What happened? The pointer went past the end of the arrary and started reading memory it wasn't supposed to end up reading. This kept happening until it found an array that had 0. This is not good to do because the C standard says that such behavior is undefined. This means the compiler is allowed to compile the code to do anything. Depending upon your compiler, your O/S, and perhaps other things, you may get any number of stars to print.

    For example, I compiled the same program, both with the '.' and without it, with LCC, and it resulted in 12 stars for both versions compiled with LCC.
    Last edited by MacGyver; 08-29-2007 at 01:20 AM. Reason: Heavy editing and fixing up.... Yes, I'm tired.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    Thanks for the explantation, it is very useful.

    For the compiler, I use Gvim.
    Last edited by vopo; 08-29-2007 at 01:35 AM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Gvim isn't a compiler...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. Float/double compiler error and TONS of questions!
    By musayume in forum C Programming
    Replies: 5
    Last Post: 10-24-2001, 01:40 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM