Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp needed in Segmentation Error in my C code
    text
    copied!<p>I have been trying to identify where my program generates segmentation all to no avail. I need help in pin-pointing which of the strings operations or char pointers is causing the segmentation fault during runtime. The program compiles successfully but gives segmentation error during run-time.</p> <pre><code>#include&lt;curses.h&gt; #include&lt;strings.h&gt; #include&lt;unistd.h&gt; #include&lt;stdlib.h&gt; /*Implements a Scrolling headband that takes a string argument and continously scrolls*/ int main(int argc, char* argv[]) { /*A Substring function to return substring of a string*/ char *substr(const char *src, int start, int len); /*Appends a character to the Given string*/ void append(char* s, char c); /***Check if the argument is invalid***/ if(argc!=2) { puts("Invalid number of arguments: Usage:headband &lt;String&gt;"); } /**Get headtext from the string argument argv[1]**/ char *headtext = argv[1]; /*headband(headtext);*/ /*temporary variable to store strings as execution progresses*/ char temp[100]; /*Counter for streaming and scolling headband text*/ int count = 0; /*Placeholder for temporary headband text*/ char hold[100]; int i; /*maximum x and y co-ordinates of the Screen*/ int max_x,max_y; /*Initialize screen for ncurses*/ initscr(); /*Don't show cursor*/ curs_set(0); /*Get terminal console dimensions*/ getmaxyx(stdscr, max_y, max_x); /*Get console width set to default console screen width=80*/ int consolewidth = max_x; /*Clear the screen*/ clear(); /*Set the first value as end of String for the momment*/ temp[0] = '\0'; /*Run this loop continuously to keep scrolling*/ for (;;) { for(i=0; i &lt; strlen(headtext); i++) { count++; /*Append headband text character by character to string hold*/ append(temp, headtext[i]); move(0,consolewidth - count); if (consolewidth - count &gt; 0) { mvaddstr(0,console_width-count,temp); refresh(); } else if (consolewidth - count == 0) { strcpy(hold, temp); char q; int com = i; for(;;) { /*Scroll text by triming one character at a time*/ /*from the left, then adding that character to the*/ /*right side of the text*/ com = com + 1; if (com &lt; strlen(headtext)) { q = headtext[com]; } else { com = 0; q = headtext[com]; //q = hold[0]; } strcpy(hold, substr(hold, 1, strlen(hold) - 1)); append(hold, q); move(0,0); clear(); mvaddstr(0,0,hold); refresh(); usleep(50); } } usleep(50); } } return 0; } /*A Substring function to return substring of a string*/ char * substr(const char *src, int start, int len) { char *dest = malloc(len+1); if (dest) { memcpy(dest, src+start, len); dest[len] = '\0'; } return dest; } /*Appends a character to the Given string*/ void append(char s[], char c) { int len = strlen(s); s[len] = c; s[len+1] = '\0'; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload