int to char and back

This is a discussion on int to char and back within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to add a int to the first four bytes of a char * and then later on ...

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

    int to char and back

    Hi,

    I'm trying to add a int to the first four bytes of a char * and then later on I have to extract the int from the char *.
    Example

    char *foo = " number";
    int num = 123456;
    memcpy(foo,&num,4);


    .........

    int num2;
    memcpy(num2,foo,4);


    But this seems to segfault. What am I doing wrong?

    hashbang

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In this example, at least, foo is not writable memory (by you). Does it work if you use char foo[] instead?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    So using malloc should work? The actuall pinter is malloc'd memory.

    int a,b;
    char *foo = malloc(sizeof(char) * 100);
    int num = 123456;
    memcpy(foo,&num,4);

    memcpy(a,foo,4);
    b = atoi(a);
    printf("%d\n", b);
    return 0;

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And how do you expect memcpy(a, foo, 4) to work? Do you notice how your compiler complains bitterly about that line?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Yes it does, but I am not sure what I am missing.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    a is not a memory address.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    ah...


    memcpy(&a,foo,4);

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Right. But somehow I don't think you're gonna get what you wanted. Mixing binary and ASCII like that. or maybe it is.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    you might be right as it does not seem too work.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What happens if, instead of printf("%d", b) you do printf("%d",a)?

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    that works. Thanks. I thought atoi would be needed after storing it in the char *.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Storing it in a char * isn't magic -- or, more to the point, it isn't sprintf.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Why should string literals be const? Answer: http://cpwiki.sourceforge.net/Common...kes_and_errors
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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