Thread: Accessing variables with memory address

  1. #46
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    I fail again. This time it's with C strings. Important code:

    Code:
    void *test_str = malloc(17);
    *(int*)test_str = 3;
    *(char*)((int*)test_str+1) = "Hello world!"; /* problem line */
    test = (int)test_str;
    printf("the value of test = %c, the type of test = %i\n\n", *(const char*)((int*)test+1), *((int*)test));
    Error message on compile

    error C2440: '=' : cannot convert from 'const char [13]' to 'char'

    I can't seem to figure it out, any help?

  2. #47
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Surely you've learned by now that you can't assign to arrays. You can assign to specific array elements, etc., but not to arrays-as-a-whole. There is such a thing as strcpy, after all.

  3. #48
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Technically I'm assigning an array to a typeless block of memory. What I need is just a bulk memory copy function, would memccpy work or is there a better one?

  4. #49
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ITAmember View Post
    Technically I'm assigning an array to a typeless block of memory. What I need is just a bulk memory copy function, would memccpy work or is there a better one?
    No you're assigning an array to <<one slot>> of what would be your destination array, and surprisingly your compiler thinks it won't fit. To copy a bunch of memory you need memcpy/strcpy.

  5. #50
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    It at least assigns the data now, I just can't print the whole string. New code:

    Code:
    memcpy((void*)((int*)test_str+1), "Hello world!", 13);
    printf("the value of test = %s, the type of test = %i\n\n", /* this part kills the program */ *(const char*)((int*)test+1), *((int*)test));
    As you can see by the comment it's in the print section. If I change the %s to a %c it prints "H". Any ideas how to get it to print the whole string?

  6. #51
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there any particular reason you're making everything much harder than you need to?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #52
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Depends, do you have an easier method to implement dynamic typing? This will all be generated code anyway, I'm just trying to figure out how to get it to work.

  8. #53
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %s requires a pointer. Don't dereference the pointer, because %s requires a pointer.

  9. #54
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Ahh, that's the problem. Works great now, many thanks. I shouldn't have any more problems now as arrays are work basically the same as these strings.

  10. #55
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    FYI, you are making code that is difficult to read even for experienced programmers.
    First of all, such manipulation is often never good and I would avoid it if I could.
    And secondly, try performing these operations in several steps to make readability easier.
    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. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Printing out the memory address
    By Scalpel78 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2007, 11:01 AM
  4. Changing memory address
    By tzpb8 in forum C Programming
    Replies: 3
    Last Post: 07-26-2006, 09:50 AM
  5. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 07:35 AM