Welcome to the Coding Planet!
HomeHome  ­PortalPortal  ­RegisterRegister  ­Log inLog in  
Post new topic   Reply to topicShare | 
 

 Debugging exercises

View previous topic View next topic Go down 
AuthorMessage
Spoofer
Broadcaster
Broadcaster


Posts: 14
Join date: 2008-04-29

PostSubject: Debugging exercises   Wed Apr 30, 2008 10:40 am

Enas kalos programmatisths einai must na xerei na vriskei kai na diorthwnei ta lathh tou grhgora kai eukola, fusika oxi mono ta dika tou, afou polles fores tha xreiastei na sunergastei me allous. Isws kapoia stigmh na ginei ena tutorial panw se debugging techniques genika. Pros to paron as grafoume edw "exupnes" askhsoules( polles fores einai dika mas lathh Razz ) pou tha protrepoun tous upoloipous na vriskoun thn aitia tou lathous kai na diorthwnoun ton kwdika. Pisteuw einai mia wraia askhsoula otan variesai na grafeis kwdika na exaskeis to mualo sou etsi.
Loipon xekiname me ton parakatw aplo kwdika:

Code:
include <stdio.h>

int main(void){

        struct data{

                char *first_name;
                char *last_name;
        }data;

        puts("Give me your name, please:");
        scanf("%s",data.first_name);
        printf("Hello %s, now give me your last name:\n",data.first_name);
        scanf("%s",data.last_name);
        printf("Name: %s and Last name: %s \n",data.first_name,data.last_name);

        return 0;
}


Ena sample output einai auto:
Code:

Give me your name, please:
Odusseas
Hello Odusseas, now give me your last name:
Papadopoulos
Segmentation fault


To lathos telika to vlepei o gcc. Alla tis ptaiei? Ti prepei na ginei?
Back to top Go down
View user profile
Brolly
Admin
Admin


Posts: 3
Join date: 2008-04-24

PostSubject: Re: Debugging exercises   Wed Apr 30, 2008 11:15 am

Na desmeusoume mnhmh gia to onoma/epi8eto

Code:
data.first_name=(char*)malloc(20*sizeof(char));
     data.last_name=(char*)malloc(20*sizeof(char));
Back to top Go down
View user profile
m1cRo



Posts: 9
Join date: 2008-04-30

PostSubject: Re: Debugging exercises   Wed Apr 30, 2008 1:52 pm

Code:
include <stdio.h>

int main(void){

        struct data{

                char first_name[10];
                char last_name[10];
        }data;

        puts("Give me your name, please:");
        scanf("%9s",data.first_name);
        printf("Hello %s, now give me your last name:\n",data.first_name);
        scanf("%9s",data.last_name);
        printf("Name: %s and Last name: %s \n",data.first_name,data.last_name);

        return 0;
}
Back to top Go down
View user profile
m1cRo



Posts: 9
Join date: 2008-04-30

PostSubject: Re: Debugging exercises   Wed Apr 30, 2008 1:53 pm

Brolly wrote:
Na desmeusoume mnhmh gia to onoma/epi8eto

Code:
data.first_name=(char*)malloc(20*sizeof(char));
     data.last_name=(char*)malloc(20*sizeof(char));



Einai ok apla meta prepei na thn kaneis free .
Episis an kapios patisei parapano apo 20 xaraktires pali exeis overflow .
Back to top Go down
View user profile
Spoofer
Broadcaster
Broadcaster


Posts: 14
Join date: 2008-04-29

PostSubject: Re: Debugging exercises   Wed Apr 30, 2008 2:02 pm

Cool, m'aresei opws paei.. epomenws poia einai h veltisth lush?
Mhn xexname oti an einai >20 den tha einai onoma ara thelei enan effective elegxo megethous me analogo mhnuma.
As ton kanoume oso pio asfalh ginetai.. perimenw protaseis.


// edit: ok mperdeuthka me ton kwdika sou panw. Nomiza oti einai o dikos mou. Grafe kai kanena mhnuma re anthrwpe. cheers


Last edited by Spoofer on Wed Apr 30, 2008 2:44 pm; edited 1 time in total
Back to top Go down
View user profile
m1cRo



Posts: 9
Join date: 2008-04-30

