string comparison

J

junky_fellow

Consider the following piece of code:
char *str = "Hello";
if (str = "Hello")
printf("\nstring matches\n");

str is pointer to char and "Hello" is a string literal whose
type is "array of char".

How can we compare two different objects for equality ?
Is some conversion is being done before that comparison ?
If yes, which conversion rule from C standard is applied here ?
 
S

Sensei

Consider the following piece of code:
char *str = "Hello";
if (str = "Hello")
printf("\nstring matches\n");

str is pointer to char and "Hello" is a string literal whose
type is "array of char".

How can we compare two different objects for equality ?
Is some conversion is being done before that comparison ?
If yes, which conversion rule from C standard is applied here ?


strcmp


Check the string.h header file for all the functions.
 
K

Krishanu Debnath

Consider the following piece of code:
char *str = "Hello";
if (str = "Hello")
printf("\nstring matches\n");

str is pointer to char and "Hello" is a string literal whose
type is "array of char".

How can we compare two different objects for equality ?
Is some conversion is being done before that comparison ?
If yes, which conversion rule from C standard is applied here ?

'=' is assignment operator.

Krishanu
 
J

junky_fellow

Sensei said:
strcmp


Check the string.h header file for all the functions.

--
Sensei <[email protected]>

cd /pub
more beer

I know that there's a C library function "strcmp" for string
comparison. What I wanted to ask is that the comparison
if (str == "Hello") doesn't give any compile time error.
Nor does it give any Warning. That means some conversion
should have been done because we cannot compare two objects
of different types.
 
S

Stephane Zuckerman

I know that there's a C library function "strcmp" for string
comparison. What I wanted to ask is that the comparison
if (str == "Hello") doesn't give any compile time error.
Nor does it give any Warning. That means some conversion
should have been done because we cannot compare two objects
of different types.

When you write
if (a_var == "a string") { /* ... */ }
you're really writing "is the value of a_var equals to the value of the
address that points to the constant string 'a string' ?" So, yes, there is
a conversion, but maybe not the one you thought...
 
K

Krishanu Debnath

I know that there's a C library function "strcmp" for string
comparison. What I wanted to ask is that the comparison
if (str == "Hello") doesn't give any compile time error.
Nor does it give any Warning. That means some conversion
should have been done because we cannot compare two objects
of different types.

Okay, now you have realized that you are talking about equality
operator. Yes, "Hello" has type 'array 5 of char'. But in value
context it decays to pointer to the first element of the array
which has type 'pointer to char'.

Krishanu
 
J

junky_fellow

Krishanu said:
Okay, now you have realized that you are talking about equality
operator. Yes, "Hello" has type 'array 5 of char'. But in value
context it decays to pointer to the first element of the array
which has type 'pointer to char'.

Krishanu

Can you please specify which section of C standard specify this
conversion ?
 
K

kernelxu

there may not be any warnnings or errors ,even though you had used
"(str == "Hello") ", they just campare their address value ,not the
strings.

if you use (str = "Hello") , the judge expression is always true .
"printf("\nstring matches\n");" will be excuted .
 
C

Chen Ming

u should use *lint* to check your program.

I know that there's a C library function "strcmp" for string
comparison. What I wanted to ask is that the comparison
if (str == "Hello") doesn't give any compile time error.
Nor does it give any Warning. That means some conversion
should have been done because we cannot compare two objects
of different types.
 
P

pete

What I wanted to ask is that the comparison
if (str == "Hello") doesn't give any compile time error.

After you get that settled, you need to know that
("Hello" == "Hello")
isn't guaranteed true.
Identical string literals may either refer to the same object
or to different objects.
 
B

bwaichu

You are confused.

char *string; /* string is a pointer */

char *string = "hello"; /* string is a pointer that points to 'h' */

char *string; /* declare the pointer */
string = malloc(5); /* sets up memory to point to*/

string = "hello"; /* assigns "hello" to memory pointed to */

Here's a small C program:

#include <stdio.h>
#include <stdlib.h>

int
main() {

char *string;
string = malloc(5);

string = "hello\n";

(void)printf("%s\n", string);

exit(0);
}

Here's it disassembled:

..section .rodata # read only data

..LC0:
.string "hello\n"

LC1:
.string "%s\n"

..section .text #program starts here

..global main

