(sorry, the Computer Science forum isn't active) 2D ARRAYS IN C I want to save multiple-character words into a 2d array: char xyz [10][20] is it enough for me to do it like (if my word is assigned to a variable called abc): abc=xyz[0] or do i have to break it down to characters and put them indivudually into their respective places, such as: abc[0]=xyz[0][0]; abc[1]=xyz[0][1]; ... abc[N] = xyz [0][N] ?
xyz[0] will be an array of 20 characters. abc isn't, right? so abc=xyz[0] will probably throw you a warning. or run-time error
abc is a 1D array, such as: char abc[5] i would like to input 10 words into abc, each one into one row of xyz
*into xyz
Warning that i got , when abc was a char. "initialization makes integer from pointer without a cast " Let me try with abc as char array
well, abc is a char array. actually, i want to do this with numbers but i will treat them as char arrays (e.g. char aa=501436)
your input is filled in abc one-by-one and you want to fill the 2D char array, xyz, right? or the other way round?
"save in 2D array" so shouldn't your question be xyz[0] = abc; whether that is legal or not...
and to answer your question, you can fill xyz one char at a time, by using nested for.
yes, that's what i meant
what is "nested" for? (i know what for is)
for inside a for, for(i = 0; i< 10; i++) { for(j=0; j<20; j++) { .... } }
#hartnn can i also ask about the next step in my program?
have alook, it might help http://stackoverflow.com/questions/18083984/how-to-store-and-then-print-a-2d-character-string-array
this explains any 2D array, quite nicely. http://stackoverflow.com/questions/26941470/initializing-2d-char-array-in-c
so my program will probably use a do while to generate a number (given by the user) of unique words that will be saved into my 2d array. since the words need to be different, i need to scan the 2d array to find out whether the currently generated word already is in my memory. which function should i use? imagine i will have something like this: char xyz[3][5]= { a, p, p, l, e, t, r, e, e, -, c, a, r, -, -, } and my generated word will be apple. the problem is that the characters in xyz each fill one field and the rows don't really act like compact words
(by the way, i got a one-week ban from stackowerflow for asking elementary questions so probably no help for me there)
Try saving the word (characters) you want to check if it's in memory, then have some for-loop to see if any words in the 2D array start with the first letter, then second, third etc letter in the word you're looking up.
@aleroth this would be a problem because i will work with words with unlimited length
They can't be unlimited because your 2d array is limited?
Your 2d array is given a fixed maximum number of characters, a 2d array looks like this: xyz[max number of words][max length of words]
they will be kinda limited but the 2d array will be like xyz[desired number of words][1000]
i have an idea, maybe i could use a nested loop with memcmp that would compare the current word with all the previous numbers and return 0 if the word hasn't been used yet, then the original loop would save the word into another row of the 2d array
*with all the previous words
well, is it even possible to compare something to a single row of a 2d array, for example compare char abc to the row xyz[0]?
to compare a char array abc, with one of the entries of 2D array xyz[0], (A) compare it character by character using one for loop (B) memcmp
i'll use a loop with memcmp, thanks :) by the way, @hartnn, i will continue programming straight away so can i tag you if i have any more questions?
and i guess memcmp internally does char by char comparison only... just using a pre-defined function in the library...which is always better as the standard function might check for boundary cases which we could forget to check.
sure, feel free to tag me. hope some C expert also contributes...even I am still a beginner :)
http://ee.hawaii.edu/~tep/EE160/Book/chap9/subsection2.1.3.1.html#SECTION0013100000000000000
@hartnn if i only want something to happen if a certain condition occurs, can i just put there an: if (my condition) {the action happens} and leave it like that, with no else?
yes, most definitely.
Join our real-time social learning platform and learn together with your friends!