![]() |
|
#1
|
|||
|
|||
Array/String questionIm having some trouble understanding array's and string's. I know that an string is a number of characters all put together but Im having trouble using them. I would like to have a 2D array and have a string from this array entered and scaned into the program(i can do this). However i would like to later call this string and assign the number of where it is in the array to it.
that might be kinda confusing so heres an example. array[10][7] = {....................} (array's listed in brackets) then i scan in a string from this array (ex. monday) then in a later function i call the scaned in string but i get a number out of it (ex. 0-9) thanks for any help |
|
#2
|
|||
|
|||
Re: Array/String questionheres what i have so far
CPP / C++ / C Code:
and as i said im having trouble with the search function and how to return the number 0-9 of the array. this is need to calculate the resistance Last edited by LuciWiz : 09-Nov-2006 at 16:52.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#3
|
|||
|
|||
Re: Array/String questionQuote:
I'll reword it slightly: In C there is no such thing as a "string" data type. C library functions need a pointer to char as arguments, and the name of an array of char used by itself (without the [] brackets) is treated as a pointer to char. Therefore, an array of char can hold a "string" used in C programs. In order to use functions such as strcpy(), strcat, etc, the contents of the array will be a zero-terminated sequence of chars. In C there is such a thing as a "string literal". This is denoted by enclosing something in quote marks. The string literal is treated as a pointer to char, and the value of the pointer is the address somewhere in program data space of the first character of the "string". Therefore string literals can be used as arguments to string functions (but note that you can read from a string literal but you shouldn't try to write to it). We can declare an array of pointers to char, and initialize it so that each element of the array has a value that points to a string literal. In the following example, the array names is an array of pointers, and it is initialized to some specific things. Therefore, things like names[0], names[1], etc. can be used as arguments to the various string functions (printf("%s"), strcpy, etc). And the array can be considered to be an array of constant "strings". An example: CPP / C++ / C Code:
Output: Code:
Another example, that searches for a name among the crew members: CPP / C++ / C Code:
Output Code:
Note that to use scanf to get a "string", you give the address of the first element of the array. When you use the name of an array without the "[]" brackets, it is treated as pointer whose value is the address of the first element of the array. Regards, Dave |
|
#4
|
|||
|
|||
Re: Array/String questionthanks for clarifying what a string and array exactly are. however, im still having some problems with the search. i understand what you said but with my program i would like to enter multiple strings and from each string return a subscript of the list element and use these numbers to do math.
ex(number returned for first string: 2 number returned for second string: 4 number returned for third string:1 2+4+1=7) or something to that extent ive revised my previous code and it collets the colors but does not produce an answer CPP / C++ / C Code:
Last edited by LuciWiz : 09-Nov-2006 at 16:52.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#5
|
|||
|
|||
Re: Array/String questionQuote:
With my example, your array would look like this: CPP / C++ / C Code:
If you want a 2-D array of chars instead of an array of pointers to char, you could have this: CPP / C++ / C Code:
I didn't cover this, but what it does is the following: 1. It allocates memory for a 2-D array of char. 2. It initializes the "rows" of the array as follows: It copies the string literal "black" to row number 0 of the array It copies the string literal "brown" to row number 1 of the array etc. Now you could use either one and your program would work. It will not work with the way you declared the array, since you had a 2-D array of pointers. Not useful for this program. Note that, for either of the two ways that I suggested, the notation for the rest of the program would be not identical, but either way could be used. The second method would allow you to change the "strings" in the array contents if the program required it, but either way would work in your program. I would put in a print statement for debugging purposes, just to make sure that the array is initialized properly. CPP / C++ / C Code:
Quote:
So, define a search function that takes a string as an argument (since the user enters a string) as well as the array and the count. Have the search function return the index of the color that the user entered. Call the function three times; once for each color band. Do the arithmetic on the three values that were returned: CPP / C++ / C Code:
Now, use strcmp() in your search program to detect a match between the user entry and members of the array. You should be able to get something like: Code:
If you want a single function that calculates the value, then give it the three user strings as arguments (as well as the array name and size). That function will call the search() function three times and do the math on the returned values. Of course, you could take out the extra print statements after debugging was complete. Regards, Dave |
|
#6
|
|||
|
|||
Re: Array/String questionthanks for all your help by the way.
now ive updated it and put in strcmp but it now doesnt compile due to "the passing of arguement 1 of 'search' from incompatable pointer type". i thought i had all the pointers defnined tho. heres the revised version CPP / C++ / C Code:
Last edited by admin II : 10-Nov-2006 at 05:24.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#7
|
|||
|
|||
Re: Array/String questionQuote:
So change the variable type declaration for color_codes to match the argument type. If you are compiling this as a C program the message should be a warning, not an error. On the other hand there are errors: When you call a function, you must have an argument for every function parameter. The function definition has nine arguments. You call it three times with three arguments each time. That doesn't compute. Also the function itself uses something called "target", but there is no argument named "target". The function looks up one value in a table. Look at the code and think about what the code is supposed to do. Then make it do it. Regards, Dave |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Borland compile question | monnick | C++ Forum | 4 | 12-Feb-2006 18:40 |
| non-member function question | crq | C++ Forum | 1 | 03-Feb-2005 22:59 |
| Simple question on arrays--please help! | brookeville | C++ Forum | 16 | 18-Nov-2004 00:23 |
| Repetition structure problem and question | brookeville | C++ Forum | 17 | 29-Oct-2004 18:48 |
| question of practice | magiccreative | C++ Forum | 1 | 06-Feb-2004 08:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The