main:
pushq %rbp # set up the stack
movq %rsp, %rbp
subq $16, %rsp # allocate space on the stack for
# 16 bytes
movl $5, %edi # pass the size to malloc
call malloc
movq %rax, -8(%rbp) # %rax is the string variable
movq $.LC0, -8(%rbp) # this is the memory assign
movq -8(%rbp), %rsi # "hello\n"
movl $.LC1, %edi # "%s\n"
movl $0, %eax # NOP padding
call printf
movl $0, %edi # exit(0)
call exit

So now you want to compare two strings. If you look at libc strcmp:

int
strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(unsigned char *)s1 - *(unsigned char *)--s2);
}

What is being looked at are the individual values of each element of
string. Since 'A' is really 0x41, when checking for comparison, you
are really comparing two values and doing simple subtraction.

C hides this from you. The assembly would use cmps or else you could
just manually do the compare.

Brian
 
B

Barry Schwarz

Consider the following piece of code:
char *str = "Hello";
if (str = "Hello")
printf("\nstring matches\n");

str is pointer to char and "Hello" is a string literal whose
type is "array of char".

How can we compare two different objects for equality ?
Is some conversion is being done before that comparison ?
If yes, which conversion rule from C standard is applied here ?

After fixing the = to == in the if, the answer to your question is you
don't know if they are different objects. The compiler is allowed to
reuse string literals. It is entirely possible that str points to the
same string that is used in the if. It is also possible that str
points to a different string, even though the two strings contain same
six characters.

Just for fun, you might try
if ("Hello" == "Hello") ...

The same implementation defined behavior allows the expression to
evaluate to 1 or 0 as a result of how the compiler implements
duplicate string literals.


<<Remove the del for email>>
 
K

Keith Thompson

char *string; /* string is a pointer */

char *string = "hello"; /* string is a pointer that points to 'h' */

char *string; /* declare the pointer */
string = malloc(5); /* sets up memory to point to*/

You've allocated 5 bytes (assuming the malloc succeeds), which isn't
enough room for "hello"; you need 6 bytes to allow for the trailing
'\0'.
string = "hello"; /* assigns "hello" to memory pointed to */

No, that's a pointer assignment. The string literal refers to a
statically allocated block of 6 bytes with the value "hello\0". The
assignment causes string to point to that block. If this follows the
malloc above, you've just created a memory leak.
 
B

bwaichu

C strings have trailing zeros, but those are only important if you wish
to count out the length of the string or perform other string functions
after the assignment. You do not need to have the trailing zero.

My example would have been better if I used write() instead of
printf(). The trailing zero is just a C concept. The only purpose the
zero serves is to tell you where the end of the string is.

I can code a string that starts with it's size and does not terminate
with a zero. I think Pascal does strings that way.

The allocation of memory occurs either in .bss or in the heap.
The address of the string sits on the stack. You can write strings to
the stack in C, but I cannot imagine why you would want to do that.

What I do not understand is why you think I created a memory leak.
Unless I use another function that will search for the terminating
zero, I do not see the problem. For example, strcmp will search for
the terminating zero.

Brian
 
K

Keith Thompson

C strings have trailing zeros, but those are only important if you wish
to count out the length of the string or perform other string functions
after the assignment. You do not need to have the trailing zero.

They're only important if you want to use C strings, which are the
most common use of character arrays in C.
My example would have been better if I used write() instead of
printf(). The trailing zero is just a C concept. The only purpose the
zero serves is to tell you where the end of the string is.

C concepts are what we discuss here.

[snip]
What I do not understand is why you think I created a memory leak.
Unless I use another function that will search for the terminating
zero, I do not see the problem. For example, strcmp will search for
the terminating zero.

Please provide some context; don't assume that everyone can see the
article to which you're replying. I'm sick and tired of explaining
how to do this, so just search for "google broken reply link" in this
newsgroup.

Here's a fragment of the code you posted earlier:

char *string;
string = malloc(5);

string = "hello\n";

The first statement allocates a block of 5 bytes (assuming the
malloc() call succeeds) and causes string to point to the beginning of
the newly allocated block.

The second statement causes string to point to the beginning of the
string literal "hello\n" (which occupies 7 bytes). You've now lost
your only pointer to the memory allocated by malloc(), and you have no
way to release it (there's nothing you can pass to free()). That's a
memory leak.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top