473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

trying to pass an array of structure with the index.

momotaro
357 100+
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
9 1828
ilikepython
844 Expert 512MB
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
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
1,764 Expert 1GB
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
9,208 Expert Mod 8TB
There are some slight diferances,but it can be sayed that they are equal..
What are the differences?
Jun 18 '07 #7
Savage
1,764 Expert 1GB
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
9,208 Expert Mod 8TB
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
5
by: Sam | last post by:
Hi All I have couple of question regarding property of a class and structures. **** ---- Here is my class and structure ---- ***** 1. Public Structure MyPoint 2. Dim p As Point 3. ...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
2
by: André | last post by:
Hi, I have to pass a lot of variables from vb.net to javascript using client callback. the problem is that there are single variables, but also arrays with differents indexes. I have two...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
4
by: frizzle | last post by:
Hi there, I have a function to create an array of all files in a certain folder, so i can display the structure. The actual function is below the message, as is an example of its output. As...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.