473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Program isn't working properly

94 64KB
My C program isn't reaching the "return 0;" statement:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *invertString( const char *const str );
  6.  
  7. int main( void )
  8. {
  9.     char string[ 25 ];
  10.     char *ptr;
  11.  
  12.     printf( "Enter a string without whitespace: " );
  13.     scanf( "%25s", string );
  14.  
  15.     ptr = invertString( string );
  16.  
  17.     if ( ptr != NULL )
  18.     {
  19.         printf( "The inverted string is %s.\n", ptr );
  20.     }
  21.     else
  22.     {
  23.         printf( "A memory error occurred.\n" );
  24.     }
  25.  
  26.     printf( "LINE %d.\n", __LINE__ );
  27.  
  28.     return 0;
  29. }
  30.  
  31. char *invertString( const char *const str )
  32. {
  33.     unsigned int i;
  34.     unsigned int j;
  35.     unsigned int lengthOfStr = strlen( str );
  36.     char *newStr = ( char * )malloc( ( lengthOfStr + 1 ) * sizeof( char ) );
  37.  
  38.     if ( newStr == NULL )
  39.     {
  40.         printf( "ERROR!" );
  41.         return newStr;
  42.     }
  43.  
  44.     for ( i = lengthOfStr - 1, j = 0 ; i >= 0 ; --i, ++j )
  45.     {
  46.         *( newStr + j ) = *( str + i );
  47.     }
  48.  
  49.     *( newStr + j ) = '\0';
  50.  
  51.     return newStr;
  52. }
The program is supposed to invert a string by creating a new string and there putting the contents of the original string in reverse order. The problem is, it isn't reaching the "return 0;" statement. I even added the statement

Expand|Select|Wrap|Line Numbers
  1. printf( "ERROR!" );
inside the invertString function to check whether a memory error is occurring, but it isn't executing. What is possibly going on? Thanks in advance.

PS: the encoding of the source file is CP1252.
PS: the program is compiling successfully.
Mar 11 '14 #1
4 1272
weaknessforcats
9,208 Expert Mod 8TB
Your invertString function is not returning.

Expand|Select|Wrap|Line Numbers
  1. for ( i = lengthOfStr - 1, j = 0 ; i >= 0 ; --i, ++j )
When i gets to zero it still satisfies the condition to continue the loop, which causes i to become -1. And off you go on an infinite loop.
Mar 11 '14 #2
stdq
94 64KB
Thanks for your answer. But when I ran the program, its execution ended automatically.
Mar 12 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
The code you posted does not work. It crashes in an infinite loop just where I said it would. The code you ran has to be different.
Mar 13 '14 #4
weaknessforcats
9,208 Expert Mod 8TB
The code you posted crashes where I said it would:

Expand|Select|Wrap|Line Numbers
  1. for ( i = lengthOfStr - 1, j = 0 ; i >= 0 ; --i, ++j )
  2.     {
  3.         *( newStr + j ) = *( str + i );
  4.     }
  5.  
  6.     *( newStr + j ) = '\0';
  7. etc...
  8.  
When i is zero, a --i occurs. This makes i a -1. Negative numbers are written in 2's complement so -1 looks like 4294967295, which is a very big positive number. This makes the loop run a few billion more times than planned. This causes str+i to try to access memory outside the program. Then the crash.
Mar 13 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion is showing half the range of farenheit. print...
8
by: Randy Yates | last post by:
I'm using mingw/g++ 3.3.3. When I use pos = tellg(), getline(), setg(pos), then the next getline() does NOT get from the original position. I've tried doing a clear() before the seekg() to no...
1
by: Roberto Castro | last post by:
I have some problems with the way I am showing the BLOB fields in the Image web controls. It does work on my localhost though sometimes I need to hit Refresh for the images to load properly....
5
by: Nita Raju | last post by:
Hi, I have to validate a textbox for date without using the validation controls. So i had to use IsDate(). It's not working properly when i give "11//2004". When i enter the above date it...
2
by: Annu | last post by:
Hi I need help on <enbed> tag. Following code(No 1) is working properly on windows but on linux code no.2 is not working Code No 1: <EMBED type='application/x-mplayer2' ...
12
kamill
by: kamill | last post by:
I have done a logout page for logout from admin section and provides a link to logout from admin section.Whenever i clicked on logout link it redirected to index.php of admin section......BUT when i...
5
by: damezumari | last post by:
When a user logs in to my site http://iwantyourquestion.com I set $_SESSION to true if his username and password are OK. When he calls a page I check if $_SESSION is true. If it not I ask him to...
3
by: rajasree | last post by:
Hi all, am doing a project in PHP. my javascript code is working properly in ie. But its not working in firefox. Please help me my code is as follows; <script language="javascript"...
4
by: zairali | last post by:
Hi, I am trying to fix a webpage ( http://www.d.umn.edu/itss/labs/maps/ ) which uses some html (or xml also?) to show pop ups when you rollover the numbers on a map of the building. They work fine...
8
by: neovantage | last post by:
Hey all, Website page is not working properly in IE6 and IE7 but working normal in firefox. Can somebody tell me how i can fixed that problem. Here is the url of the page http://www.ragehockey.biz/...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.