Thread: Trying to understand Arrays

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    Trying to understand Arrays

    I am having a hard time grasping the concept of Arrays... I have read about 100 different resources on arrays (Generic at this point) and cannot seem to figure it out.

    Here is my attempt at what I think is a simple array.
    Code:
    #include <stdio.h>
    
    int main()
    {
        float a[13]; // Days of Christmas items
        a[0] = Nothing;
        a[1] = A Partridge in a Pear Tree;
        a[2] = Two Turtle Doves;
        a[3] = Three French Hens;
        a[4] = Four Calling Birds;
        a[5] = Five Gold Rings;
        a[6] = Six Geese A-laying;
        a[7] = Seven Swans A-swimming;
        a[8] = Eight Maids A-milking;
        a[9] = Nine Ladies Dancing;
        a[10] = Ten Lords A-leaping;
        a[11] = Eleven Pipers Piping;
        a[12] = Twelve Drummers Drumming;
        
        printf("On the ____ day of Christmas your true love gave to you\n");
        printf("Enter the day of Christmas\n");
        scanf("%f", &a);
        
     
     getchar(); 
    
    }
    However when I compile this I get a return error of my floats being undeclared. Am I even on the right track, is this even an array?

    Thank You in advance.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Try to grasp the concept of floats vs strings first.

    Learn what a C String is and a floating point number. Learn how to use them both first. What you're doing now is trying (incorrectly) to interchange them all around.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, you need quotes aroung any string, so
    Code:
    A Partridge in a Pear Tree
    is treated by the compiler as a list of (undefined) variables, and you'll probably also get some other errors because it's not "correct C". Make it
    Code:
    "A Partridge in a Pear Tree"
    instead.

    Now, unfortunately, that's not the only problem. Your type for the array is "float", and you can't store strings in a float - it's meant to store floating point numbers, e.g. 3.14159, 0.0001, 1000101002321.0976275, etc.

    A char-pointer would be a better choice ("chat *").

    --
    Mats

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok you have an array of floats called 'a' and it has storage for 13 floats, indices 0-12. each one stores one float.

    when you assign any element of the array you have to give it a value whos data type is float, which is a real number, ie 0, 1, -1, 3.14, 333333, etc.

    your (kind of) assigning strings to it, which cant be done.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    Thank you for the information all... back to the board.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Try something like this...

    char *sevendays[7];

    sevendays[0] = "On the first day blah blah"
    ...ect...ect...

    for (int i = 0; i < 7; i++)
    printf("On the &#37;d day of christmas, my true love gave to me %s\n", i+1, sevendays[i]);

    I don't know why you would think a string was a float...

    What you have there is mutilple string values with something in common, you want to group them together by number, so you use an array. In C there are only null-terminated strings, which are of type char pointer. So make and array of how many strings (char *), well the number of days or whatever...
    Last edited by MacNilly; 08-16-2007 at 10:10 PM.

Popular pages Recent additions subscribe to a feed