How to assign and access data from a struct of arrays in C -
i'm trying create array of type struct i'm having trouble assigning , accessing data once i've created it. have basic understanding of things in c, i'm still new.
here declaration of struct , array:
typedef struct search{ char word[10]; }; struct search words[40];
the first time need use array when need store string in first element (from command line argument). mistake, syntactically , theoretically?
words[0] = *argv[count]; //it says can't assign char struct words
the next time need access inside function. first line how have called function, , post function prototype , lines inside giving me trouble. please let me know if need clarify structure.
parsesearchwords(words); // function call int parsesearchwords(struct search *word); // function prototype word[0][0] = 'a';// lines giving me errors printf("%s\n", *word[0][0]);
although i'm sure it's obvious what's wrong statement, error is: subscripted value neither array not pointer nor vector.
thanks help, please let me know if can clarify anything.
struct search { char word[10]; };
defines structure search
contains array of char
s of size 10 member. when want access elements (char
s) of array, need explicitly access member :
int parsesearchwords(struct search *word) { ... word[0][0] = 'a'; printf("%s\n", *word[0][0]); }
should be:
int parsesearchwords(struct search *s) { ... s[0].word[0] = 'a'; printf("%s\n", s[0].word); }
and copying argv
should more this:
struct search words[40]; int = 0; (; < argc; ++i) { strncpy(words[i].word, *argv[i], 9); words[i].word[9] = '\0'; }
and in case want use typedef
, suggest using more meaningful name search
, written in uppercamelcase:
typedef struct search{ char word[10]; } word;
then prototype of parsesearchwords
function might become:
int parsesearchwords(word *words)
Comments
Post a Comment