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

why i cant access the structure from this point???

momotaro
100+
P: 357
am trying to access a member of a structure from my function but it didn't work????? please help!!!

Expand|Select|Wrap|Line Numbers
  1. //my struct:
  2. typedef struct{
  3.     char *file_name[MAXWORD];
  4.     char *word[MAXWORD];
  5.     int count_words;
  6. }Text;
  7. //some code...
  8.  
  9. void Count_word(Text *input_file)
  10. {
  11.        int i = 0;
  12.        for(fscanf(Text.file_name, " %s", Text.word[i]);
  13.        strcmp(Text.word[i], "#") != 0;
  14.        fscanf(Text.file_name, " %s", Text.word[i++]));
  15.        Text.count_words = i - 1; // why can't I acces the structure from here?
  16. }
Jun 16 '07 #1
Share this Question
Share on Google+
4 Replies


momotaro
100+
P: 357
I have found the problem but i don't understand why it works like that???


the solution:

Expand|Select|Wrap|Line Numbers
  1. //replace:
  2. Text.count_words = i - 1;
  3. //by:
  4. input_file->count_words = i - 1;
  5.  
  6. //MAGIC... 
  7.  
Jun 16 '07 #2

Savage
Expert 100+
P: 1,764
I have found the problem but i don't understand why it works like that???


the solution:

Expand|Select|Wrap|Line Numbers
  1. //replace:
  2. Text.count_words = i - 1;
  3. //by:
  4. input_file->count_words = i - 1;
  5.  
  6. //MAGIC... 
  7.  
You have created a pointer to the struct,right?

Like:

Text *input_file;

allocatted memory for him,right?

Now you want to access the member of the struct on which input_file points.This is what arrow operator does,it access data that your pointer is pointing to.There is a way to do it with operator '.',but it's messy and it has compicated syntax.

Savage
Jun 17 '07 #3

Expert 10K+
P: 11,448
am trying to access a member of a structure from my function but it didn't work????? please help!!!

Expand|Select|Wrap|Line Numbers
  1. //my struct:
  2. typedef struct{
  3.     char *file_name[MAXWORD];
  4.     char *word[MAXWORD];
  5.     int count_words;
  6. }Text;
  7. //some code...
  8.  
  9. void Count_word(Text *input_file)
  10. {
  11.        int i = 0;
  12.        for(fscanf(Text.file_name, " %s", Text.word[i]);
  13.        strcmp(Text.word[i], "#") != 0;
  14.        fscanf(Text.file_name, " %s", Text.word[i++]));
  15.        Text.count_words = i - 1; // why can't I acces the structure from here?
  16. }
Do you realize that both file_name and word are arrays of char pointers?
The input_file parameter itself is a pointer to a Text structure so you reference
the members of the structure like this:

Expand|Select|Wrap|Line Numbers
  1. input_file->file_name;
  2. input_file->word;
  3. input_file->count_words;
  4.  
kind regards,

Jos
Jun 17 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Do you realize that both file_name and word are arrays of char pointers?
The input_file parameter itself is a pointer to a Text structure so you reference
the members of the structure like this:


Code: ( c )
input_file->file_name;
input_file->word;
input_file->count_words;


kind regards,
Yes. And be sure to allocate memory for those pointers before you use them.
Jun 17 '07 #5

Post your reply

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