|
![]() |
|
| Author |
|
|||||||
|
Goa`uld!
Posts: 510
Location: Brisbane, Queensland
|
Ok I'm doing this thing... and I need to read a list of words into an array.. and each word has to have it's own index, so I can go like if (wordArray[2] == secondWord) etc... But as far as I'm aware you can't have an array of type String in c. Only char. So is there anyway around this? |
|||||||
| #0 09:22pm 29/08/01 |
|
|||||||
|
system
|
--
|
|||||||
| #0 |
|
|||||||
|
Skitza
Posts: 63
Location: Brisbane, Queensland
|
f*** arrays :) |
|||||||
| #1 09:24pm 29/08/01 |
|
|||||||
|
Goa`uld!
Posts: 511
Location: Brisbane, Queensland
|
hmm interesting concept... |
|||||||
| #2 09:25pm 29/08/01 |
|
|||||||
|
maxe
Posts: 2809
Location: Brisbane, Queensland
|
i think i might have had a similar prob before. there was some kind of magic command to fix it. but then im failing C++ and theres every likelyhood i have no idea what im on about. lemme look. |
|||||||
| #3 09:26pm 29/08/01 |
|
|||||||
|
PeaceKeeper
Posts: 772
Location: Brisbane, Queensland
|
yay for maxe, i failed c :D |
|||||||
| #4 09:28pm 29/08/01 |
|
|||||||
|
KidZ0r
Posts: 1034
Location: Brisbane, Queensland
|
Does it have anything to do with Structures ?? have a structure with a string in it nasd compare structures not strings ??? been a while cannot remember to much hehe |
|||||||
| #5 09:29pm 29/08/01 |
|
|||||||
|
maxe
Posts: 2810
Location: Brisbane, Queensland
|
ummm, i dont understand what you really want, but try looking for a command in the help called "atoi". it converts something to something else, which sounds kinda like what you want. |
|||||||
| #6 09:44pm 29/08/01 |
|
|||||||
|
Goa`uld!
Posts: 512
Location: Brisbane, Queensland
|
I think atoi converts a character into an integer... |
|||||||
| #7 09:47pm 29/08/01 |
|
|||||||
|
log
Posts: 92
Location: Brisbane, Queensland
|
you could just say its an array of character pointers eg char * wordArray[] , but i dont know if this will give you a pointer exception or you could just create a word struct and then make the array an array of pointers to the struct eg : struct wordStruct { char* word; }; typedef struct wordStruct word; word wordArray[] ; wordArra[x]->word = "whatever" |
|||||||
| #8 09:53pm 29/08/01 |
|
|||||||
|
log
Posts: 93
Location: Brisbane, Queensland
|
umm, if you use ure malloc statments correctly option 1 is the way to go |
|||||||
| #9 09:54pm 29/08/01 |
|
|||||||
|
PsykoNit
Posts: 6
Location: Brisbane, Queensland
|
My c is a bit rusty, but i know you can't do a straight comparison between strings. you'll need to do use strcmp (or something like that) to do the comparison. As for storing strings in an array: you can setup an array of strings as a two dimensional array: eg. char strings[10][4]; is the declaration for 10 strings of 3 chars each... (or 4 strings of 9 chars each, i can't remember). |
|||||||
| #10 09:58pm 29/08/01 |
|
|||||||
|
Goa`uld!
Posts: 513
Location: Brisbane, Queensland
|
yeah i tried the multidimensional array but it just didn't work. |
|||||||
| #11 10:03pm 29/08/01 |
|
|||||||
|
...
Posts: 140
Location: Brisbane, Queensland
|
pfftt c get some vb in ya |
|||||||
| #12 10:03pm 29/08/01 |
|
|||||||
|
Ferret
Posts: 105
Location: Sydney, New South Wales
|
larf maxe log and psychonit are right too bad i don't understand the question any more than any one else here did:) i eat breathe and s*** c/C++ code and drink VB dammit like everyone else |
|||||||
| #13 11:13pm 29/08/01 |
|
|||||||
|
Goa`uld!
Posts: 514
Location: Brisbane, Queensland
|
ok I have a problem with a code I wrote. What happens is it freezes, like it's gone into a continuous loop
can anyone find why it might be doing that? |
|||||||
| #14 09:16am 30/08/01 |
|
|||||||
|
Goa`uld!
Posts: 515
Location: Brisbane, Queensland
|
btw the params it accepts are char findThis[] which is an array like "h,e,l,l,o" int findThisLength which is a number that is the length of 'findThis' char inThis[] is a record of words like "h,e,l,l,o, ,t,h,i,s, ,i,s, ,a,n, ,a,r,r,a,y" int inThisLength is the length of inThis. |
|||||||
| #15 01:29am 30/08/01 |
|
|||||||
|
Goa`uld!
Posts: 516
Location: Brisbane, Queensland
|
damn forum took out tab spaces :( |
|||||||
| #16 01:30am 30/08/01 |
|
|||||||
|
Leon Trotsky
Posts: 127
Location: Brisbane, Queensland
|
thx gao'uld - if i was doing taht comp2301 project i'd copy offa you :) fortunately, i chose assignment 2, which i haven't started but should get through (i hope) With yer code, from what I can see (considerin i know what you wanna do here) while((findThisPosition if(inThis[inThisPosition] != findThis[findThisPosition]) { boolean = 0; inThisPosition++; findThisPosition++; } your saying WHILE (true if.....) i think you've found your infinite loop - if I'm reading this wrong (and i might be i think) then I suggest you just do breakpoints, or step throughs and inspect the value of different variables (tutors showed me how) But yeh, taht while statement seems weird to me! gl man - if you get desperate you can have my code :D |
|||||||
| #17 03:13pm 30/08/01 |
|
|||||||
|
Toll Booth Willy
Posts: 1027
Location: Brisbane, Queensland
|
ERKK! Thats gota look crappy whrn you run it. for your debug statments put a /n in it, so it looks like this, "hello world /n" The /n makes a newline. you could make an Array of [x][y] x words, y are the characters in the word. so [1][0] is H, [1][1] is E, [1][2] is L, ..., [1][4] is O to make HELLO . And for finding errors in your code its good to space things out a bit, and comment will always help other people looking at your code. Comments wont usually help you, but it helps others ;) |
|||||||
| #18 04:58pm 30/08/01 |
|
|||||||
|
klayman
Posts: 608
Location: Brisbane, Queensland
|
i thought khel was some c god, or does he just like to pretend he knows it? :) |
|||||||
| #19 05:05pm 30/08/01 |
|
|||||||
|
Unleaded
Posts: 166
Location: Brisbane, Queensland
|
is this qgl ? or UQgl ? LOL! |
|||||||
| #20 05:07pm 30/08/01 |
|
|||||||
|
Lash
Posts: 131
Location: Brisbane, Queensland
|
If you want an array like you frist stated, you'd need to have array with a series of char points to strings...like char *wordArray[no_of_words]; and then for each entry in the array you'd have to malloc the area required for the string and free it once you're done. wordArray[wordnumber] = malloc(string_length, sizeof(char); Malloc will give you a pointer to some free memory space. You can then set the string using strcpy(). You have to then derefence the pointer to the string with free. This is overly complicated. Or, do what PsykoNit said and have a multidimensional array, so char wordArray[no_of_words][max_length_of_words+1]; (the max length of word has to plus one as the string has to be null terminated) and then you can copy a string into an array using strcpy - strcpy('Some string here', wordarray[word_number]); This makes sense as a string can be easily considered an array of chars. All that code succeeds in doing is really copying a string to an array of strings. This all seems really pointless though with what it looks like you want which seems to be to step through and seach for a certain string that might be contained in a series of strings and return the first entry that it was found in. Here's a hint - check the string libraries. Or to do it yourself, you want to find a string in a string like for (int myPos = 0; (myPos + string1Length) <= string2Length; myPos++) { count = 0; while ((*(string1 + count) == *(string2 + myPos + count)) && (count != string1length)) count++; if (count == string1length) return 1; } return 0; Is COMP2301 the old CS229 subject taken by Schulzy? I did that subjects a few years back, but I think it's changed a bit since then. edit: And, if you can't do string1 == string2 either if that's what you want to do... You'd need to use strcmp (I think that's it from memory - string compare) :) |
|||||||
| #21 05:33pm 30/08/01 |
|
|||||||
|
trog
Posts: 3946
Location: Brisbane, Queensland
|
lash, you're making that up |
|||||||
| #22 05:25pm 30/08/01 |
|
|||||||
|
Toll Booth Willy
Posts: 1031
Location: Brisbane, Queensland
|
you dont need pointers if you dun wanna. Sounds like they havnt learnt about pointer yet, have you? |
|||||||
| #23 05:52pm 30/08/01 |
|
|||||||
|
Goa`uld!
Posts: 519
Location: Brisbane, Queensland
|
ok I'm doing this anymore. I'm doing a different assignment. It's a simple search utility where I can use strstr() without having to go through the darn array one character at a time like this. |
|||||||
| #24 06:23pm 30/08/01 |
|
|||||||
|
Goa`uld!
Posts: 520
Location: Brisbane, Queensland
|
I haven't learnt anything. Simple fact is, I don't know c. Yet they make me do assignments in it... bastards :( |
|||||||
| #25 06:25pm 30/08/01 |
|
|||||||
|
Hyper
Posts: 5
Location: Australian Capital Territory
|
arrr.. a few questions here.. "and I need to read a list of words into an array.. and each word has to have it's own index, so I can go like if (wordArray[2] == secondWord) etc..." Right. Anything else? About now I'm feeling kinda sad. In 3rd year uni you'll learn all sorts of neat stuff to do things like this damn quick.. and then forget them withing 6 months. There are built in C++ handlers for creation of arrays etc. Sorry can't remember their names/calls.. it's been 2 years. But: (to clarify): Are you comparing only 2 strings.. or more? Do you need to compare the words in a single string against each other? ie: I have string 1.. does it exist in string 2? Or: Does this word "blerg" exist in the string? Wondering.. |
|||||||
| #26 06:29pm 30/08/01 |
|
|||||||
|
Hyper
Posts: 6
Location: Australian Capital Territory
|
Ok. Does this help? http://www.cse.fau.edu/~cot3002l/stringsC++/stringsC++.html Nice overview of c++: http://www.cee.hw.ac.uk/~pjbk/pathways/cpp1/cpp1.html What's really nice of some people is when they dump their year's worth of code on a web page somewhere and forget it. Google is such a nice search engine, isn' tit? The funny thing is.. no matter what your assignment is.. it's pretty much guarenteed that someone's done it before. (with a 50/50 chance of it being on the web somewhere). So.. want to learn something.. or just pass that damn subject? :) |
|||||||
| #27 06:37pm 30/08/01 |
|
|||||||
|
Jim
Posts: 1379
Location: Brisbane, Queensland
|
No you can't have an array of type string in C - there is no type string in C. But you can effectively compare two 'strings' using pointers to two arrays of chars to see if they're the same. Just keep moving *'s and &'s around everywhere until it sort of works, and then wait until Nats comes along to fix it for you. And definitely don't format it the gay way Lash suggested, with those faget trog-like variableNamesLikeThisHowHomoIsIt - this is really important, especially for your assignment marks. If your lecturer sees gayTrogFormating he'll go 'haha, this guy fails without me even checking the rest cos this code has gayTrogFormat code' |
|||||||
| #28 07:07pm 30/08/01 |
|
|||||||
|
Toll Booth Willy
Posts: 1033
Location: Brisbane, Queensland
|
I dont see whats wrong with the double array. cause it dosnt use pointers so it no too complex. A simple double loop is all it takes to search it? |
|||||||
| #29 08:34pm 30/08/01 |
|
|||||||
|
Nathan
Posts: 1020
Location: Brisbane, Queensland
|
be sure to comment out all the gayTrogMiniVote() code |
|||||||
| #30 08:56pm 30/08/01 |
|
|||||||
|
arcx33
Posts: 21
Location: Brisbane, Queensland
|
I think you will find that the comp2301 newsgroup could be very useful for your inquiry. Anyway, in C an array of chars is a string as long as the last char in the array is a 0. I dont mean a '0' (character zero), i mean a null 0, as in str[5] = 0; you might notice that *argv[] (or **argv) is an array of C strings. - Ryan Krumins aka arcx33 |
|||||||
| #31 09:05pm 30/08/01 |
|
|||||||
|
plok
Posts: 85
Location: Brisbane, Queensland
|
1st) Get a firm understanding of pointers and how they work. 2nd) With the help of (1), get a firm understanding of C strings, which are simply a null-terminated array of chars. 3rd) Look at the standard C libraries. In your case, strtok spings to mind as being potentially very helpful. 4th) Just when you are on the cusp of an epiphany of enlightenment, fire up doom for an hour then start again at step 1. |
|||||||
| #32 12:20pm 31/08/01 |
|
|||||||
|
Decane
Posts: 116
Location: Brisbane, Queensland
|
come to qgl forums, chat to people, make new friends, learn c++ by reading other peoples problems! |
|||||||
| #33 01:02pm 31/08/01 |
|
|||||||
|
Jim
Posts: 1389
Location: Brisbane, Queensland
|
man strtok [snip]
[/snip] |
|||||||
| #34 01:48pm 31/08/01 |
|
|||||||
|
system
|
--
|
|||||||
| #34 |
|
|||||||
|
| ||||||||