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

trying to pass an array of structure with the index.

momotaro
100+
P: 357
hi every one im trying to pass to a function an array with its index:
Expand|Select|Wrap|Line Numbers
  1. //this is my struct
  2. typedef struct{
  3.     char word[20];
  4.     int anagrams;
  5. }Word;
  6.  
  7. //this is the prototype of the function:
  8. int find_anagrams(char **, char **);
  9. //code...
  10. if(find_anagrams(word_arr[i].word, word_arr[j].word));
  11. //code...
  12. //the definition(where there is the problem!!!!)
  13. int find_anagrams(char *word1, char *word2)
  14. {
  15.     word_arr[i]. // dont work i can not access my struct!!!
  16. }
Jun 17 '07 #1
Share this Question
Share on Google+
9 Replies


ilikepython
Expert 100+
P: 844
hi every one im trying to pass to a function an array with its index:
Expand|Select|Wrap|Line Numbers
  1. //this is my struct
  2. typedef struct{
  3.     char word[20];
  4.     int anagrams;
  5. }Word;
  6.  
  7. //this is the prototype of the function:
  8. int find_anagrams(char **, char **);
  9. //code...
  10. if(find_anagrams(word_arr[i].word, word_arr[j].word));
  11. //code...
  12. //the definition(where there is the problem!!!!)
  13. int find_anagrams(char *word1, char *word2)
  14. {
  15.     word_arr[i]. // dont work i can not access my struct!!!
  16. }
Unless word_arr is global, you won't be able to access it through the function. Do you want to pass the struct as an arguement instead of just the char *?
Expand|Select|Wrap|Line Numbers
  1. if(find_anagrams(word_arr[i], word_arr[j])){}
  2.  
Please explain what you are trying to do.
Jun 17 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
//this is my struct
typedef struct{
char word[20];
int anagrams;
}Word;

//this is the prototype of the function:
int find_anagrams(char **, char **);
//code...
if(find_anagrams(word_arr[i].word, word_arr[j].word));
//code...
//the definition(where there is the problem!!!!)
int find_anagrams(char *word1, char *word2)
{
word_arr[i]. // dont work i can not access my struct!!!
}
You do not pass a char*. You pass a Word or a Word*:
Expand|Select|Wrap|Line Numbers
  1. int find_anagrams(Word *word1, Word* word2)
  2. {
  3.      //in here you use word1->word to use the array
  4. }
  5.  
Jun 17 '07 #3

P: 55
int find_anagrams(struct nameofyourstruct word[], int size);

The previous notation is much more efficient. I just recommended another approach to do so without using pointer.
Jun 18 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
int find_anagrams(struct nameofyourstruct word[], int size);

The previous notation is much more efficient. I just recommended another approach to do so without using pointer.
This still uses a pointer.

struct nameofyourstruct word[]

is the same as

struct nameofyourstruct* word
Jun 18 '07 #5

Savage
Expert 100+
P: 1,764
This still uses a pointer.

struct nameofyourstruct word[]

is the same as

struct nameofyourstruct* word
There are some slight diferances,but it can be sayed that they are equal..

Savage
Jun 18 '07 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
There are some slight diferances,but it can be sayed that they are equal..
What are the differences?
Jun 18 '07 #7

Savage
Expert 100+
P: 1,764
What are the differences?
You don't need to allocate memory for [],but you do need to allocate it for *.

Savage
Jun 18 '07 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
You don't need to allocate memory for [],but you do need to allocate it for *.
Expand|Select|Wrap|Line Numbers
  1. char arr[];
  2. char* arr1;
  3.  
This compiles and no memory was allocated. Of course to use these things tou wiull need to allocate memory. I'm just saying that char arr[] and char* arr1 are equivalent since the name fo the array is the address of element 0. Therefore arr and arr1 are both addresses of char, ot char*

The most you might say is that char[] involves an array but the compiler has no way to check this. All this is, really, is initialization syntax where you have the compiler allocate an array based on the number of enumerated elements.
Jun 19 '07 #9

Expert 10K+
P: 11,448
There are some slight diferances,but it can be sayed that they are equal..

Savage
There are no difference in the value context it is used here (read: as a parameter
to a function) a T[] is the same as a T*

kind regards,

Jos
Jun 19 '07 #10

Post your reply

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