PostSubject: Re: Debugging exercises   Wed Apr 30, 2008 2:24 pm

Spoofer wrote:
Cool, m'aresei opws paei.. epomenws poia einai h veltisth lush?
Mhn xexname oti an einai >20 den tha einai onoma ara thelei enan effective elegxo megethous me analogo mhnuma.
As ton kanoume oso pio asfalh ginetai.. perimenw protaseis.


Ma egrapsa mia lysh apo panw .
an thelete na ekxoreite mnimh tote use malloc kai free .
Back to top Go down
View user profile
Spoofer
Broadcaster
Broadcaster


Posts: 14
Join date: 2008-04-29

PostSubject: Re: Debugging exercises   Wed Apr 30, 2008 2:55 pm

m1cRo wrote:
Code:
include <stdio.h>

int main(void){

        struct data{

                char first_name[10];
                char last_name[10];
        }data;

        puts("Give me your name, please:");
        scanf("%9s",data.first_name);
        printf("Hello %s, now give me your last name:\n",data.first_name);
        scanf("%9s",data.last_name);
        printf("Name: %s and Last name: %s \n",data.first_name,data.last_name);

        return 0;
}


m1cRo aytos o kwdikas den douleuei etsi opws theloume. An dwsw sthn prwth scanf 15 'a' tote to periexomeno tou first_name tha einai ta 9 prwta 'a' kai tou last_name ta upoloipa 6.
To thema einai oti prepei na emfanizei mhnuma lathous se periptwsh pou kapoio apo ta 2 einai megalutero enos logikou megethous ( estw 15) kai tautoxrona na mhn ginetai kai overflow.
Perimenw kati oloklhrwmeno.
Back to top Go down
View user profile
christaker



Posts: 1
Join date: 2008-05-06

PostSubject: Re: Debugging exercises   Tue May 06, 2008 11:55 pm

Code:
#include <stdio.h>
#include <string.h>

main()
{
   struct data{
       char first_name[15];
       char last_name[15];
   }data;
         
   puts("Give me your name, please:"); 
   while(scanf("%s",data.first_name)&&strlen(data.first_name)>15){
      puts("Name must be under 16 char.\nGive me your name please : ");};
   
   printf("Hello %s, now give me your last name:\n",data.first_name);
  while(scanf("%s",data.last_name)&&strlen(data.last_name)>15){
      puts("Last name must be under 16 char.\nGive me your last name please : ");};
   printf("Name: %s and Last name: %s \n",data.first_name,data.last_name);

   return 0;
}


Etsi einai dekto? An mou epitrepetai vevaia na kanw include to string.h ....... Smile
Back to top Go down
View user profile
Spoofer
Broadcaster
Broadcaster


Posts: 14
Join date: 2008-04-29

PostSubject: Re: Debugging exercises   Wed May 07, 2008 2:25 am

christaker wrote:
Code:
#include <stdio.h>
#include <string.h>

main()
{
   struct data{
       char first_name[15];
       char last_name[15];
   }data;
         
   puts("Give me your name, please:"); 
   while(scanf("%s",data.first_name)&&strlen(data.first_name)>15){
      puts("Name must be under 16 char.\nGive me your name please : ");};
   
   printf("Hello %s, now give me your last name:\n",data.first_name);
  while(scanf("%s",data.last_name)&&strlen(data.last_name)>15){
      puts("Last name must be under 16 char.\nGive me your last name please : ");};
   printf("Name: %s and Last name: %s \n",data.first_name,data.last_name);

   return 0;
}


Etsi einai dekto? An mou epitrepetai vevaia na kanw include to string.h ....... Smile



christaker wraia prospatheia kai fusika mporeis na kaneis xrhsh tou string.h. Ti exeis omws na peis gi'auto:

Code:
Give me your name, please:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Name must be under 16 char.
Give me your name please :
as
Hello as, now give me your last name:
as
Name: as and Last name: as
Segmentation fault


ps: Ti mporei na ginei?
Back to top Go down
View user profile
 

Debugging exercises

View previous topic View next topic Back to top 
Page 1 of 1

Permissions of this forum:You cannot reply to topics in this forum
Code-Masters :: Coding :: C-
Post new topic   Reply to topic