LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2010, 06:55 AM   #1
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Rep: Reputation: 9
how to reverse a string without using standard library


I am making a program to reverse a string
Code:
#include<stdio.h>
int main ()
{
int i,j;
char *p,*s,*g;
 p="abcdefgt";
 i=0;j=0;
 s=NULL;

        while (p[i] != '\0')
        {
        printf("%d  %c\n ",i,p[i]);
        i++;
        }
        i--;

       while (i>=0){

         s[j]=p[i];
         j++;
        i--;
        printf("\n j=%d i=%d",j,i);
        }
  printf(" %s\n",s);
}
but the above code is giving me a segmentation fault on line
Code:
         s[j]=p[i];
Why is that happening?
 
Old 09-22-2010, 07:15 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by jamesbon View Post
Code:
char *p,*s,*g;
...
s=NULL;
...
s[j]=p[i];
}
but the above code is giving me a segmentation fault on line
Code:
s[j]=p[i];
Why is that happening?
You're dereferencing a NULL pointer - malloc() some memory or make s into an array of adequate size.
 
Old 09-22-2010, 08:27 AM   #3
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Original Poster
Rep: Reputation: 9
The program has worked the way you suggested.But can you give me a link to what you said.
 
Old 09-22-2010, 08:56 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by jamesbon View Post
The program has worked the way you suggested.But can you give me a link to what you said.
I don't know what you mean - if you want info just search the internet or any C book's index for "dynamic memory allocation" or "arrays".
 
Old 09-22-2010, 10:29 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You have a pointer to a char 's', and what char array does it point to? In C, if you haven't allocated memory, and specifically assigned the pointer to point to that memory, then de-referencing the pointer (assigning to the place to which it points) will cause a write to someplace random, violating memory protection, and resulting in the segmentation fault that you see. Probably the number one C novice conceptual hurdle.

--- rod.
 
Old 09-22-2010, 01:05 PM   #6
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Original Poster
Rep: Reputation: 9
theNombr thanks I got your point.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
_mbsbtype() in standard library. jaepi Programming 1 12-13-2007 10:22 AM
Standard Template Library Reference (std::string) lucky6969b Programming 3 03-15-2006 03:09 PM
'sort' fn. in C++ standard library frankie_DJ Programming 2 12-14-2004 02:27 PM
how to reverse a string in C programming egoleo Programming 12 04-05-2004 05:45 AM
C++ standard library. Thetargos Programming 8 10-09-2003 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration