You are not logged in.
Pages: 1
Find 5 errors within this c code! errors in the same row is counted as one, and ignore any logical errors in the algorithm of function find.
Give the solution to the error as well.
#include <stdio.h>
#include <stdlib.h>
struct Elem {int key; int value;};
//-performs binary search.
//- REturns -1 if key not found in array.
int find(Elem *array, int size, inte key)
{
//-start is the first element in the search space
//- stop is the last
int start = 0, stop = size -1;
int pos;
for(;;)
{
if (stop < start) return -1;
pos = start + (stop - start)/ 2;
if (array[pos]->key == key) return array[pos]->value;
//- Split the search space
if(key < array[pos].key) stop = pos -1;
else start = pos +1;
}
}
int main(int argc, char *argv[])
{
struct Elem *array[] =
{
{ .key =0, .value =10 },
{ .key =4, .value =14 },
{ .key =7, .value =17 },
{ .key =11,.value =21 }
};
int value = find(array, 4, atio(argv[1]));
printf("%s\n", value);
return 0;
}
The ultimate Archlinux release name: "I am your father"
Offline
What are the rules ? gcc detects all these errors.
pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))
Offline
int find(Elem *array, int size, inte key)
I know of no datatype called inte. Also, "Elem" should be "struct Elem"
if (array[pos]->key == key) return array[pos]->value;
array[pos]->x should be array[pos].x for all x.
struct Elem *array[]
bogus syntax - get rid of the *. -edit- I guess I should clarify that, in general, it's not bogus syntax per se, but for this specific example it is. -/edit-
int value = find(array, 4, atio(argv[1]));
atio is undefined.
printf("%s\n", value);
Should be %d, not %s
-edit- Shining makes a good point. I didn't use gcc to find these, but I suppose anyone with a bit of knowledge could use it to find 'em. Although, I think the error for the third one I posted would be a bit more vague... -/edit-
Last edited by Cerebral (2007-08-29 18:28:25)
Offline
Yeah.. the rules should be not using gcc.. just reading the code and finding out.. Sorry I thought it was obvious not to use gcc sine it would be no challenge..
Nice cerebral, about the datatype inte is just my mispell..
Last edited by jinn (2007-08-29 18:05:38)
The ultimate Archlinux release name: "I am your father"
Offline
damn.. I missed this..
segfault at atoi after fixing the code up.. Cerebral can you answer this also
The ultimate Archlinux release name: "I am your father"
Offline
Er. Pass in a parameter?
Offline
nope.. Actually I have no idea why it segfaults..
[21:16 root programming]# ./a.out 4
zsh: segmentation fault ./a.out 4
[21:17 root programming]#
The ultimate Archlinux release name: "I am your father"
Offline
then compile with -g and run it through gdb.
$ gcc test.c -g
$ gdb a.out
When it crashes, type 'bt' to get a backtrace - it'll tell you where it's crashing, and you can print out the contents of variables to figure out what's going on.
Offline
yeah, tried that.. but cant get anything useful from this..
(gdb) run 0
Starting program: /root/programming/a.out 0
Program received signal SIGSEGV, Segmentation fault.
0xb7e7d6cb in strlen () from /lib/libc.so.6
(gdb) bt
#0 0xb7e7d6cb in strlen () from /lib/libc.so.6
#1 0xb7e50795 in vfprintf () from /lib/libc.so.6
#2 0x08048590 in _IO_stdin_used ()
#3 0x00000000 in ?? ()
(gdb)
The ultimate Archlinux release name: "I am your father"
Offline
check your printf line.
Offline
damn.. I am blind.. forgot about the error that I created.. %s..
The ultimate Archlinux release name: "I am your father"
Offline
Pages: 1