Program hangs when encounters switch

This is a discussion on Program hangs when encounters switch within the C Programming forums, part of the General Programming Boards category; Hi folks, I've come across a peculiar behavior of the program written below: Code: #include<stdio.h> #include<stdlib.h> void removevowels(char* sen); int ...

  1. #1
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46

    Program hangs when encounters switch

    Hi folks,

    I've come across a peculiar behavior of the program written below:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void removevowels(char* sen);
    
    int main()
    {
    	char *sen = "Hello, Good Morning!!";
    	printf("String before function call: %s\n", sen);
    	removevowels(sen);
    	printf("String after function call: %s\n", sen);
    
    	return 0;
    }
    
    void removevowels(char* sen)
    {
    	char *p, *q;
    	p = q = sen;
    	while(*p != '\0'){
    		printf("sen = %x\n", sen);
    		printf("p = %x\n", p);
    		printf("q = %x\n", q);
    		printf("%s\n\n", p);
    
    		switch(*p){
    			case 'a': case 'e': case 'i': case 'o': case 'u':
    			case 'A': case 'E': case 'I': case 'O': case 'U':
    				p++;
    				break;
    
    			default:
    				*q = *p;
    				p++;
    				q++;
    				break;
    		}
    
    	}
    	*q = '\0';
    }
    Upon compiling this program using MinGW (on Win XP), the compilation goes fine without errors but the program hangs when it is run.

    The same is not happening if I compile and run using Borland C Compiler (on Win XP).

    I added a few printf statements and found that it hangs at the switch statement.

    Please share your thoughts on this.

    Regards
    maverix

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If by "hangs" you means "throws a segmentation fault", then yes. (At least that's what I got compiling with MinGW on XP.) sen points to a string literal and cannot be modified. And so when you set q to sen, you can't modify q either, since q now points to a string literal. Declare sen as a char array, then strcpy your string literal in there.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Yes, I get a segmentation fault as well, but other than that, I'm not sure what the function is supposed to be doing?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Ah!! yes.
    I didn't think on those lines.

    I changed the declaration as below
    Code:
    char sen[] = "Hello, Good Morning!!";
    and it ran fine... :-)

    Thanks for your inputs...

    regards
    maverix

  5. #5
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Quote Originally Posted by Elysia View Post
    Yes, I get a segmentation fault as well, but other than that, I'm not sure what the function is supposed to be doing?
    I was just writing a function to remove vowels from a string!!!

    However, as I had mentioned earlier, the program in the first post works when compiled with Borland C compiler. Any specific reason for it to work. Was wondering why it (BCC) is letting to change a string literal.

    regards
    maverix

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Some compilers store the string in a read-only section and some do not. There's no (AFAIK) standard on which to define this behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Well whether a string literal is truly "read-only" is a matter for the implementation. For a start, it would require OS support, so if you tried it using a DOS compiler (even if running on XP), then all code and data is writeable, even where it's declared const.

    It's just another example of you need to understand what the standard is telling you, rather than trying to figure out the rules from observations on a specific compiler. The standard in this instance just states that "strings" can be placed in read-only memory.

    Another point of view is to never equate "works" with "bug-free". For any given implementation, there are plenty of places where you get lucky and it does what you want, but try the same thing on another implementation, and it will hand you your ass on a plate.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  2. Rotary switch program help
    By hudson in forum C Programming
    Replies: 4
    Last Post: 02-15-2006, 05:49 AM
  3. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21