Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:40:56+00:00 2026-06-18T05:40:56+00:00

Okay, first off I’m new to C and very new to pointers (Java programmer),

  • 0

Okay, first off I’m new to C and very new to pointers (Java programmer), so please be gentle. For this class I’m only allowed to use Syscalls, so no stdio.

This function is receiving as input a C-string as a void*, and another void* that points to the area in memory I need to add it to. For whatever reason it’s not properly getting put into the new location.

*(char*)ptr = (char*)data;

ptr is a void* and is the location in memory I want to write the string to, and data is a void* that is the string I want to write there.

If I print out these two values, I get the correct string when pringing (char*)data, but I just get a garbage character when printing (char*)ptr. If I change the above line to:

*(char*)ptr = *(char*)data;

Then only the first character of the string goes into the memory there.

What am I doing wrong?

Thanks.

Full Source:

#include <stdlib.h>
#include <unistd.h>

#define F_first         1 //First call to function, allocate amount of memory specified by data
#define F_last          2 //last call to function, free memory area
#define F_data_int      3 //void* arg points to integer data
#define F_data_char     4 //void* arg points to character string
#define F_data_float    5 //void* arg points to float data
#define F_print         6 //print the accumulated values in memory

#define Ptr_offset      32 //the amount of extra data I'm setting aside to store info about how much total data is allocated and how much of that area is being used

void* f( int code, void* mem, void* data )
{
    size_t totalSize = 500;
    size_t dataSize = 0;
    size_t availableSize = 0;
    size_t neededSize = 0;
    void* ptr;

    if( code == F_first )
    {

        if( mem != NULL )
        {
            exit(1);
        }

        if( data != NULL )
        {
            totalSize = (size_t)data + Ptr_offset;
            mem = malloc( totalSize );

        }else{
            totalSize += Ptr_offset;
            mem = malloc( totalSize );
        }

        dataSize = 0;
    }
    else if( code == F_last )
    {
        if( mem == NULL )
        {
            exit(1);
        }
        else
        {
            free( mem );
        }

    }
    else if( code == F_data_int )
    {
        dataSize = *(int*)(mem + (Ptr_offset/2));
        totalSize = *(int*)mem;
        availableSize = totalSize - dataSize;
        neededSize = sizeof(*(int*)data);

        if( neededSize >= availableSize )
        {
            totalSize = neededSize * 2;
            mem = realloc(mem, totalSize);
        }

        ptr = mem + Ptr_offset + dataSize;
        *(int*)ptr = *(int*)data;

        ptr = mem + Ptr_offset/2;
        *(int*)ptr = dataSize+sizeof(*(int*)data);

    }
    else if( code == F_data_char )
    {
        dataSize = *(int*)(mem + (Ptr_offset/2));
        totalSize = *(int*)mem;
        availableSize = totalSize - dataSize;
        neededSize = sizeof(*(char*)data);

        if( neededSize >= availableSize )
        {
            totalSize = neededSize * 2;
            mem = realloc(mem, totalSize);
        }

        ptr = mem + Ptr_offset + dataSize;
        strcpy(ptr, data);

        printf("\n chardata: %s \n charptr: %s \n", (char*)data, (char*)ptr);

        ptr = mem + Ptr_offset/2;
        *(int*)ptr = dataSize+sizeof(*(char*)data);

    }else if( code == F_data_float )
    {
        dataSize = *(int*)(mem + (Ptr_offset/2));
        totalSize = *(int*)mem;
        availableSize = totalSize - dataSize;
        neededSize = sizeof(*(float*)data);

        if( neededSize >= availableSize )
        {
            totalSize = neededSize * 2;
            mem = realloc(mem, totalSize);
        }

        ptr = mem + Ptr_offset + dataSize;
        *(float*)ptr = *(float*)data;

        ptr = mem + Ptr_offset/2;
        *(int*)ptr = dataSize+sizeof(*(float*)data);

    }else if( code == F_print )
    {
        ptr = mem + Ptr_offset/2;
        write(1, ptr, *(int*)mem);
    }

    if(mem != NULL)
    {
        *(int*)mem = totalSize;
    }

    return mem;
}

int main( void )
{
    int i_a;
    float f_a;

    void* m;
    int* i;
    float* fl;

    fl = &f_a;
    i = &i_a;

    m = f( F_first, 0, (void*)300 );
    m = f( F_data_char, m, (void*)"Systems programming class has " );

    f_a = 69.7;
    m = f( F_data_float, m, (void*)fl );
    m = f( F_data_char, m, (void*)"registered " );
    m = f( F_data_char, m, (void*)"students in a " );
    m = f( F_data_char, m, (void*)"classroom of " );

    i_a = 70;
    m = f( F_data_int, m, (void*)i );
    m = f( F_data_char, m, (void*)"\n" );

    m = f( F_print, m, 0 );

    m = f(F_last, m, 0);

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:40:57+00:00Added an answer on June 18, 2026 at 5:40 am

    Use strcpy or its safer variant strncpy to copy a string. You cannot assign arrays in C.

    strcpy(ptr, data);
    

    Regarding your code:

    *(char*)ptr = (char*)data;
    

    This is not valid C, the left side of the = operator has type char and the right side has type char *. You cannot assign a char * to a char.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, first off I am very, very new to java. For this project I
Okay so first off, Im pretty new to programming, Ive only read a bit
OKay first off this is really really similiar to the http://dribbble.com homepage. In the
Okay, first off, i started (trying) to learn Java about 2 days ago. im
first off I'm very new to rails - I'm playing about with a little
Okay, so first off, I'm fairly new to web design. But for a project
Okay To start off this is my first post so I apologize initially for
First off, I'm using XCode 4.0.2. Okay, here is my issue. I can build
first, I can't stand Crystal! Okay, that's off my chest... Now, we have an
Okay at first I thought this would be pretty straightforward. But I can't think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.