Question

Why does Edit work. and Edit 2 fail to work like edit 1? Also how to make Edit2 work like Edit?

Edit: Working code from comment:

#include <stdio.h>

int main()
{
    char recBuffer[8024];
    char* temp = (char*)malloc(65536);

    strcpy(recBuffer, "Hello\n");

    int bytesRead = 7;
    memcpy(temp , &recBuffer, bytesRead );

    printf("%s\n", temp);

    return 0;
}

EDIT 2 Why this fails?:

#include <stdio.h> 

void Append(char* b, char data, int len)
{
    memcpy(b , &data, len ); 
}

int main() 
{ 
    int bytesRead = 7; 
    char recBuffer[8024]; 
    char* temp = (char*)malloc(65536); 
    strcpy(recBuffer, "Hello\n"); 
    Append(temp, recBuffer, bytesRead);    
    printf("%s\n", temp); 
    return 0; 
}
Was it helpful?

Solution

Because your function parameter is char not char*. You should make the data parameter char* and pass it in directly to memcpy instead of passing its address.

OTHER TIPS

The second argument of Append is a char, not a char*!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top