By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

Is this line of code legale ?

momotaro
100+
P: 357
I've created a structure and trying to reach one of it's members, is this use correct?

Expand|Select|Wrap|Line Numbers
  1. typedef char Word[MAXWORD];
  2. typedef struct{
  3.     Word *file_name;
  4.     Word *word[MAXWORD];
  5.     int count_words;
  6. }Text;
  7. // ...code
  8. fscanf(Text.file_name, " %s", Text.word[i][j]);
Jun 16 '07 #1
Share this Question
Share on Google+
4 Replies


Savage
Expert 100+
P: 1,764
I've created a structure and trying to reach one of it's members, is this use correct?

Expand|Select|Wrap|Line Numbers
  1. typedef char Word[MAXWORD];
  2. typedef struct{
  3.     Word *file_name;
  4.     Word *word[MAXWORD];
  5.     int count_words;
  6. }Text;
  7. // ...code
  8. fscanf(Text.file_name, " %s", Text.word[i][j]);

Have you performed memory allocation?

Savage
Jun 16 '07 #2

momotaro
100+
P: 357
yes. "i have to add this in order to get the min char requiered to submit" :)
Jun 16 '07 #3

Savage
Expert 100+
P: 1,764
yes. "i have to add this in order to get the min char requiered to submit" :)
Then it should work!!


Savage
Jun 16 '07 #4

DeMan
100+
P: 1,806
1.) Not sure why you use double indexing on the printf (if you are trying to print each character indivisually, use %c instead of %s), to use %s I don't think you need the second reference.....
2)you might like to make sure that each word in the array is null terminated....
Using %s prints all characters up to the first Null Terminator, this on the whole does not cause major dramas, but suppose you had a wordd(or words), in the array that contain MAXWORD number of letters, If you print from the beginning of that word, you may inadvertently print the word beyond as well. If this word is at the end of the array, you may print out other memory as well.
3) YOu use MAXWORD to define the size of both arrays, while this is perfectly legal, you might like to consider that the reason to use such defines, is to minimise changes (and confusion caused by them) in the future. I assume at the moment MAXWORD is something like 10 - pretty reasonable, 10 character words are biggish and you only want to use 10 words. Consider if you wanted to change this to 100 (or 1000) in the future. Words of this size are extremely rare, and you waste a lot of space for little reason. I would seperate the constants so that you have (for example) MAXWORD_LENGTH and MAX_NUMBER_OF_WORDS.

That's my two cents, I'm Kent Brockman
Jun 16 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.