Thread: Program Error

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    13

    Program Error

    This program is printing weird. Can anyone help me how to fix it?

    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
    	int a [ ] = {0, 2, 4, 6, 8},
    		*p = a + 3;
    
    	printf("&#37;s%d%s\n%s%d%s\n")
    		"a[?] = ", *p, "?",
    		"a[?+1] = ", *p + 1, "?");
    	return 0;
    }

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Shindel View Post
    This program is printing weird. Can anyone help me how to fix it?

    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
    	int a [ ] = {0, 2, 4, 6, 8},
    		*p = a + 3;
    
    	printf("%s%d%s\n%s%d%s\n")
    		"a[?] = ", *p, "?",
    		"a[?+1] = ", *p + 1, "?");
    	return 0;
    }
    That's not a program, sorry. It is more like when my four year old gets a hand to my keyboard.
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    LOL....well its a lab for class and when it prints, it prints weird.

  4. #4
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Shindel View Post
    LOL....well its a lab for class and when it prints, it prints weird.
    I am even surprise it prints for you at all.


    Code:
    printf("&#37;s%d%s\n%s%d%s\n") /* what's that ) there? */
    There's no string in your array of type int called a. All of those are single integers in army formation , thus you can not pass a %s to printf at all.

    About these:
    Code:
    		"a[?] = ", *p, "?",
    		"a[?+1] = ", *p + 1, "?");
    No idea what they are. I suppose you were trying to do some assignments and using them as part of the printf arguments, but it doesn't work like that.
    Last edited by Aia; 02-04-2008 at 07:15 PM.
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Shindel View Post
    LOL....well its a lab for class and when it prints, it prints weird.
    Are you saying that you can actually compile it? The syntax is wrong. From gcc:

    foo.c: In function ‘main’:
    foo.c:10: error: expected ‘;’ before string constant
    foo.c:11: error: expected statement before ‘)’ token

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Yea It acutally compiles....but when it prints...it prints part of the code.

    This is what it prints:

    a[?] = 6?
    a[?+1] = 7?

  7. #7
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Shindel View Post
    Yea It acutally compiles....but when it prints...it prints part of the code.

    This is what it prints:

    a[?] = 6?
    a[?+1] = 7?
    Amazing.

    Assuming we take the extra parenthesis after \n"
    Code:
    printf("&#37;s%d%s\n%s%d%s\n", "a[?] = ", *p, "?","a[?+1] = ", *p + 1, "?");
    The first %s is for the string "a[?] = "
    The first %d is for *p
    The second %s is for "?"
    Then a newline
    The third %s is for "a[?+1] = "
    The second %d is for *p + 1
    and then it just display "?" as a literal string.

    Don't ask me to go through that again
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Shindel View Post
    Yea It acutally compiles....but when it prints...it prints part of the code.

    This is what it prints:

    a[?] = 6?
    a[?+1] = 7?
    How did you copy the code? Did you use copy and paste, which would ensure accuracy, or did you retype the whole thing by hand? If the latter, please compare the two and fix any errors in what you posted.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int* p - declares pointer to int
    p = a+3 - makes pointer to point the 4th element of the array a which is 6

    *p later retreives this element to print it
    *p+1 - does the same, then adds one (getting 7) and prints the resulting value.

    So your "weird" output is as expecting. It is just hard to understand due to messy coding style
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM