473,656 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trying to pass an array of structure with the index.

momotaro
357 Contributor
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 1835
ilikepython
844 Recognized Expert Contributor
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 Recognized Expert Moderator Expert
This code:
//this is my struct
typedef struct{
char word[20];
int anagrams;
}Word;

//this is the prototype of the function:
int find_anagrams(c har **, char **);
//code...
if(find_anagram s(word_arr[i].word, word_arr[j].word));
//code...
//the definition(wher e there is the problem!!!!)
int find_anagrams(c har *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
Peterwkc
55 New Member
int find_anagrams(s truct nameofyourstruc t 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 Recognized Expert Moderator Expert
int find_anagrams(s truct nameofyourstruc t 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 nameofyourstruc t word[]

is the same as

struct nameofyourstruc t* word
Jun 18 '07 #5
Savage
1,764 Recognized Expert Top Contributor
This still uses a pointer.

struct nameofyourstruc t word[]

is the same as

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

Savage
Jun 18 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 Recognized Expert Top Contributor
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 Recognized Expert Moderator Expert
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 Recognized Expert MVP
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
9137
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
7064
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 the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
5
2645
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. Dim ptColor As Color 4. End Structure
104
16929
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 that array Could you show me a little example how to do this? Thanks.
7
3161
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 thorughout the course the programs use so it will need to
2
1628
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 questions: 1) is this technology made for passing a lot of variables or in fact only for one? 2) does it exist a esier way to pass the values and to recover them in javascript?
8
10159
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". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
4
1579
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 one can see, the filesize is also stored in the array (more info will be added, like filemtime), what i need is to sum all bytes per folder.
5
2675
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 that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.