1. 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. 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.

3. 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";`

4. 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. 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 */```

6. Wow! That was a bucket load of help! I'll try it and report back! Thanks!

7. 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.

8. Originally Posted by Elysia
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.

9. And so would &temp[i].
Note that this would give the string starting off from the position i, and not a single character.

10. 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. 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.

12. >> 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. Originally Posted by Elysia
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.

14. It works perfectly! Thanks!

15. Originally Posted by MK27
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?