1

I am working on some code to parse log data, this part in particular is used to translate the decimal form of a month to a human form ( so 1 would be jan ), and to accomplish this I am trying to use a combination of a for loop and an if statement, and had thought that I could simply use a variable as the number for the statement to retrieve the array cell, but it did not work out as expected, so if someone could offer advice as to how I can do this I would really appreciate it, thanks! (here is the main chunk of the code)

for ( x = 0; x < 11; x++ ) {
    int altint = 1 + x;
    if ( dmon == altint ) {
            printf( "%s\n", field_month[x] );
            {break;}
    }
}
3
  • 1
    what is in field_month, and dmon? Commented May 17, 2012 at 0:42
  • field month is an array with the human names of the months, so { jan, feb, march, etc } and then dmon is the decimal version of the month Commented May 17, 2012 at 0:44
  • oh shit actually had made a dumb mistake, had mixed up the name of the array itself and the numerical reference to the month, works perfectly now though, thanks! Commented May 17, 2012 at 0:51

3 Answers 3

2

I'd like to suggest that you not use a for loop and instead look up the element that you want directly:

printf("%s\n", field_month[dmon]);

If your dmon is 1-indexed (1 for January, .., 12 for December), then a slight change:

printf("%s\n", field_month[dmon-1]);
Sign up to request clarification or add additional context in comments.

7 Comments

@Evan, note that tpaksu updated his answer to include dmon-1.
True, but your explanation is clear and concise. I updated my comment.
thanks! and if I wanted to save the output would I just save the output of printf to a variable?
@lacrosse1991: You'd have trouble saving the output of printf(3) to a variable -- it, after all, prints to a standard IO stream. Probably better would be something like char *month = field_month[dmon];. (That gives you a pointer to the data -- it doesn't make a copy of it. That will be important later. :)
thanks! worked perfectly as well, sorry if those were dumb questions, still getting the hang of things, the speed at which C operates at though is well worth it :)
|
2

just printf("%s",field_month[dmon]) or field_month[dmon-1] as it depends on your month names array?

Comments

0

Your for loop can be optmized to:

if (dmon >= 1 && dmon <= 12)
{
    printf("%s\n", field_month[dmon]);
}

Because the only thing it's really doing is preventing an invalid index of being used to access your array...

1 Comment

Good point about avoiding bad array accesses -- but arrays are zero-based and thus indexed from 0 to 11 -- unless a bogus element is stored in the first array position.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.