Reverse a String in C - Solve Segmentation Fault

  • Context: Undergrad 
  • Thread starter Thread starter carl123
  • Start date Start date
  • Tags Tags
    Reverse String
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a segmentation fault encountered in a C program intended to reverse a string using forked processes. The focus is on understanding the cause of the error and clarifying the correct handling of string indices.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their code and reports a segmentation fault when executing it.
  • Another participant points out that the variable i is set to the length of the string, which includes the terminating null character, leading to potential out-of-bounds access.
  • A further clarification is requested regarding the implications of using the length returned by strlen in the context of valid string indices.
  • It is noted that accessing an out-of-bounds element can lead to a segmentation fault, though it may not always occur.
  • A participant mentions a procedural note regarding the absence of a homework template in the original post.
  • There is a correction regarding the identification of the original poster, clarifying that the initial poster is not the participant who pointed out the template issue.

Areas of Agreement / Disagreement

Participants generally agree on the cause of the segmentation fault related to string indexing, but the discussion remains unresolved regarding the best approach to fix the code.

Contextual Notes

The discussion highlights the importance of correctly handling string indices in C, particularly in relation to the null terminator, but does not resolve the specific implementation issues in the code provided.

carl123
Messages
55
Reaction score
0
1.JPG

2.JPG

This is what I came up with but I keep getting segmentation fault whenever I run it in linux. Not sure why. Any help would be appreciated. Thanks
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
    int i, childpid;

    for (i = strlen(argv[1]); i >= 0; i--) {
        if (fork() == 0) {
            printf("%c\n",argv[1][i]);
            break;
        }
    }

    return 0;
}
 
Mathematics news on Phys.org
carl123 said:
i = strlen(argv[1])
This is the position of the terminating null character, not the last character of the string.
 
  • Like
Likes   Reactions: carl123
Orodruin said:
This is the position of the terminating null character, not the last character of the string.
Thanks for your reply. Pls could you explain further. Not sure what you mean
 
if you have a 10 character string then valid indexes are 0-9 but strlen returns a 10 so your first loop iteration is referencing an out of bounds element of the string which can result in a segmentation fault but not always.
 
Please note: @CAF123 this post is missing a homework template. Since there are answers it will stay as is.
 
Thanks for the correction. Since the thread died and @CAF123 straightened me out, let's close this thread.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K