473,383 Members | 1,801 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,383 software developers and data experts.

C: Progam not working

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. }
  53.  
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.
Aug 12 '14 #1
1 1116
weaknessforcats
9,208 Expert Mod 8TB
This code from invertString:
Expand|Select|Wrap|Line Numbers
  1. char *newStr = ( char * )malloc( ( lengthOfStr + 1 ) * sizeof( char ) );
  2.  
  3.     if ( newStr == NULL )
  4.     {
  5.         printf( "ERROR!" );
  6.         return newStr;
  7.     }
  8.  
will only print ERRROR! if malloc() returns a null pointer. This will only happen if your program runs out of heap memory. Since this is highly unlikely, you never see this ERROR!

Then there is this code in invertString:
Expand|Select|Wrap|Line Numbers
  1. for ( i = lengthOfStr - 1, j = 0 ; i >= 0 ; --i, ++j )
  2.      {
  3.         *( newStr + j ) = *( str + i );
  4.      }
  5.  
Assume, for the moment, your string is "A". lengthOfStr is 1. Now 1 is subtracted starting i at 0. The loop continues if i >=0. The A is inverted. Now comes --i for the second cycle. i is now -1 When you hit str+i you crash trying to access memory that's not part of your program.
Aug 12 '14 #2

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

Similar topics

5
by: carl bloc | last post by:
Has any body got any code for this brief that a friend has to do, its for the uk version of the lottery i.e 6 balls and a bouns ball. 1. a control loop is required to determine the operation...
13
by: Buck Rogers | last post by:
Hi guys! I am trying to do an excercise from my C book, which coincidently is very similar to the one in the recent "program" thread. Task: Write a program that uses redirection to accept...
5
by: tshad | last post by:
I have been working with setting my drop boxes to allow double clicking to select an item. It worked fine until I made some changes. I then stripped the page down to the bare essentials to find...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
10
by: sp | last post by:
The application is written in Visual Basic / .NET and working without problems under Windows XP, Windows 2000, Windows 2003 but it isn't working under Windows ME and Windows 98 - the computer...
17
by: vj | last post by:
Hi All, I am using C++Builder V5.0 to run my C programs. I am not an expert in C. Normally, I run the C program with these steps: 'Compile', 'Make Project', 'Build Project' and then 'Run'. Many...
0
by: fabien.lyon | last post by:
-----Message d'origine----- De : fabien.lyon Envoyé : mercredi 30 mai 2007 20:16 À : 'python-list@python.org' Objet : RE: embeded python progam into visual C++ application crash 2.5.1 is...
5
by: Break2 | last post by:
I am new to C++ and have learned that when I build a program and I want to learn how, it is best to build each program according to the same standard. In my programs I want to have a function...
0
by: jk9427 | last post by:
Hello all, I have a weird situation here. We are on an ISeries AS400. We have created an SQL table and can access it just fine when the COBOL program is called directly. The problem exists when...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.