Hello,

My program looks something like this:

char *name= (char *)malloc(200);

unsigned char type=1;

unsigned char GetType(){
    return type;
}

char* GetName(){ 
    return name;
};


void function(unsigned char* message, int* intPtr){

    message[0]= GetType(); // works correctly
    
    // CODE HERE    

}



void main(){

unsigned char *message= (unsigned char *) malloc(256);

int *ptrInt = (int *)malloc(sizeof(int));

function(message, ptrInt);

printf("message[0]= %d",message[0]); // works correctly

}

I have a pointer to an array of 256 unsigned chars. I am parsing that pointer as a parameter to function(message, intPtr).

Inside function(), I want to load the first byte from my array ( message[0]) with an integer from 0-255 (works).

Next, I want the bytes from 1... strlen(name) to store the name. How can I do that?

For instance, I've tried:

void function(unsigned char* message, int* intPtr){

		char *xxx=GetName(); //or even

                char *xxx; xxx=GetName();

		printf("\nGetName: \n");
		
		while (*xxx)
			printf("%c",*xxx++);

		memcpy(message+1, GetName(), strlen(GetName());

}

Ideas, please?

Recommended Answers

All 3 Replies

why are you calling GetName() so many times??? And for what purpose is the second parameter to that function?

char* xxx = GetName();
if( xxx != NULL)
{
   printf("%s\n", xxx);
   memcpy(message+1,xxx,strlen(xxx));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.