Algaja Cee progeja lõbud

Mõtlesin et värskendan oma väheseid Cee teadmisi läbi kasuliku. Kirjutan sellise pisikese poomise mängu. Alustan siis tasapisi basic asjadest.


#include

#define INPUTTEXT "Sisesta täht nr "
#define TRIES 3

main () {
char a[10];
char b;
int count = 0;

while (TRIES > count) {
printf( INPUTTEXT "%d :\n", count );
scanf( "%s", b);
a[count] = b;
++count;
}

return 0;
}

Hetkel on siis tulemus selline:
[10:43:31 margusja@Irack:~/Documents/poomine ] $ gcc poomine.c -o poomine
[10:43:33 margusja@Irack:~/Documents/poomine ] $ ./poomine
Sisesta täht nr 0 :
a
Segmentation fault
[10:43:36 margusja@Irack:~/Documents/poomine ] $ gd
gdb gdiffmk gdk-pixbuf-csource gdk-pixbuf-query-loaders gdlib-config
[10:43:36 margusja@Irack:~/Documents/poomine ] $ gd
gdb gdiffmk gdk-pixbuf-csource gdk-pixbuf-query-loaders gdlib-config
[10:43:36 margusja@Irack:~/Documents/poomine ] $ gdb ./poomine
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “i386-apple-darwin”…Reading symbols for shared libraries … done

(gdb) run
Starting program: /Users/margusja/Documents/poomine/poomine
Reading symbols for shared libraries ++. done
Sisesta täht nr 0 :
a

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xffffffbf
0x96d97fc1 in __svfscanf_l ()
(gdb)

Nii uus katse ehk on string tüüpidega mingi jama:

#include

#define INPUTTEXT "Sisesta täht nr "
#define TRIES 3

main () {
long a[10];
int b;
int count = 0;

while (TRIES > count) {
printf( INPUTTEXT "%d :\n", count );
scanf( "%d", b);
a[count] = b;
++count;
}

[08:55:03 margusja@Irack:~/Documents/poomine ] $ gdb poomine
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “i386-apple-darwin”…Reading symbols for shared libraries … done

(gdb) run
Starting program: /Users/margusja/Documents/poomine/poomine
Reading symbols for shared libraries ++. done
Sisesta täht nr 0 :
1
Sisesta täht nr 1 :
2
Sisesta täht nr 2 :
3

Program exited normally.
(gdb)
(gdb) quit

return 0;
}

NB! kui gdb kasu ootate siis kompileerida -g võtmega.

Hetkel käitub minu programm mitte just nii nagu mina soovin. Nagu altpoolt näha on ei oodata täht nr 1’te.
[10:59:29 margusja@Irack:~/Documents/poomine ] $ ./poomine
Sisesta täht nr 0 :
a
Sisesta täht nr 1 :
Sisesta täht nr 2 :
b
Sisesta täht nr 0 – (null):
Sisesta täht nr 1 – (null):
Sisesta täht nr 2 – (null):

Siinkohal tuleb appi gdb:

[11:01:52 margusja@Irack:~/Documents/poomine ] $ gdb ./poomine
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “i386-apple-darwin”…Reading symbols for shared libraries … done

(gdb) l 10
5
6 main () {
7 char a[10];
8 char b;
9 int count = 0;
10
11 while (TRIES > count) {
12 printf( INPUTTEXT “%d :\n”, count );
13 b = getchar();
14 ++count;
(gdb)
15 }
16
17 count = 0;
18 while (TRIES > count) {
19 printf( INPUTTEXT “%d – %s:\n”, count, a[count] );
20 ++count;
21 }
22
23
24 return 0;
(gdb) b 14
Breakpoint 1 at 0x1f7d: file poomine.c, line 14.
(gdb) run
Starting program: /Users/margusja/Documents/poomine/poomine
Reading symbols for shared libraries ++. done
Sisesta täht nr 0 :
a

Breakpoint 1, main () at poomine.c:14
14 ++count;
(gdb) disp b
1: b = 97 ‘a’
(gdb) continue
Continuing.
Sisesta täht nr 1 :

Breakpoint 1, main () at poomine.c:14
14 ++count;
1: b = 10 ‘\n’
(gdb) continue
Continuing.
Sisesta täht nr 2 :
b

Breakpoint 1, main () at poomine.c:14

Nagu näha on satub nr 1 sloti \n ehk reavahetus.
14 ++count;
1: b = 98 ‘b’


Seega tuleb reavahetusest lahti saada. See on stringi sisestamises üks väga levinud probleem.

[22:43:54 margusja@Irack:~/Documents/poomine ] $ gdb ./poomine
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “i386-apple-darwin”…Reading symbols for shared libraries … done

(gdb) l
1 #include
2 #include
3
4 #define INPUTTEXT “Sisesta täht nr ”
5 #define TRIES 3
6
7 main () {
8 char a[10];
9 char b[20];
10 int count = 0;
(gdb)
11
12 while (TRIES > count) {
13 printf( INPUTTEXT “%d :\n”, count );
14 fgets (b, sizeof b, stdin );
15 char *newline = strchr(b, ‘\n’);
16 *newline = ‘\0’;
17 a[count] = b[0];
18 ++count;
19 }
20
(gdb)
21 printf(“%s:\n”,a );
22
23 return 0;
24 }
(gdb)
(gdb) b 18
Breakpoint 1 at 0x1fb5: file poomine.c, line 18.
(gdb) run
Starting program: /Users/margusja/Documents/poomine/poomine
Reading symbols for shared libraries ++. done
Sisesta täht nr 0 :
a

Breakpoint 1, main () at poomine.c:18
18 ++count;
(gdb) disp b[0]
Invalid character ‘?’ in expression.
(gdb) disp b[0]
1: b[0] = 97 ‘a’
(gdb) disp a[0]
2: a[0] = 97 ‘a’
(gdb) cont
Continuing.
Sisesta täht nr 1 :
b

Breakpoint 1, main () at poomine.c:18
18 ++count;
2: a[0] = 97 ‘a’
1: b[0] = 98 ‘b’
(gdb) disp a[1]
3: a[1] = 98 ‘b’
(gdb) cont
Continuing.
Sisesta täht nr 2 :
c

Breakpoint 1, main () at poomine.c:18
18 ++count;
3: a[1] = 98 ‘b’
2: a[0] = 97 ‘a’
1: b[0] = 99 ‘c’
(gdb) disp a]
A syntax error in expression, near `]’.
(gdb) disp a[2]
4: a[2] = 99 ‘c’
(gdb) cont
Continuing.
abc:

Program exited normally.
(gdb)

Leave a Reply