Why Does My C Program That Uses A Function To Total Months in a Year Fail? -
i've written program that's gradually grown simple structure holds information month, array of 12 structures of same information non-leap year. now, i'm trying include function that, "when given month number, returns total days in year including month. assume structure template of question 3 , appropriate array of such structures declared externally."
when press run 2 errors, neither of understand. here are: "undefined symbols architecture x86_64: "_months", referenced from: _days in main.o ld: symbol(s) not found architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation)"
any appreciated, thanks.
#include <stdio.h> int days(int monthnum); struct month { char name[10]; char abbreviaton[4]; int days; int monthnum; }; int main(void) { struct month months[12] = { {"january", "jan", 31, 1}, {"february", "feb", 28, 2}, {"march", "mar", 31, 3}, {"april", "apr", 30, 4}, {"may", "may", 31, 5}, {"june", "jun", 30, 6}, {"july", "jul", 31, 7}, {"august", "aug", 31, 8}, {"september", "sep", 30, 9}, {"october", "oct", 31, 10}, {"november", "nov", 30, 11}, {"december", "dec", 31, 12}, }; struct month *sign; sign = &months[12]; days(months[12].monthnum); return 0; } extern struct month months[12]; int days(int monthnum) { int index, total; if (monthnum < 1 || monthnum > 12) return(-1); else { (index = 0, total = 0; index < monthnum; index++) total += months[index].days; return(total); } }
here code have now. program works:
#include <stdio.h> int days(int monthnum); struct month { char name[10]; char abbreviaton[4]; int days; int monthnum; }; struct month months[12] = { {"january", "jan", 31, 1}, {"february", "feb", 28, 2}, {"march", "mar", 31, 3}, {"april", "apr", 30, 4}, {"may", "may", 31, 5}, {"june", "jun", 30, 6}, {"july", "jul", 31, 7}, {"august", "aug", 31, 8}, {"september", "sep", 30, 9}, {"october", "oct", 31, 10}, {"november", "nov", 30, 11}, {"december", "dec", 31, 12}, }; int main(void) { int value; int count = 0; struct month *sign; sign = &months; printf("enter month number: "); scanf("%d", &months[count].monthnum); value = days(sign->monthnum); printf("%d", value); return 0; } extern struct month months[]; int days(int monthnum) { int index, total; if (monthnum < 1 || monthnum > 12) return(-1); else { (index = 0, total = 0; index < monthnum; index++) total += months[index].days; return(total); } }
the immediate problem due line;
extern struct month months[12];
there no array definition reference here; months
declared inside of main
, else. rid of line.
next have logical issues:
days(months[12].monthnum);
you have overrun bounds of array. arrays 0 indexed, i.e., array of twelve elements contains valid indices 0-11. 12 1 far.
as aside, not need specify dimension of array when explicitly initializing each element. use:
struct month months[] = { {"january", "jan", 31, 1}, {"february", "feb", 28, 2}, {"march", "mar", 31, 3}, {"april", "apr", 30, 4}, {"may", "may", 31, 5}, {"june", "jun", 30, 6}, {"july", "jul", 31, 7}, {"august", "aug", 31, 8}, {"september", "sep", 30, 9}, {"october", "oct", 31, 10}, {"november", "nov", 30, 11}, {"december", "dec", 31, 12}, };
the compiler knows there twelve elements because told so. don't have change dimension when/if add or remove element.
next problem; variable scope. have inside of days
function:
total += months[index].days;
well, days
has no access months
because months
local main
. need study , understand variable scope.
Comments
Post a Comment