Function help?

This is a discussion on Function help? within the C Programming forums, part of the General Programming Boards category; I'm trying to write a function that will convert letters to numbers, but I keep getting this error: warning: passing ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    28

    Function help?

    I'm trying to write a function that will convert letters to numbers, but I keep getting this error:

    warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast
    Here's my function:

    Code:
    static void decodeCode(void) {
            char temp[20] = "baba";
    	int charlength = sizeof(temp)-1;
    	char temp2[20];
    	int i = 0;
    
    	while(i < charlength){
    		if (!strcmp(temp[i], "a")) {
    			strcat(temp2, "0");
    		}
    		if (!strcmp(temp[i], "b")) {
    			strcat(temp2, "1");
    		}
    		if (!strcmp(temp[i], "c")) {
    			strcat(temp2, "2");
    		}
    		if (!strcmp(temp[i], "d")) {
    			strcat(temp2, "3");
    		}
    		if (!strcmp(temp[i], "e")) {
    			strcat(temp2, "4");
    		}
    		if (!strcmp(temp[i], "f")) {
    			strcat(temp2, "5");
    		}
    		if (!strcmp(temp[i], "g")) {
    			strcat(temp2, "6");
    		}
    		if (!strcmp(temp[i], "h")) {
    			strcat(temp2, "7");
    		}
    		if (!strcmp(temp[i], "i")) {
    			strcat(temp2, "8");
    		}
    		if (!strcmp(temp[i], "j")) {
    			strcat(temp2, "9");
    		}
    		
    		i++;
    	}
    	
    	strcpy(temp, temp2);
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you are just comparing a single character, you should do it this way:
    Code:
    if (temp[i]=='a')
    Notice single quotes!

    Actually, in this case you probably want to use case:
    Code:
    switch (temp[i]) {
            case 'a':  strcat(temp2,"0");  break;
            case 'b':  strcat(temp2,"1");  break;
    Altho there is an even easier way, because ASCII characters have numerical values (a is 97, b is 98, etc, and 0 is 48, 1 is 49), so you could just go:
    Code:
    for (i=0; i<strlen(temp); i++) temp2[i]=temp[i]-97+48;
    temp2[i]='\0';  /* now null terminate temp2 */
    Which is the whole thing in two lines.
    Last edited by MK27; 03-16-2009 at 11:44 AM.
    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. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    One more note: don't use strcat() on an uninitialized string, because it will not be null terminated. Initialize it the string this way first:
    Code:
    char temp2[20]="\0";
    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

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    Altho there is an even easier way, because ASCII characters have numerical values (a is 97, b is 98, etc, and 0 is 48, 1 is 49), so you could just go:
    You can just add and subtract on the glyphs too...

    MK's suggestion exploits the fact that most charsets are ordered alphabetically. You'll run into problems with another (less) common charset, EBCDIC, still found on IBM dinosaurs.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    While on the topic of exploits and optimization, you are better to NOT use strlen in a for loop unless the length of the string is actually changing, since this means performing a strlen() at each iteration, witness
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
    	char string[]="string is this", two[]="string", *ptr=string;
    	int i;
    	for (i=0;i<strlen(ptr);i++) {
    		printf("%c%d",ptr[i],i);
    		ptr=two;
    	}
    }
    So in the context of that two lines from the other post, three lines is more optimal:
    Code:
    int len=strlen(temp);
    for (i=0; i<len; i++) temp2[i]=temp[i]-97+48;
    temp2[i]='\0';  /* now null terminate temp2 */
    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

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Wow! That was a bucket load of help! I'll try it and report back! Thanks!

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    The problem is that temp is an array of chars. An array will decay to a pointer to its first element when passed to a function, but since you use the index operator, you get a char from that array and char != char*.
    So there you have it.
    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.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    The problem is that temp is an array of chars. An array will decay to a pointer to its first element when passed to a function, but since you use the index operator, you get a char from that array and char != char*.
    So there you have it.
    meaning while "temp" is a valid pointer for strcmp, temp[i] is not (hence the warning about it being an integer, which it more or less is, a=97, b=98...)

    temp+i would work.
    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

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    And so would &temp[i].
    Note that this would give the string starting off from the position i, and not a single 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.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    Who cares what would work? The OP doesn't want to compare strings as a whole. strcmp is the wrong function to use here to begin with.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Yes, it is, but you don't need to get worked up about it.
    I was explaining the technical reason behind why strcmp doesn't work and I suppose MK27 just added what might work with strcmp, but what not be the right case here.
    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.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    >> Yes, it is, but you don't need to get worked up about it.
    Not every post I write is fit for a dramatic reading.

    >> ... I suppose MK27 just added what might work with strcmp, but what not be the right case here.
    And likewise not everything I post is a personal affront. I had a point, you had a point, MK has a point. Shut up about me, seriously.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    Yes, it is, but you don't need to get worked up about it.
    I was explaining the technical reason behind why strcmp doesn't work and I suppose MK27 just added what might work with strcmp, but what not be the right case here.
    Indeed, and I would be much more likely (in fact almost certain) to use &temp[i] if my brain was typing code at the time. Hmmm.
    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
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    It works perfectly! Thanks!

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Quote Originally Posted by MK27 View Post
    So in the context of that two lines from the other post, three lines is more optimal:
    Code:
    int len=strlen(temp);
    for (i=0; i<len; i++) temp2[i]=temp[i]-97+48;
    temp2[i]='\0';  /* now null terminate temp2 */
    Is there a way to do that same thing, only converting a number to a letter? 0 = a, 1 = b, and so forth?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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