0

I am currently learning C and trying to do the problems from The C Programming Language, not sure why this error is occurring. Question:

Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time.

I have tried to comment out the whole segment of the function reverse(make it do nothing), and yet it doesn't help. getln was provided by the textbook.

#include <stdio.h>

#define MAXLINE 1000

int getln(char line[], int limit);
void reverse (char line[]);

main(){
    int len;
    char line[MAXLINE];
    while ((len = getln(line, MAXLINE)) > 0){
      reverse(line);
      printf('%s',line);
    }
}

/*returns length of s*/
int getln(char s[], int lim){
  int c, i;
  for (i = 0; i < lim-1 && (c = getchar())!=EOF && c!='\n'; ++i)
    s[i] = c;
  if (c == '\n'){
    s[i] = c;
    ++i;
  }
  s[i] = '\0';
  return i;
}

void reverse(char s[]){
  int len, i;
  len = sizeof(s)/sizeof(s[0]);
  char temp[len];
  for (i = 0; i < len; ++i){
    temp[i] = s[len - i];
  }
  while ((s[i] = temp[i] != '\0'))
    ++i;
}

I was expecting the line that I entered to be reversed. Yet, error shows - Segmentation fault.

Thank you!

  • 2
    In the `reverse()` function, `sizeof(s)` is the size of a pointer, not the size of the array. – Barmar Apr 04 '19 at 21:09
  • 2
    Use `len = strlen(s)+1`. – Barmar Apr 04 '19 at 21:09
  • See https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array – Barmar Apr 04 '19 at 21:12
  • The line `len = sizeof(s)/sizeof(s[0]);` doesn't give the array size when the array is a function parameter (nor in some other circumstances involving global arrays defined in other files but not declared with a size). The `len = sizeof(s)/sizeof(s[0]);` notation only works when the array is a complete array type. – Jonathan Leffler Apr 04 '19 at 21:14
  • Also, make sure you don't copy the null terminator to the beginning of `temp`. – Barmar Apr 04 '19 at 21:15
  • 1
    `for (i = 0; i < len; ++i)` will reverse the string twice ending up wit what you started with... After fixing your `strlen(s)` problem, perhaps `for (i = 0; i < len / 2; ++i)` ?? – David C. Rankin Apr 04 '19 at 22:31

1 Answers1

0

the posted code does not compile!

When compiling, always enable the warnings, then fix those warnings.

For gcc, at a minimum use:

-Wall -Wextra -Wconverson -pedantic -std=gnu11

Note: other compilers use different options to produce the same output.

Compiling the posted code results in:

gcc   -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" 

untitled.c:8:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main(){
 ^~~~

untitled.c: In function ‘main’:

untitled.c:13:14: warning: multi-character character constant [-Wmultichar]
       printf('%s',line);
              ^~~~

untitled.c:13:14: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]

In file included from untitled.c:1:0:
/usr/include/stdio.h:318:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
 extern int printf (const char *__restrict __format, ...);
            ^~~~~~

untitled.c: In function ‘getln’:

untitled.c:21:12: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
     s[i] = c;
            ^
untitled.c:23:12: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
     s[i] = c;
            ^

untitled.c: In function ‘reverse’:

untitled.c:32:15: warning: ‘sizeof’ on array function parameter ‘s’ will return size of ‘char *’ [-Wsizeof-array-argument]
   len = sizeof(s)/sizeof(s[0]);
               ^

untitled.c:30:19: note: declared here
 void reverse(char s[]){

I would also, strongly, suggest using the C library function: fgets()to read the line, then use the function:strspn()to replace the trailing newline with a NUL byte, then usestrlen()` to obtain the number of characters in the line. These functions are fully explained in the MAN pages

Note: This looks like a homework assignment, so I'm not going to give you the code to perform the task, but you should be able to produce that yourself.

As a starting point, the function main() has two valid signatures:

int main( int argc, char (argv[] )
int main( void )

Note that both signatures have a return type of int

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Thank you so much! This wasn't a homework assignment as I am just learning it for myself, but I will look into everything suggested! – Daniel Lam Apr 08 '19 at 01:55