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

Why am I getting segmentation fault?

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void reverse(int pos, char *str){
int l = strlen(str);
if(pos<(l/2))
{
char ch;
ch = str[pos];
str[pos]=str[l-pos-1];
str[l-pos-1]=ch;
reverse(pos+1,str);
}
}

char* reverseWords(char *a) {
int i=0,j=0, l=0;
char *s, *result;
l = strlen(a);
reverse(0,a);
result = (char*)malloc(sizeof(char)*(l+1));
strcpy(result,"");

for(i=0;i<=l;i++)
{
if(a[i]!=' ' && a[i]!='\0')
s[j++]=a[i];
else{
s[j++]='\0';
reverse(0,s);
strcat(result,s);
if(i!=l)
strcat(result," ");
j=0;
}

}
result[l]='\0';
return result;
} <- At this point I am getting segmentation fault.

int main()
{
char str[]="STRING TO REVERSE";
char *output;
output = reverseWords(str);
printf("%s\n",output);
return 0;
}
Oct 1 '11 #1
1 1447
johny10151981
1,059 1GB
your problem is you didn't allocate memory for char *s..... and please use wrap your code portion with CODE tage
Oct 1 '11 #2

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

Similar topics

10
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
18
by: bumps | last post by:
Guys, I am getting segmentation fault while I am trying to open a file in a small function. int PingSamplesList::saveListElements (char *outputFileString) { PingSampleElement *tmpPtr; int...
19
by: Sameer | last post by:
Hi friends, I am using Mandriva Linux 9.2 and gcc. My source code is, int chunkin ; //no error int i ; for (i=0;i<7225;i++) { chunkin = somedata ;
8
by: priyanka | last post by:
Hi, I am getting segmentation fault in a very simple thing. But I am not able to figure out waht is wrong. I should be missing something. char *kernel_rget_pt; char kernel_rec_buf = {'t',...
5
by: doni | last post by:
Hi, I am a beginner to C and I wrote a program that takes arguments and prints it in hex. I am getting a segmentation fault if I dont pass any arguments instead I want it to display the Usage...
2
pawanrpandey
by: pawanrpandey | last post by:
Hi, When I am running my Perl Application, which uses some additional Perl Libraries, getting 'Segmentation Fault (core dumped)' error. I have build the perl again but still the problem is...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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.