-1

I'm trying to create an array of type struct but I'm having some trouble assigning and accessing data from it once I've created it. I have a basic understanding of what things do in C, but I'm still very new.

Here is my declaration of the struct and array:

typedef struct search{ 
    char word[10];
};

struct search words[40];

The first time I need to use the array is when I need to store a string in the first element (from a command line argument). What is my mistake, syntactically and theoretically?

words[0] = *argv[count]; //It says I can't assign char to struct words

The next time I need to access it is inside a function. The first line is how I have called the function, and then I will post the function prototype and then the lines inside that are giving me trouble. Please let me know if I need to clarify that structure.

parseSearchWords(words); // function call


int parseSearchWords(struct search *word); // function prototype


word[0][0] = 'a';// THe lines giving me the errors
printf("%s\n", *word[0][0]);

Although I'm sure it's obvious what's wrong with the statement, the error is: subscripted value is neither array not pointer nor vector.

Thanks for the help, please let me know if I can clarify anything.

LihO
  • 41,190
  • 11
  • 99
  • 167
Vayran
  • 77
  • 1
  • 4
  • 1
    Before you use arrays, write a *simpler* program with just one single variable of type `search` and make sure you understand how *that* works. – Kerrek SB Oct 07 '13 at 08:19

2 Answers2

1

What is my mistake, syntactically and theoretically?

searchWords[0] = *argv[count]; //It says I can't assign char to struct words

you should copy the argv[count] string into the searchWords[0]. The searchWords[0] is an array and it's not a pointer so it could not point to the argv string. Please refer to the following link to see the difference pointer and array: *(a++) is giving error but not *(a+1)?? where a is array name?

You could copy argv[count] string into the searchWords[0] in this way:

strcpy(searchWords[0], argv[count]);

And I think you mean words[0] and not searchWords[0]. If the case you can not access to the first array in this way. it should be words[0].word. So the copy will be in this way

strcpy(words[0].word, argv[count]);
Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0
struct search { 
    char word[10];
};

defines a structure search that contains an array of chars of size 10 as a member. When you want to access the elements (chars) of this array, you need to explicitly access this 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 look more like this:

struct search words[40];
int i = 0;
for (; i < argc; ++i) {
    strncpy(words[i].word, *argv[i], 9);
    words[i].word[9] = '\0';
}

And in case you want to use typedef, then I suggest you using more meaningful name than search and written in UpperCamelCase:

typedef struct search{ 
    char word[10];
} Word;

then the prototype of parseSearchWords function might become:

int parseSearchWords(Word *words)
LihO
  • 41,190
  • 11
  • 99
  • 167
  • @Vayran: You're welcome :) See my edit, I realized that the pointer you send to `parseSearchWords` is an address of that `words` array you were filling with `argv` right? :) The way you access its elements should be a bit different :) – LihO Oct 07 '13 at 08:35