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

Recommended Answers

All 10 Replies

Oop. Never mind. :)

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.

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.

And does your compiler support C99?

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.

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

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

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

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.

Got it. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.