Find the answer to your Linux question:
Results 1 to 5 of 5

Thread: Array in C

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Array in C

    Hi, i'm new to C, and I'm trying to do array in c. My code is below, and this code somehow when I run the compiler it give me runtime error. The black screen only show one line of printf("The automobile type is:\n"); and that is, then it give me error that I have to close the program. Btw i'm using pcGRASP compiler.

    Can you tell me what I did wrong, please anyone?

    Here is the code:

    #define SIZE 5
    int main()
    int i;
    char autoMobile[SIZE]={"Honda","Toyota","Nissan","Ford","BMW"};

    printf("The automobile type is:\n");

    for(i = 0; i<SIZE; i++)
    printf("%d%10s\n", i, autoMobile[i]);

    return 0;

  2. #2
    Linux Guru
    Join Date
    Apr 2003
    London, UK
    Hiya, welcome to this_site_does_not_exist!

    Working code is:

    #include <stdio.h>
    #define SIZE 5
    int main&#40;&#41; &#123;
            int i;
            char *autoMobile&#91;SIZE&#93;=&#123;"Honda","Toyota","Nissan","Ford","BMW"&#125;;
            printf&#40;"The automobile type is&#58;\n"&#41;;
            for&#40;i = 0; i<SIZE; i++&#41;
                    printf&#40;"%d %s\n", i, autoMobile&#91;i&#93;&#41;;
            return 0;
    and the output is:

    bash-2.05a# gcc prob.c -Wall -o x; ./x
    The automobile type is:
    0 Honda
    1 Toyota
    2 Nissan
    3 Ford
    4 BMW
    the problem was on the line
    char *autoMobile&#91;SIZE&#93;=&#123;"Honda","Toyota","Nissan","Ford","BMW"&#125;;
    You did not include the '*' in front of the variable name "AutoMobile".

    Bascally you need to put the * there beacase in fact you are declaring an Array Of Pointers(which point to text strings), as opposed to an array of text.

    As you learn more C and get into pointers this will become much clearer.


  3. #3
    Thanks... ... now I know hehehe..

  4. $spacer_open
  5. #4
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    And knowing is half the battle...
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

  6. #5
    Just Joined!
    Join Date
    Sep 2003
    I'm just startiing C programming too, and this tutorial is excellent, here is alink to its info on arrays and pointers: , on the page before this there is some more detailed info about pointers in particular.

    Kind Regards
    Mark Rawson

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts