Reputation:
I'm trying to write a program that gets two pointers , and a count of bytes, and compares them . for example , if we have p1 and p2 , and our count is 2 , then we'll check the 2 bytes p1[0]+p1[1] and p2[0]+p1[1].
mem_cmp :
#include <stdio.h>
int memcmp(const void *p1, const void *p2, int count)
int main()
{
char p1[5];
char p2[5];
int lexValue;
memcpy(p1, "01Aa", 5);
memcpy(p2, "01aA", 5);
lexValue = memcmp( p1, p2, 5 );
if( lexValue == 0 )
printf( "Both are equal" );
else if( lexValue < 0 )
printf( "p2 is bigger than p1" );
else
printf( "p1 is bigger than p2" );
return 0;
}
memcmp:
int memcmp(const void *p1, const void *p2, int count)
{
char *p1Char;
char *p2Char;
if ( count == 0 )
return 0;
p1Char = (char *)p1;
p2Char = (char *)p2;
if( *p1Char == *p2Char )
return memcmp( p1Char+1, p2Char+1, count-1 );
else
{
if( *p1Char > *p2Char )
return 1;
else
return -1;
}
}
when I try to compile I get this :
gcc -g -ansi -Wall -pedantic memcmp.c -o memcmp.o
memcmp.c: In function ‘memcmp’:
memcmp.c:11:7: warning: statement with no effect [-Wunused-value]
memcmp.c:19:1: warning: control reaches end of non-void function [-Wreturn-type]
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [memcmp.o] Error 1
EDIT: this is the makefile:
memcmp : mem_cmp.o memcmp.o
gcc -g -ansi -Wall -pedantic mem_cmp.o memcmp.o -o memcmp
mem_cmp.o: mem_cmp.c
gcc -c -ansi -Wall -pedantic mem_cmp.c -o mem_cmp.o
memcmp.o: memcmp.c
gcc -c -ansi -Wall -pedantic memcmp.c -o memcmp.o
and now this is the error log I see:
gcc -g -ansi -Wall -pedantic mem_cmp.o memcmp.o -o memcmp
mem_cmp.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.fini+0x0): first defined here
mem_cmp.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.data+0x0): first defined here
mem_cmp.o: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i686-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
mem_cmp.o:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
mem_cmp.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.text+0x0): first defined here
mem_cmp.o:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata+0x0): first defined here
mem_cmp.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i686-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
mem_cmp.o:(.dtors+0x4): first defined here
/usr/bin/ld: error in mem_cmp.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
make: *** [memcmp] Error 1
EDIT 2:
no I get only tose warnings :
mem_cmp.c:11:2: warning: implicit declaration of function ‘memcpy’ [-Wimplicit-function-declaration]
mem_cmp.c:11:2: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
gcc -g -ansi -Wall -pedantic mem_cmp.o memcmp.o -o memcmp
Upvotes: 0
Views: 1021
Reputation: 121387
<string.h>
contains the standard function memcmp()
. So its prototype conflicts with your version of memcmp()
.
Also, your function prototype is missing a semi-colin ;
.
Rename your function and add semi-colon:
int my_memcmp(const void *p1, const void *p2, int count);
and
int my_memcmp(const void *p1, const void *p2, int count) {
...
...
}
When you declare:
char* p1Char, p2Char;
only p1Char
is a pointer; p2Char
is just a char. You want:
char* p1Char, *p2Char;
Also, note you need <string.h>
since you are using memcpy()
.
Upvotes: 1