Thread: Error in copying of charachters

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Error in copying of charachters

    Dear!


    char *mov_dx;
    int decpnt, sign;
    char *premier;
    mov_dx = ecvt(xor_a, 5, &decpnt, &sign); /*till here everything OK*/

    /* the following line-code creates some errors */

    for (int v = 0; v < 5; v++) premier[v] = mov_dx[v];

    How can I correct it? I'm working with C in DevC++

    Thanks

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    are you using C99 mode?

    use code tags for posting the code

    you premier is uninitialized pointer pointing to some random location - you cannot write there.

    allocate some buffer (staticaly or dynamically) before you write to the memory
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    I don't know if I am using C99 and how to allocate some buffer... I'm quite new... Can you explain me?

    Thanks

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    vart means you never initialized premier. It is defined as a pointer, but does not point to anything.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read up on malloc/free or use a buffer on the stack instead.
    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.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for example declare premier as
    char premier[5];

    to allocate 5 bytes in this buffer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Actually, I think vart was referring to this:
    Code:
    for (int v = 0; v < 5; v++)
    Declaring variables in for loop initialization sections like that is C99 -- or C++. Try putting "int v;" somewhere near the beginning of a block -- say, the beginning of the enclosing function -- and then using just plain
    Code:
    for(v = 0; v < 5; v++)
    Strangely, though, Dev-C++ usually lets you do that . . . .

    Read up on malloc/free or use a buffer on the stack instead.
    To elaborate: if you declare a pointer, it doesn't point to anything. You can't assign anything to the pointer, because you'll be writing into space that you are not allowed to write to. (In all likelihood.)

    In order to get space to write to, you either need to use stack space and declare an array
    Code:
    char data[80];
    or use heap space and dynamically allocate memory:
    Code:
    char *data = malloc(80);
    /* ... */
    free(data);
    Static arrays do not need to be freed, but they are of a fixed size. Which you use depends on what you need.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying file from one user directory to another
    By Bargi in forum Linux Programming
    Replies: 6
    Last Post: 05-17-2009, 04:09 PM
  2. Deep and Shallow Copying
    By peckitt99 in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2007, 09:37 PM
  3. Copying a whole directory and its content
    By Enira in forum C Programming
    Replies: 4
    Last Post: 03-12-2006, 02:13 PM
  4. Shallow/Deep copying, pointers
    By littleweseth in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 06:36 PM
  5. Copying a file
    By rkjd2 in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2001, 10:24 AM