Thread: I don't get the difference :(

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    30

    Question I don't get the difference :(

    Aren't both of the below posted codes analogous
    Code:
    int getword(char *word, int len){
    	int cnt = 0,c;
    
    	for(cnt  = 0; (c=getchar())!= ' ' && c!='\n' && cnt<len; cnt++)
    		*word++=c;
    	*word='\0';
    	return word[0];
    }
    Code:
    int getword(char word[], int len){
    	int i=0,c;
    	for(i=0; (c=getchar())!=' ' && c!='\n' && i<len; i++)
    		word[i] = c;
    	word[i] = '\0';
    	return word[0];
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No, the return values are completely different. The first version returns '\0', and the second version returns the first letter retrieved from getchar(). Also, both version are buggy in that they can overrun the buffer.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    Quote Originally Posted by pshirishreddy View Post
    Aren't both of the below posted codes analogous
    Code:
    int getword(char *word, int len){
    	int cnt = 0,c;
    
    	for(cnt  = 0; (c=getchar())!= ' ' && c!='\n' && cnt<len; cnt++)
    		*word++=c;
    	*word='\0';
    	return word[0];
    }
    Code:
    int getword(char word[], int len){
    	int i=0,c;
    	for(i=0; (c=getchar())!=' ' && c!='\n' && i<len; i++)
    		word[i] = c;
    	word[i] = '\0';
    	return word[0];
    }
    Code:
    char buf[BUFSIZE];
    int bufp=0;	//next free position in buf
    
    int getch(void){	
    return((bufp>0) ? buf[--bufp]:getchar());
     }
    
    void ungetch(int c){
    	if(bufp >=BUFSIZE)
    		printf("too manu");
    	else
    		buf[bufp++] = c;
    }
    int getword(char *word, int lim){
    	int c,getch(void);
    	void ungetch(int);
    	char *w = word;
    	while(isspace(c=getch()))
    		;
    	if(c!=EOF)
    		*w++=c;
    	if(!isalpha(c)){
    		*w='\0';
    		return c;
    	}
    	for(;--lim>0;w++)
    		if(!isalnum(*w=getch())){
    			ungetch(*w);
    			break;
    		}
    		*w = '\0';
    		return word[0];
    }

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    What do you want to know by just showing a code?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  3. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  4. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM