Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone make this C program simpler

Status
Not open for further replies.

SwatiA

Programmer
May 10, 2002
9
IN
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
/* to take a string input of unknown length from the user and print it*/
void main()
{
int i=0;
char *s;
s=(char *) malloc(MAX);
if(!s)
printf(&quot;memory not available\n&quot;);
int c;
while (( (c=getchar())!='\n') && (i < (MAX-1) ) )
s[i++]=c;
s='\0';
int j =MAX;
while((i==j-1))
{
j = j+MAX;
s=(char*) realloc(s,j);
if(!s)
printf(&quot;memory not available\n&quot;);
while(c!='\n' && (i < (j-1)))
{
s[i++]=c;
c=getchar();
}
s='\0';
}
printf(&quot;%s\n&quot;,s);
free(s);
}



 
How about:-

#include <stdio.h>

void main()
{
char inpstring[120];

printf(&quot;Please input string\n&quot;);
gets(inpstring);
printf(&quot;The string was %s\n&quot;,inpstring);
exit(0);
}
 
But that will give the core dump when string length is more than 120.
 
Ok increase the buffer to 500 that should be more than enough. You must remember that there is a maximum line length that can be entered from the command line.
 
#include <stdio.h>
int main(int argc, char **argv)
{
char *buff = 0;
int len = 0;
int c;


while((c = getc(stdin)) && c != '\n'){

if(!buff){
if(!(buff = (char *)malloc(1+len))) exit(1);
}else{
if(!(buff = (char *)realloc(buff,1+len))) exit(1);
}

*(buff+len++) = c;
}
*(buff+len) = 0;
printf(&quot;|%s|\n&quot;,buff);
exit(0);
}
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
thanks for the response. The response by jamisar(Programmer) has been helpful.
Swati
 
taking hint from jamisar(Programmer) I changed my program to:
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
void main()
{
char * s=NULL;
int i=0,c;
int j=MAX;

while( (c=getc(stdin))!='\n')
{
if (!s)
{
if(!(s=(char*)malloc(j)))
exit(1);
}
else if ( i== j-1)
{
j=j+MAX;
s=(char*)realloc(s,j);
if(!(s=(char*)realloc(s,j)))
exit(1);
}
s[i++]=c;
}
s='\0';
printf(&quot;%s\n&quot;,s);
}




 
it's a very bad practice to:

a) limit programs
b) impose limits to the user working interactively
c) use magic number like 120 or 500

what happen by: prg-name < file-name ?
you will never know what is the appropriate limit
not only for char, but int, arrays, struct and so on
struct xxx qqq[MAX];
who said you MAX is MAX enougth ?
how many progs are core dump, years later, because
of this MAX or a stupid magic-number ?

please, think and work dynamically! or go back to basic.

if you really want to do this way, do it safe
this never core dump:

#define MaxBuff what-you-want /* and USE it */

char buff[MaxBuff+1];

while(fgets(buff,MaxBuff,fd)){
OR:
while(fgets(buff,sizeof(buff),fd)){

PS: i prefer the 1. version -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
swati:
why so complex ??
- your while don't check the NULLER
- the realloc depend from 's' not from 'i'
please don't use ONE char variable names,
suppose you have to change 'i' to 'iii' in a x-thousend
file code, how will you sure do it ?
let main ALLWAYS exit a status.
void main() is STUPID! it will return a random value
what if:
prg-name && echo OK || echo KO
in a shell script ?
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
&quot;void main()&quot; may or may not be stupid, but it's not valid C, so that's reason enough to never use it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top