Char Arrays + Functions + For

This is a discussion on Char Arrays + Functions + For within the C Programming forums, part of the General Programming Boards category; I didn't wanted to create another post, for this, I think it has to be a little problem.. Code: char ...

  1. #1
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193

    Char Arrays + Functions + For

    I didn't wanted to create another post, for this, I think it has to be a little problem..
    Code:
    char sameWord(char *tolook, char *word){
    	char lenght, seclenght, i,j;
    	char buff[7][7];
    	char secbuff[7][7];
    	lenght = strlen(word);
    	for(i=0;i<lenght;i++){
    		buff[i] = word[i]; -> First Error (line 30)
    		for(j=0;j<lenght;j++){
    			if(word[j] == word[i]){
    				buff[i][i]++;
    			}
    		}
    	}
    	seclenght = strlen(tolook);
    	for(i=0;i<lenght;i++){
    		secbuff[i] = tolook[i]; -> Second error (line 39)
    		for(j=0;j<lenght;j++){
    			if(tolook[j] == tolook[i]){
    				secbuff[i][i]++;
    			}
    		}
    	}
    	j=0;
    	for(i=0;i<lenght;i++){
    		if(buff[i][i] <= secbuff[i][i]) {
    			j++;
    		}
    	}
    	if(j==lenght) {
    		return 1;
    	}
    	else {
    		return 0;
    	}
    }
    I'm getting this error
    keyfunc.c:30: error: incompatible types in the allocation
    keyfunc.c:39: error: incompatible types in the allocation
    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Hint: word[i] is a char but buff[i] is not.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    But.. I'm passing the pointer with a supposed word, I store the value of each letter in the supposed buffer and the times it appears, if the two of them are characters why can't be assigned?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you can't assign an array with a character, right? And that's what your "line 30" tries to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    But I'm assigning it to the first value and so on.., are you saying that my pointer is not showing the values?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		buff[i] = word[i]; -> First Error (line 30)
    What is buff[i], and what is word[i]?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by lautarox
    But I'm assigning it to the first value and so on..
    What is "it", and what "first value" are you talking about?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I ment, that the for fills the buff array with the values from the word pointer, that is a char array also

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lautarox View Post
    I think it has to be a little problem..
    I think it might be more than a little problem and if you followed laserlight's hint you might be starting to think the same thing!

    Are you just trying to compare two words? If so, maybe start again...what's the purpose of buff[i][i]++? This will just change a letter to the next letter, so that if buff[i][i]='b', buff[i][i] will now be c. But at that point in the program flow, buff[i][i] was bound to be undefined anyway, so you will be taking a mystery value and adding 1 to it.

    Also: the return type should be int, not char.
    Last edited by MK27; 12-08-2008 at 12:35 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. #10
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Well.. the purpose of my function is to see if a word can be found in 6 random letters, for example, "esters" the function takes a word and it has a buff array that stores the letter in the buff[i] and the times it appears in the buff[i][i], then it does the same with the other one and then checks if the word can be formed with the random letters
    Last edited by lautarox; 12-08-2008 at 10:10 AM.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You still do not need a 2D char array for this (or two loops). There is something you are confused about.

    This will do more or less what you are asking and might clarify some issues for you.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int sameWord(char *one, char *two) {  	
    	int len1=strlen(one), len2=strlen(two), i,j;
    
    	for (i=0;i<len1;i++) {
    		for (j=0;j<len2;j++) {
    			if (one[i]==two[j]) break;
    			if (j==len2-1) return 0;           //end of "two", letter not found
    		}
    	}	
    	return 1;
    }
    
    int main(int argc, char *argv[]) {
    	if (argc<3) {puts("Two words needed");return -1;}
    	if (sameWord(argv[1],argv[2])==1) printf("\"&#37;s\" has the letters to spell \"%s\"\n",argv[2],argv[1]);
    	else printf("\"%s\" does not have the letters to spell \"%s\"\n",argv[2],argv[1]);
    	return 0;
    }
    Output:
    Code:
    ./a.out this xchtsi
    "xchtsi" has the letters to spell "this"
    ./a.out this xcgtso
    "xchgso" does not have the letters to spell "this"
    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

  12. #12
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    But what if the word has 2 same letters?

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lautarox View Post
    But what if the word has 2 same letters?
    So you don't want (?):
    "xem" has the letters to spell "meme"

    I suppose you could zero a letter in "two" out after it is used, so that it will never be found again (if you do that, you'll have to work with a copy of "two" or else it will be affected in main() after the function completes). But my point with the post was to demonstrate a (functioning) function with a for loop operating on char arrays, so that you could contrast this with what you were trying to do, and thereby get a better idea of what will work, vs. what you think will work, and hopefully bring the two closer together.

    If you see what I mean. What you are trying to do is fairly simple, the reason you are having a hard time is because you don't have enough grasp of the C syntax & concepts involved. Which is a good reason to keep working at it. So you could try to do what I just suggested to the code I posted before. Practice, practice, practice!
    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

  14. #14
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    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

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by lautarox
    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
    If you are talking about the errors on lines 30 and 36 as per your original code example, the reason is that you cannot assign a char to an array, yet word[i] and tolook[i] are chars, and buff[i] and secbuff[i] are arrays.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast
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, 04:31 AM
  4. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 06:05 AM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21