strcpy error

This is a discussion on strcpy error within the C Programming forums, part of the General Programming Boards category; WORKING char result[] = "ls"; parse(result, args); NOT WORKING- giving me Segmentation fault (core dumped) char result[512]; strcpy(result,"ls"); parse(result, args); ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    strcpy error

    WORKING
    char result[] = "ls";
    parse(result, args);
    NOT WORKING- giving me Segmentation fault (core dumped)

    char result[512];
    strcpy(result,"ls");
    parse(result, args);
    I don't get it. Why is the second code not working. I am just copying "ls" into result with the strcpy function
    Last edited by jordanguyoflove; 03-09-2009 at 03:15 PM.

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    you never 'terminate' the string ('\0').

    http://www.cs.cf.ac.uk/Dave/C/node19.html

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    strcpy automatically null terminates the string.
    As for why it doesn't work, I urge you to post the smallest possible compilable code that demonstrates the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    i stand corrected.

    show us your parse() code.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    strcpy() expects both its arguments to be pointers to a char array.
    Last edited by itCbitC; 03-09-2009 at 03:28 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    It does not. It wants one char* (dst) and one const char* (src), nothing more.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Quote Originally Posted by itCbitC View Post
    strcpy() expects both its arguments to be pointers to a char array.
    This also doesn't work.
    char result[128];
    char ass[] = "ls";
    strcpy(result,ass);
    parse(result, args);

    Oh and I didn't get previously Segmentation fault but an error where the program terminated.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by Elysia View Post
    It does not. It wants one char* (dst) and one const char* (src), nothing more.
    How's that different from what I posted?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Quote Originally Posted by abraham2119 View Post
    i stand corrected.

    show us your parse() code.
    That's it.
    Code:
    void  parse(char *line, char **args)
    {
         while (*line != '\0') {       
              while (*line == ' ' || *line == '\t' || *line == '\n')
                   *line++ = '\0';     
              *args++ = line;          
              while (*line != '\0' && *line != ' ' && 
                     *line != '\t' && *line != '\n') 
                   line++;            
         }
         *args = '\0';                
    }//end of void  parse(char *line, char **args)

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <string.h>
    
    
    #define MAX_LINE 80
    
    void setup(char inputBuffer[], char *args[], int *background)
    {
    
    }
    
    
    
    
    
    /*
    void  parse(char *line, char **args)
    {
         while (*line != '\0') {       
              while (*line == ' ' || *line == '\t' || *line == '\n')
                   *line++ = '\0';     
              *args++ = line;          
              while (*line != '\0' && *line != ' ' && 
                     *line != '\t' && *line != '\n') 
                   line++;            
         }
         *args = '\0';                
    }//end of void  parse(char *line, char **args)
    */
    
    void  parse(char *line, char **args)
    {
         while (*line != '\0') {       
              while (*line == ' ' || *line == '\t' || *line == '\n')
                   *line++ = '\0';     
              *args++ = line;          
              while (*line != '\0' && *line != ' ' && 
                     *line != '\t' && *line != '\n') 
                   *line++;            
         }
         *args = '\0';                
    }//end of void  parse(char *line, char **args)
    
    
    
    void preparse(char *line, char **args)
    {
    
    
    
     
     
    	char result[] = "ls";
            
    	parse(result, args);
    
    }
    
    
    void shell(char **args)
    {
    int state;
    pid_t  pid;
    
    pid = fork();
    
    	if(pid  < 0){
    	fprintf(stderr, "Fork Failed");
    	exit(-1);
    	}//end of if(pid  < 0) 
    
    	
    	
    	else if (pid == 0) {          /* for the child process:         */
              if (execvp(*args, args) < 0) {     /* execute the command  */
                   printf("execvp failed\n");
                   exit(1);
              }
         }
         else {                                  /* for the parent:      */
              while (wait(&state) != pid);       /* wait for completion  */
                   
         }
    
    }//end of shell();
    
    
    
    
    int main(void)
    {
    
    
    char inputBuffer[MAX_LINE];
    int background;
    char *args[MAX_LINE/2 + 1];
    char commandLine[512];
    char commandLine1[256];
    char commandLine2[128];
    char commandLine3[128];
    char commandLine4[128];
    
    int semicolon = 0;
    
    
    	while(1)
    	{
    
    	background = 0;
    	printf(" COMMAND ->");
    	setup(inputBuffer, args, &background);
    	
    	gets(commandLine);
            
    	
    
    	//getsnew(commandLine);	
    
    
    	preparse(commandLine, args);
    	
    	//parse(commandLine, args); 
    
    	shell(args);
    
    	}//end of while(1)
    
    	printf("\n");
    
    	
    
    
    }//end of int main(void)

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Quote Originally Posted by itCbitC View Post
    How's that different from what I posted?
    Then what were you trying to imply?
    I must have misunderstood your post.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
              while (*line == ' ' || *line == '\t' || *line == '\n')
                   *line++ = '\0';
    You probably want to stop this when you hit the end of the string too. Just in case there is a string of just spaces and tabs.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21