C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 2nd, 2011, 11:19 AM
RodEsp RodEsp is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 1 RodEsp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 36 sec
Reputation Power: 0
Exclamation Memory & arrays - "Segmentation fault (core dumped)" error.

Hey guys, so I'm trying to write a little program that will put together any number of strings passed to it into a single string and then print out that string backwards.
I think I have everything correct but I keep getting a "Segmentation fault (core dumped)" error after compiling and running. So far the output I get looks like this if I enter "Hello" and "World" for my two strings:


argc = 3
argv[1] = Hello
argv[2] = World
len = 12
string = 9
string = 9Hello World
Segmentation fault (core dumped)


Also, how can I fix that I'm getting that weird 9 character for my *string[0] index? I'd like my string "Hello World" to start from *string[0].


This is my code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void reverse(char *string);
 
/**************************************************  ****************************
 * main() function
 **************************************************  ****************************/
 
int main(int argc, char **argv){
 
        //check for arguments
        if ( argc < 2 )
        {
          printf("Usage : reverse <string>\n");
          return 0;
        }
 
        printf("argc = %d\n", argc);
 
        //concatenate the strings
        int i = 1;
        int len = 0;
 
        while ( i < argc  )
        {
          printf("argv[%d] = %s\n", i, argv[i]);
          i++;
        }
 
        i = 1;
 
        while ( i < argc )
        {
          len += strlen(argv[i]) + 1;
          i++;
        }
 
        printf("len = %d\n", len);
 
        char *string[len];
        i = 1;
 
        printf("string = %s\n", *string);
 
        while ( i < argc )
        {
          strcat(*string, argv[i]);
          strcat(*string, " ");
          i++;
        }
 
        //reverse the string
        reverse(*string);
 
        printf("string = %s\n", *string);
 
        return 0;
}
 
/**************************************************  ****************************
 * reverse() function using recursion
 **************************************************  ****************************/
 
void reverse(char *string)
{
        if(*string != '\0')
        {
          reverse(string+1);
        }
 
        printf("Reverse of String = %s\n", *string);
}

Reply With Quote
  #2  
Old February 3rd, 2011, 10:48 AM
Cerl Cerl is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 3 Cerl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 23 m 14 sec
Reputation Power: 0
Hi,
The problem is char *string[len] can't allocat the memory like that use malloc or calloc :
Code:
void reverse(char *string);

/**************************************************    ****************************
 * main() function
 **************************************************    ****************************/

int main(int argc, char **argv){

        //check for arguments
        if ( argc < 2 )
        {
          printf("Usage : reverse <string>\n");
          return 0;
        }

        printf("argc = %d\n", argc);

        //concatenate the strings
        int i = 1;
        int len = 0;

        while ( i < argc  )
        {
          printf("argv[%d] = %s\n", i, argv[i]);
          i++;
        }

        i = 1;

        while ( i < argc )
        {
          len += strlen(argv[i]) + 1;
          i++;
        }

        printf("len = %d\n", len);

        char *string;
        string=(char*)calloc(1,len);
        i = 1;

        printf("string = %s\n",string);

        while ( i < argc )
        {
          strcat(string, argv[i]);
          strcat(string, " ");
          i++;
        }

        //reverse the string
        reverse(string);

        printf("string = %s\n",string);
        free(string);
        return 0;
}

/**************************************************    ****************************
 * reverse() function using recursion
 **************************************************    ****************************/

void reverse(char *string)
{
        if(*string != '\0')
        {
          reverse(string+1);
        }

        printf("Reverse of String = %s\n",string);
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - "Segmentation fault (core dumped)" error.


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap