Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 14:42

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: [C] String Retrival
PostPosted: 17 Mar 2009 21:16 

Joined: 17 Mar 2009 21:04
Posts: 7
Out of my own curiousity I attempted writing a little function to retrieve strings without the use of string.h, however whenever I call the function the program crashes, here is the function:
Code:
int string(void)
{
int i;
char string[i];

for(i=0; (i!="\n")&&(i<=2147483646); i++)
{
string[i] = getchar();
if ('string[i]'=="\b") {
                      printf("\b");
                      i--;
                      }
else printf("%c", &string[i]);
}
}


Top
 Profile  
 
 
 Post subject: Re: [C] String Retrival
PostPosted: 17 Mar 2009 23:26 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
it's normal that crashes, that code is wrong in almost each line and is not clear what it should do :)

the following function is the correct code of what I "guess" should be done by the one you pasted:
Code:
int string(void) {
    int     i;
    char    buf[1000];

    for(i = 0; i < (sizeof(buf) - 1); i++) {
        buf[i] = getchar();
        fputc(buf[i], stdout);
        if(buf[i] == '\b') i--;
        if(buf[i] == '\n') break;
    }
    buf[i] = 0;
    return(i);
}


Top
 Profile  
 
 Post subject: Re: [C] String Retrival
PostPosted: 17 Mar 2009 23:45 

Joined: 17 Mar 2009 21:04
Posts: 7
Thanks so much I am learning C through a code book I found in the trash pretty worn, I do not have internet access either (posting from a friends house).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: