简体   繁体   中英

comparing and defining strings in C

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?

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

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, ie

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

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.


1 Note that fname + length - 1 is equivalent to &fname[length - 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] == '%' )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM