Thread: problems with strcpy

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    11

    problems with strcpy

    I'm trying to copy a recieved char-array (cur) into cur2. Cur is a line read by fgets from user-input.

    I need a copy because cur is later divided into words, but cur2 should hold the entire line. A simplified code-snippet:
    Code:
    #define MAX 20000
    #define LINE 120
    #define WORDS 20
    
    void run (char cur[]) 
    {
        char *param[MAX];
        char *temp[MAX];
        char cur2[WORDS];
        int s;
    
        cur[strlen(cur)-1] = '\0';
        strcpy(cur2, cur);
        temp[s] = cur2;
        s++;
    }
    
    int main (void)
    {
      char *cur;
    while (1) {
        printf("input >");
        fflush(stdout);
        cur = malloc(LINE);
        if (fgets(cur,LINE,stdin) == NULL) {
          free(cur);  
          break;
        }
        if (n>=MAX) {
          printf("buffer overflow..\n");  
          exit(0);
        }
        run(cur);
      }
      exit(0);
    }
    The idea is that temp[] should hold each input-line, but somehow every element is replaced by the input .

    If input is:
    > pwd
    > ls -la
    > h

    then temp[0], temp[1], and temp[2] now contains 'h'. Any help would be appreciated!!

  2. #2
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Your code doesn't compile without errors.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    s is uninitialised in your run() function, so setting temp[s] = cur2 will yield undefined behaviour. Also, in main(), you are calling malloc() every time through the loop and only freeing the result when fgets() returns NULL (on end of file or error) => memory leak. The variable n in main() is not declared, so the code shouldn't compile. That's just on a quick look; I haven't dug any deeper.

  4. #4
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Code:
    void run (char cur[]) 
    {
      char *param[MAX]; // this is like saying, 
                        // I want to have a pointer to a string
                        //  are you sure you know what you are doing?
    
      char *temp[MAX]; // see above
      char cur2[WORDS];
      int s; // uninitialized
    
      cur[strlen(cur)-1] = '\0';
      strcpy(cur2, cur); // maybe use strncpy? strncpy(cur2, cur, strlen(cur));
      temp[s] = cur2; 
      s++; // erh... what for?
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Its only a snippet of a bigger system, pasting the whole code here would be too much... the code-snippet shouldnt compile... Thanks for the quick replies though. Strncpy did the trick! Thanks yxunomei

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    With strncpy() you need to terminate the string yourself. From http://www.cplusplus.com/ref/cstring/strncpy.html
    Code:
    /* strncpy example */
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char str1[]= "To be or not to be";
      char str2[6];
      strncpy (str2,str1,5);
      str2[5]='\0';
      puts (str2);
      return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Thanks for all the help! It solved my errors

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by andor
    Your code doesn't compile without errors.
    Of course it doesn't. You need to include files
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat_s problems
    By Aisthesis in forum C++ Programming
    Replies: 18
    Last Post: 06-02-2009, 09:59 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 05:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM