Thread: How fix error? (point arithmetics)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Question How fix error? (point arithmetics)

    code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int
    main()
    {
     /* initial data */
     char *original,*copy;
     original = calloc(sizeof(char),30);
     copy = calloc(sizeof(char),30);
     original = "My original string";
     copy = "It's cheap chines copy :)";
    
     /* output original data */
     printf("Original string: \"%s\"\n",original);
     printf("Copy string: \"%s\"\n\n",copy);
    
     /* do coping strings (copy to original) */
     printf("do coping strings (copy to original)\n\n");
     original = calloc(sizeof(char),30);
     while(*original++ = *copy++);
    
     /* output copies data */
     printf("Original string: \"%s\"\n",original);
     printf("Copy string: \"%s\"\n\n",copy);
    }

    terminal output:
    Code:
    grytskiv@ZXDSL831II:~/arithmetic$ gcc -ansi copy_string.c -o copy_string && ./copy_string
    Original string: "My original string"
    Copy string: "It's cheap chines copy :)"
    
    do coping strings (copy to original)
    
    Original string: ""
    Copy string: "Original string: "%s"
    "
    
    grytskiv@ZXDSL831II:~/arithmetic$




    HOW I CAN GET THIS RESULT IN TERMINAL???
    Code:
    ./copy_string
    Original string: "My original string"
    Copy string: "It's cheap chines copy :)"
    
    do coping strings (copy to original)
    
    Original string: "It's cheap chines copy :)"
    Copy string: "It's cheap chines copy :)"

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) You cannot asign strings with the = sign in C ... look into strcpy() and memcpy()
    2) By using the = sign you have reassigned the pointers, leaking the memory from the two calloc() calls
    3) Since you are using calloc() on the same variable twice without first using free() your code is leaking memory
    4) Your while() loop at the end is at risk of a runaway since you are not clearly defining an exit
    5) That same while loop leaves the pointers at the first null it finds (i.e. at the end of the string) thus leaking even more memory and risking a buffer overrun.
    Last edited by CommonTater; 03-13-2011 at 11:13 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Not always typing your questions in CAPS would be a bonus.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FIX Protocol: FIX using QuickFix
    By arupsarkar in forum C++ Programming
    Replies: 0
    Last Post: 07-16-2010, 11:08 AM
  2. Replies: 6
    Last Post: 12-06-2009, 06:09 AM
  3. 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, 08:18 PM
  4. compiling my code, i can't fix error
    By howru in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 03:38 AM
  5. C++ code need help to fix
    By McReal in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2007, 02:48 PM

Tags for this Thread