Thread: C Program... Program or Compiler error?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    8

    C Program... Program or Compiler error?

    I'm trying to get this program to work but it keeps printing the same thing (the before array and the after array with nothing in it) even when I'm talking the before array out of the program completely and recompiling it. I've restarted my compiler and copied and pasted the program into a new source file but it's still not working. Idk if it's the program (which may have errors I'm still a beginner) or my compiler (Dev C++). Please let me know if you're able to run it and what happens. Thanks!
    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
    // Local Declarations
    char temp;
    char a[6] = {'z', 'x', 'm', 's', 'e', 'h'};
    char* walker;
    char* plast;
    
    // Statements
    
    plast = a + 5;
    temp = *a;
    
    printf("Array before: %c %c %c %c %c %c\n", 'z', 'x', 'm', 's', 'e', 'h');
    
    printf("Array after: ");
    
    for (walker = a; walker < plast; walker++)
    {*walker = *(walker + 1);
    *walker = temp;
    printf (" %c", temp);
    } // for
    
    getch();
    return 0;
    
    }//main

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your output should like like this:
    Code:
    Array before: z x m s e h
    Array after:  z z z z z
    If you are not seeing the 'z's, you probably need to flush the output buffer. After your for loop, add fflush(stdout);.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    The same thing is still happening when I added that after the for loop. I don't understand why but I can't get it to display anything after the "Array After" line... here's the updated code:

    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
    // Local Declarations
    char temp;
    char a[6] = {'z', 'x', 'm', 's', 'e', 'h'};
    char* walker;
    char* plast;
    
    // Statements
    
    plast = a + 5;
    temp = *a;
    
    printf("Array before: %c %c %c %c %c %c\n", 'z', 'x', 'm', 's', 'e', 'h');
    
    
    for (walker = a; walker < plast; walker++)
    *walker = *(walker + 1);
    *walker = temp;
    printf (" %c", temp);
    fflush(stdout);
    
    printf("Array after: ");
    
    getch();
    return 0;
    
    }//main

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That code is completely different. Now you don't have braces after the for loop, so the only line that is looped over is:
    Code:
    *walker = *(walker + 1);
    Please post your exact code. I get the feeling you are modifying what you are pasting here.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    Yeah I changed it because I don't know which way is right.
    The goal of the problem is to use pointer notation to write a loop to rotate all values in a to the left by one element.
    I changed it again below just a little because I'm pretty sure it's how it should work.

    Thank you for your help.

    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
    // Local Declarations
    char temp;
    char a[6] = {'z', 'x', 'm', 's', 'e', 'h'};
    char* walker;
    char* plast;
    
    // Statements
    
    plast = a + 5;
    temp = *a;
    
    printf("Array before: %c %c %c %c %c %c\n", 'z', 'x', 'm', 's', 'e', 'h');
    
    printf("Array after: ");
    
    for (walker = a; walker < plast; walker++)
        *walker = *(walker + 1);
    *walker = temp;
    printf (" %c", temp);
    
    fflush(stdout);
    
    getch();
    return 0;
    
    }//main

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You still are not printing out the array a second time, you are just printing out 1 character.

    To help, you may want to add a print_array function:
    Code:
    void print_array(char* array, size_t size)
    {
        int i;
        for(i = 0; i < size; i++)
            printf("%c ", array[i]);
        putchar('\n');
    }
    Then you can call that whenever you want to print your array like so:
    Code:
    print_array(a, sizeof(a));
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    It still printed the same thing in the dos prompt. I'm floored at why it isn't showing me what the program clearly states. Do you happen to have a compiler you could run it through and see if it shows the before array correctly then the after array with no letters?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Post the exact code you have, and I'll give it a try.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    Thanks.

    Code:
    #include <stdio.h>
    
    void print_array(char* array, size_t size);
    int main (void)
    {
    // Local Declarations
    char temp;
    char a[6] = {'z', 'x', 'm', 's', 'e', 'h'};
    char* walker;
    char* plast;
    
    // Statements
    
    plast = a + 5;
    temp = *a;
    
    printf("Array before: %c %c %c %c %c %c\n", 'z', 'x', 'm', 's', 'e', 'h');
    
    printf("Array after: ");
    
    print_array(a, sizeof(a));
    
    
    /*for (walker = a; walker < plast; walker++)
        *walker = *(walker + 1);
    *walker = temp;
    printf (" %c", temp);
    
    fflush(stdout);
    */
    
    getch();
    return 0;
    
    }//main
    
    void print_array(char* array, size_t size)
    {
    //Local Declarations
        int i;
    
    //Statements
        
        for(i = 0; i < size; i++)
            printf("%c ", array[i]);
        return;
    }

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That prints out:
    Code:
    Array before: z x m s e h
    Array after: z x m s e h
    You took out the fflush(stdout) out of main() and the newline print out of print_array(). Therefore it's possible that you may not see everything printed out (since there is nothing there to explicitly flush the output buffer). What happens if you just run this:

    Code:
    #include <stdio.h>
    
    void print_array(char* array, size_t size);
    
    int main (void)
    {
        char a[6] = {'z', 'x', 'm', 's', 'e', 'h'};
    
        printf("Array before: ");
        print_array(a, sizeof(a));
    
        printf("Array after: ");
        print_array(a, sizeof(a));
    
        getchar();
        return 0;
    }
    
    void print_array(char* array, size_t size)
    {
        size_t i;
        for(i = 0; i < size; i++)
            printf("%c ", array[i]);
        putchar('\n');
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    The same exact thing.

    Array before: z x m s e h
    Array after: (nothing)

    I'm going to try it with another compiler in a minute- I'll let you know what happens- hopefully it will work normal and I'll just have to redownload mine.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I haven't used Dev-C++ before, but I heard that it's not supported that well. I think most people use Code::Blocks on Windows now.
    bit∙hub [bit-huhb] n. A source and destination for information.

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    Okay, I may try downloading it and see if it helps because the other compiler I haven't just isn't working in general =-( But you said it did show the correct output for you right? Because that could be just my issue

  14. #14
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    I'm getting the right output now in Code::Blocks but now I just can't figure out how to make it display what I want =-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 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. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM