string - Help needed in Segmentation Error in my C code -
i have been trying identify program generates segmentation no avail. need in pin-pointing of strings operations or char pointers causing segmentation fault during runtime. program compiles gives segmentation error during run-time.
#include<curses.h> #include<strings.h> #include<unistd.h> #include<stdlib.h> /*implements scrolling headband takes string argument , continously scrolls*/ int main(int argc, char* argv[]) { /*a substring function return substring of string*/ char *substr(const char *src, int start, int len); /*appends character given string*/ void append(char* s, char c); /***check if argument invalid***/ if(argc!=2) { puts("invalid number of arguments: usage:headband <string>"); } /**get headtext string argument argv[1]**/ char *headtext = argv[1]; /*headband(headtext);*/ /*temporary variable store strings execution progresses*/ char temp[100]; /*counter streaming , scolling headband text*/ int count = 0; /*placeholder temporary headband text*/ char hold[100]; int i; /*maximum x , y co-ordinates of screen*/ int max_x,max_y; /*initialize screen ncurses*/ initscr(); /*don't show cursor*/ curs_set(0); /*get terminal console dimensions*/ getmaxyx(stdscr, max_y, max_x); /*get console width set default console screen width=80*/ int consolewidth = max_x; /*clear screen*/ clear(); /*set first value end of string momment*/ temp[0] = '\0'; /*run loop continuously keep scrolling*/ (;;) { for(i=0; < strlen(headtext); i++) { count++; /*append headband text character character string hold*/ append(temp, headtext[i]); move(0,consolewidth - count); if (consolewidth - count > 0) { mvaddstr(0,console_width-count,temp); refresh(); } else if (consolewidth - count == 0) { strcpy(hold, temp); char q; int com = i; for(;;) { /*scroll text triming 1 character @ time*/ /*from left, adding character the*/ /*right side of text*/ com = com + 1; if (com < 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 return substring of 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 character given string*/ void append(char s[], char c) { int len = strlen(s); s[len] = c; s[len+1] = '\0'; }
i don't know compiler using, here quick guide how debug gcc , gdb:
$ gcc -g file.c -o myexec # -g flag enables debug info $ gdb ./myexec
now in gdb prompt. if need set command line arguments use:
set args <arg1> <arg2> ...
then run program
run
once crashes can sorts of things. shows @ point in program error happened. want use these commands:
bt # prints backtrace / call stack print <expr> # print value of expression
there couple of cheat sheets on web, can quick idea @ sorts of commands available.
Comments
Post a Comment