why is the output of the below program is coming out as 'unequal' ??can anyone please explain??

include<stdio.h>

void func(char *);

void main()
{
char *q;
char *p;
p="hello hii";
func(q);
if(p==q)
printf("equal");
else printf("unequal");
}
void func(char *pi)
{
pi="hello hii";
}

Recommended Answers

All 2 Replies

You have also problem in your formatting and/or indenting. Did you read the Formatting Help ?

Some compilers will co-locate string literals that are "equal" at the same address. However, this is not required, and a lot of compilers do not do that. So, the comparison that you are doing is comparing two pointers (addresses) that are not necessarily equal, and are not in your situation, so the comparison is false (unequal). Use the strcmp(const char* str1, const char* str2) function instead, as in

if (strcmp(p,q) == 0)
{
    printf("equal");
}
else
{
    printf("unequal");
}
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.