Error in copying of charachters

This is a discussion on Error in copying of charachters within the C Programming forums, part of the General Programming Boards category; Dear! char *mov_dx; int decpnt, sign; char *premier; mov_dx = ecvt(xor_a, 5, &decpnt, &sign); /*till here everything OK*/ /* the ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    Katy, Texas
    Posts
    2,309
    vart means you never initialized premier. It is defined as a pointer, but does not point to anything.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    for example declare premier as
    char premier[5];

    to allocate 5 bytes in this buffer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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, 05:09 PM
  2. Deep and Shallow Copying
    By peckitt99 in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2007, 10: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, 11:24 AM

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