0

I looked at other answers, but was not able to find an answer.
I want to compare the last character of a string to the literal "%".

  strcpy(ch, fName[strlen(fName) - 1]);
  printf("%d\n", strcmp(ch, "%"));

I want to compile with the cl command line compiler (MicroSoft), and get warnings:

cbx_test1.c(43) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char'
cbx_test1.c(43) : warning C4024: 'strcpy' : different types for formal and actual parameter 2

There is something wrong with my code, that's obvious. But what?

2
  • 2
    What is ch and fName? How are those variables declared? Commented Mar 31, 2015 at 15:57
  • Don't use strcmp() to compare a single character. Use ch == fname[i] instead Commented Mar 31, 2015 at 16:06

4 Answers 4

7

The issue in your case is, fName[strlen(fName) - 1] is of type char, not a const char *, as needed by strcpy(). Also, we don't know about the type of ch used here.

Considering fName is an array of type char, what you want here is

if ('%' == fName[strlen(fName)-1])
   //do something
Sign up to request clarification or add additional context in comments.

3 Comments

@Abhi even though your edit got rejected, it was a valid one. Thanks, I've made the changes. :-)
I am curious - why did you reject it then?
@Abhi If you see properly, it got rejected by community, not by me. :-)
3

First you need to consider that strings in c, are sequences of non-nul bytes followed by a nul byte, you are trying to copy a character with strcpy() which is meant to copy strings, so that's why your compiler is complaining.

You can do that by simple assignment, i.e.

char   ch;
size_t length;

length = strlen(fname);
ch     = fname[length - 1];

and then you can compare ch with the '%' character constant like this

printf("%d\n", (ch == '%'));

note the single quotes, what you wanted to do is possible though not necessary, like this

char   ch[2];
size_t length;

length = strlen(fname);

strcpy(ch, &fname[length - 1]);
printf("%d\n", strcmp(ch, "%"));

notice that two characters where allocated for the "%" to become a string since it requires the terminating nul byte, like %\0, you don't need to explicitly specify it when you use a string literal like "%" since it's implied.

You could also use memcmp() which is also not necessary in this case, but I think it's interesting to mention that strcmp() is not the only way to compare two strings in c, you could1

char   ch[1];
size_t length;

length = strlen(fname);
ch[0]  = fname[length - 1];

printf("%d\n", memcmp(ch, fname + length - 1, 1));

notice that in this case the terminating '\0' is not required because we are instructing memcmpt() to compare just 1 byte.


1Note that fname + length - 1 is equivalent to &fname[length - 1]

1 Comment

char ch; size_t length; length = strlen(fname); ch = fname[length - 1]; and then you can compare ch with the '%' character constant like this printf("%d\n", (ch == '%')); This is a very useful answer!
1

If fName[] , is a character array, then your code

strcpy(ch, fName[strlen(fName) - 1]);

is wrong, because, strcpy() expects a character array ( or in other words, a const char * ) as a parameter, not a single character.

If you just want to check a single character , then an if statement is more than enough, something like

if ( fName[strlen(fName) - 1] == '%' )

Comments

1

The problem is in both lines.

strcpy(ch, fName[strlen(fName) - 1]);

strcpy wants both params to be of char *. Buth both arguements are ch not the char *. Replace this statement with

ch = fName[strlen(fName) - 1];

printf("%d\n", strcmp(ch, "%"));

I think in this line you wanted to see the result of strcmp to be 0, which validates that ch equals to '%'. Following should work for you

if(fName[strlen(fName) - 1] == '%')
//do something

OR using the code for first statement

if (ch == '%')
//Do something

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.