954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

"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:

#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);
}
RodEsp
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Oop. Never mind. :)

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

at line 42, you can't declare a variable in c in the middle of the program, all variable declarations have to be at the beginning of the code. Also, you can't use a non-constant variable to initialize the size of the array inside brackets. Even if you could do that, you shouldn't have a pointer that points to arrays of characters, you never allocated any memory to that pointer, thats why you are getting the segmentation fault.

chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 19
 
at line 42, you can't declare a variable in c in the middle of the program

I understand it became legal to do so in C99.

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

And does your compiler support C99?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
I understand it became legal to do so in C99.


Even though it is legal in C99, its not legal in standard C. C code isn't as portable by writing declarations halfway through the code.

chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 19
 
Even though it is legal in C99, its not legal in standard C.

In March 2000, ANSI adopted the ISO/IEC 9899:1999 standard. This forum is labelled "Our C forum is the place for Q&A-style discussions related to the C language as per the ANSI C standard."

Do you mean "standard", or do you mean "a previous version"? :)

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

Hmm... I didn't know ANSI C was changed from C90 to C99, I guess I am more than 10 years behind the times...

chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 19
 

Yeah, my compiler supports C99, and no that was not the problem. I still don't know why I got a segmentation fault but my program works now.

Here's the functional code if anyone is interested. (It only works if you're running it from a command prompt or a terminal in UNIX since the strings have to be passed through the command line. That can be easily adapted for other methods of input though.)

#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;
        }

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

        i = 1;

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

        char string[len];
        string[0] = '\0';
        i = 1;

        while ( i < argc )
        {
          strcat(string, argv[i]);
          if ( i == (argc - 1))
          {
            break;
          }
          strcat(string, " ");
          i++;
        }

        printf("Reverse of String = ");

        //reverse the string
        reverse(string);

        printf("\n");

        return 0;
}

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

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

        printf("%c", *string);
}
RodEsp
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

The reason you were got a segmentation fault is because when you declared char *string[len]; in your first code as opposed to char string[len]; like in your second code, there was no memory bing pointed to by string. The first is a pointer to an array of strings, while the second is just a string.

chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 19
 

Got it. Thanks.

RodEsp
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: