Thread: Char Arrays + Functions + For

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because buf is of type char[][] and word is of type char[].
    So when you do buf[i], it becomes char[] and word[i] becomes char.
    char[] != char.
    Types mismatch.
    It's like trying to assign a character to a string... but where in the string do you want to put that character?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lautarox View Post
    Yes, sure, but why I'm having that error? why can't I assign that value to the buffer var? that will just help me a lot
    Oh no it won't. That's why I wanted to know what you are trying to do, because I think what you have done so far is pretty much beyond repair for any purpose. As laserlight, etc, have been trying to point out, this is because you are using a 2D array as if it were a 1D array, which implies you don't understand the difference.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #18
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Well.. but there I'm calling the first array of the 2d array, how can this assignment be done?
    Yes, I understand the difference, that's why I was calling the first dimension of the array to be assigned

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A simple drawing to show what you are doing:
    buf looks like:
    [ ] [ ] [ ] [ ] [ ]
    [ ] [ ] [ ] [ ] [ ]
    [ ] [ ] [ ] [ ] [ ]
    [ ] [ ] [ ] [ ] [ ]
    [ ] [ ] [ ] [ ] [ ]

    And word looks like:
    [ ] [ ] [ ] [ ] [ ]

    So now you choose a row in the buf, and you will get this:
    [ ] [ ] [ ] [ ] [ ]

    And then you choose an element in word, and you simply get one element:
    [ ]

    But where among the elements above do you wish to assign it? The compiler does not know.
    buf[n]: [ ] [ ] [ ] [ ] [ ]

    ^
    |
    |

    word[n]: [ ]

    ?

    This is why it isn't working.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lautarox
    Well.. but there I'm calling the first array of the 2d array, how can this assignment be done?
    What do you mean by "I'm calling the first array of the 2d array"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I understood it, I confused with the 2d array, thanks

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I've improved my code, but I cant count the letters correctly..
    This is my output
    Code:
    tolook: daxear word: dear
    Lenght: 4
    Cantidad de buff[0] = 100
    Cantidad de buff[1] = 101
    Cantidad de buff[2] = 97
    Cantidad de buff[3] = 114
    Sec Lenght 6
    Se econtro d en la pos: 0 de tolook
    Cantidad de buff[0] en tolook 2
    Se econtro e en la pos: 1 de tolook
    Cantidad de buff[1] en tolook 2
    Se econtro a en la pos: 2 de tolook
    Se econtro a en la pos: 2 de tolook
    Cantidad de buff[2] en tolook 2
    Se econtro r en la pos: 3 de tolook
    Cantidad de buff[3] en tolook 134514138
    Char: d Cantidad: 134523305
    Sec Char: d Cantidad: 2
    Char: e Cantidad: -1076587899
    Sec Char: e Cantidad: 2
    Char: a Cantidad: -1209212939
    Sec Char: a Cantidad: 2
    Char: r Cantidad: -1208005407
    Sec Char: r Cantidad: 134514138
    No se puede
    My code
    Code:
    int sameWord(char *tolook, char *word){
    	printf("tolook: %s word: %s\n", tolook, word);
    	char lenght, seclenght, i,j;
    	char buff[7];
    	int cant[6];
    	int seccant[6];
    	char secbuff[7];
    	lenght = strlen(word);
    	printf("Lenght: %i\n", lenght);
    	for(i=0;i<lenght;i++){
    		buff[i] = word[i];
    		for(j=0;j<lenght;j++){
    			if(buff[i] == word[j]){
    				cant[i]++;
    			}
    		}
    		printf("Cantidad de buff[%i] = %i\n", i, buff[i]);
    	}
    	seclenght = strlen(tolook);
    	printf("Sec Lenght %i\n", seclenght);
    	for(i=0;i<lenght;i++){
    		for(j=0;j<seclenght;j++){
    			if(buff[i] == tolook[j]){
    				printf("Se econtro %c en la pos: %i de tolook\n", buff[i], i);
    				seccant[i]++;
    			}
    		}
    		printf("Cantidad de buff[%i] en tolook %i\n", i, seccant[i]);
    	}
    	j=0;
    	int l;
    	for(i=0;i<lenght;i++){
    		printf("Char: %c Cantidad: %i\n", buff[i], cant[i]);
    		printf("Sec Char: %c Cantidad: %i\n", buff[i], seccant[i]);
    		if(cant[i] <= seccant[i]) {
    			j++;
    		}
    	}
    	if(j==lenght) {
    		return 1;
    	}
    	else {
    		return 0;
    	}
    }

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Nice to see you are using printf to do some debugging!

    (edit: sorry, now I was misreading you)



    Anyway, I will keep reading thru it...
    Last edited by MK27; 12-08-2008 at 01:47 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    I presume you mean to move a pointer forward. That would be cant++...
    No can do. "cant" is an array, not a pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Just for you to have a better look:
    Cantidad de buff[0] -> quantity of buff[0]
    Se econtro d en la pos: 0 -> d was found in the 0 position
    Cantidad de buff[3] en tolook -> quantity of buff[] in tolook

  12. #27
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sorry, I had to pause for food. But now let's look at your first for loop:

    Code:
    for(i=0;i<lenght;i++){
    		buff[i] = word[i];
    So we are copying word into buff one letter at a time...

    Code:
    		for(j=0;j<lenght;j++){
    			if(buff[i] == word[j]){
                                        cant[i]++;
    			}
    Now, we check to see if this letter is in word, and if so add one to cant[i]. Hmmm...

    • you didn't initialize cant to anything, use int cant[6]={0} to make the whole array 0.
    • since you copied from word to start with, this will always be true, so why not skip this inner loop and just say cant[i]++ ?


    So the result of this is that word is copied into buff, and each element of cant is incremented by one. Let's move on to the second loop (skipping the printfs):
    Code:
    seclenght = strlen(tolook);
    for(i=0;i<lenght;i++){
    		for(j=0;j<seclenght;j++){
    			if(buff[i] == tolook[j]){
    				seccant[i]++;
    			}
    		}
    	}
    Now we iterate through buff, checking each letter to see if it's in tolook. If so, the element of seccant (which is undefined again) corresponding to that letter is incremented by one.
    Next:
    Code:
            j=0;
    	int l;         // what is this for and why is it here?
    	for(i=0;i<lenght;i++){
    			if(cant[i] <= seccant[i]) {
    			j++;
    		}
    	}
    First, j is reset to use as a counter in the loop, where we compare cant to seccant. Since (if the two arrays had been initialized to zero, that is) cant={1,1,1,1,1,1} and seccant's elements will be either zero or one, j will equal the number of letters from word that found a match in seccant. Then:
    Code:
    if(j==lenght) {
    		return 1;
    	}
    	else {
    		return 0;
    	}
    Wow, lottarocks, this might actually work if you had initialized your arrays to zero! But there is a lot of unnecessary stuff going on. How about this:
    Code:
    int cant[6]={0}, seccant[6]={0}, matches=0;
    for(i=0;i<lenght;i++){                    // matches is new to replace j for counting 
    		buff[i] = word[i];
                    cant[i]++;
    }
    seclenght = strlen(tolook);
    for(i=0;i<lenght;i++){
    		for(j=0;j<seclenght;j++){
    			if(buff[i] == tolook[j]){
    				matches++;
    			}
    		}
    	}
    if(matches==lenght) {
    		return 1;
    	}
    	else {
    		return 0;
    	}
    If you look at this a little bit you should be able to see how to streamline things even further.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #28
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    If I actually do this
    Code:
    for(i=0;i<lenght;i++){ 
    		buff[i] = word[i];
                    cant[i]++;
    It won't count if there are two exact letters, that's why I'm doing a for and counting how many times it appears, I've initialized the arrays with 0 and I'm still having the same problem, look
    tolook: daxear word: dear
    Lenght: 4
    Cantidad de buff[0] = 100 -> Why am I getting this high numers?
    Cantidad de buff[1] = 101
    Cantidad de buff[2] = 97
    Cantidad de buff[3] = 114
    Sec Lenght 6
    Se econtro d en la pos: 0 de tolook
    Cantidad de buff[0] en tolook 1
    Se econtro e en la pos: 1 de tolook
    Cantidad de buff[1] en tolook 1
    Se econtro a en la pos: 2 de tolook <- Why is it doing it twice? and in the same position of tolook
    Se econtro a en la pos: 2 de tolook
    Cantidad de buff[2] en tolook 2
    Se econtro r en la pos: 3 de tolook
    Cantidad de buff[3] en tolook 1
    Char: d Cantidad: 1
    Sec Char: d Cantidad: 1
    Char: e Cantidad: 1
    Sec Char: e Cantidad: 1
    Char: a Cantidad: 1
    Sec Char: a Cantidad: 2
    Char: r Cantidad: 1
    Sec Char: r Cantidad: 1
    Se Puede

  14. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lautarox View Post
    Cantidad de buff[0] = 100 -> Why am I getting this high numers?
    Cantidad de buff[1] = 101
    Cantidad de buff[2] = 97
    Cantidad de buff[3] = 114
    Hey great, these are actually letters:
    100=d
    101=e
    97=a
    114=r

    google "codigos ascii" (or ascii table) and you will quickly figure it out. It's very important to understand with C and probably all computer languages.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #30
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    lol I confused, I changed buff[i] for cant[i], now I realize..
    But I can't fix the word counting &#172;&#172;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 07:05 AM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM