Hai everyone,
I have one doubt... Can u please clarify this...
If i put
char a[10]="hai-hello"

if(strcmp(a[4],'-')==0)


then it will displays an error
strcmp' : cannot convert parameter 1 from 'char' to 'const char *'


Wat is the reason for this?
Can anybody tell me

Dani AI

Generated

A quick clarification: the compiler error happens because a single character value was passed where a C string pointer is required. and pointed this out — the correct, simplest fix for a single-character check is to compare the array element directly. Below are a few safe, practical alternatives depending on the intent.

For a single-character test use a direct comparison (bounds-check the index):

char s[] = "abc-def";
size_t i = 3;
if (s[i] == '-') { /* hyphen at i */ }

To find a character anywhere in the string, use the standard search function:

if (strchr(s, '-') != NULL) { /* hyphen found */ }

To compare a substring starting at some index with a literal, pass the address of that element (ensure the remainder is NUL-terminated):

if (strcmp(&s[i], "def") == 0) { /* matches "def" at i */ }

Notes and cautions: always verify the index is within the string length and that any pointer passed to string functions points to a NUL-terminated region; otherwise behaviour is undefined. Creating a tiny temporary two-byte buffer is another way to treat a char as a string, but direct char comparison is clearer and faster. See the standard library docs for details on strcmp and strchr.

Recommended Answers

All 3 Replies

just look at the suntax of stcmp() and u will be able to understand.
int strcmp ( const char * str1, const char * str2 );

it take address of the char, not the char.

the function is strcmp. you are comparing a character and not a string.

You only need to go:

if( a[4] == '-') because the == operator can compare characters.
also in the string:
char a[10]="hai-hello"
it will return false because the - is at position a[3] not position a[4].

If you are using strcmp you can compare strings (notice double quotes).

K thanks for ur suggestion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.