Begginer's array question

Hey all, sorry for my newbishness :)

But umm, does anyone know the correct code for setting strings as the elements of an array?

For example, this practice book I've picked up has a challenge program at the end. It asks me to design a program in which the user can input monthly sales figures for an entire year using loops.

What I want to do is create something like:

char months[12]={jan, feb, march, april......};

Then I'll have a loop increment the array starting from months[0]. However, I can't seem to find the method to make this work. My book only features integer arrays, not character strings.

Thanks

Comments

  • : Hey all, sorry for my newbishness :)
    :
    : But umm, does anyone know the correct code for setting strings as the elements of an array?
    :
    : For example, this practice book I've picked up has a challenge program at the end. It asks me to design a program in which the user can input monthly sales figures for an entire year using loops.
    :
    : What I want to do is create something like:
    :
    : char months[12]={jan, feb, march, april......};
    :
    : Then I'll have a loop increment the array starting from months[0]. However, I can't seem to find the method to make this work. My book only features integer arrays, not character strings.
    :
    : Thanks
    :

    A string is an array of characters, so you would need to make a 2D-array:

    [code]
    char months[12][4] =
    {
    "jan",
    "feb",
    "mar",
    /* and so on... */
    };
    [/code]

    12 is the number of strings. 4 is the maximum string length allowed. In this case it is 3, but since C strings need null termination, you need to add 1 to reserve room for it.

    When writing "jan", you get the null termination added at the end automaticly. So the above example is equal to:

    [code]char months[12][4] =
    {
    {'j', 'a', 'n', ''},
    {'f', 'e', 'b', ''},
    {'m', 'a', 'r', ''},
    /* and so on... */
    };[/code]


  • Thanks! Yeah, I was thinking that a 2d array might be the way to go, however, it just seemed like a lot of work, I figured their might exist a quicker way :) Well, good programming takes time I guess, thanks!

    : : Hey all, sorry for my newbishness :)
    : :
    : : But umm, does anyone know the correct code for setting strings as the elements of an array?
    : :
    : : For example, this practice book I've picked up has a challenge program at the end. It asks me to design a program in which the user can input monthly sales figures for an entire year using loops.
    : :
    : : What I want to do is create something like:
    : :
    : : char months[12]={jan, feb, march, april......};
    : :
    : : Then I'll have a loop increment the array starting from months[0]. However, I can't seem to find the method to make this work. My book only features integer arrays, not character strings.
    : :
    : : Thanks
    : :
    :
    : A string is an array of characters, so you would need to make a 2D-array:
    :
    : [code]
    : char months[12][4] =
    : {
    : "jan",
    : "feb",
    : "mar",
    : /* and so on... */
    : };
    : [/code]
    :
    : 12 is the number of strings. 4 is the maximum string length allowed. In this case it is 3, but since C strings need null termination, you need to add 1 to reserve room for it.
    :
    : When writing "jan", you get the null termination added at the end automaticly. So the above example is equal to:
    :
    : [code]char months[12][4] =
    : {
    : {'j', 'a', 'n', ''},
    : {'f', 'e', 'b', ''},
    : {'m', 'a', 'r', ''},
    : /* and so on... */
    : };[/code]
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories