Reputation: 501
I have an issue with C strings. I have array char char_array[256] = {0}
.
I had fulfilled this array with char elements. When I print these elements printf("%c", char_array[0]);
, printf("%c", char_array[1]);
and etc, the values are correct. But when I try to print printf("%s", char_array);
, it prints nothing...
Also I have function void function(char* char_pointer)
. I am trying to return data strcpy(char_pointer, char_array)
. But it returns nothing...
Please, help me, how to return data from function, that later, outside function, I could print printf("%s", char_pointer_from_function)
?
UPDATE:
Function:
void unmarshal_packet(char *packet)
{
char packet_size_string [10] = {0};
char unmarshaled_data [2000] = {0};
int counter = 0;
while (packet[counter] != ':' && counter < strlen(packet))
{
packet_size_string[counter] = packet[counter];
counter++;
}
counter = strlen(packet_size_string) + 1;
for (counter; counter < strlen(packet); counter++)
{
if ((packet[counter] != '\r') && (packet[counter] != '\n'))
{
unmarshaled_data[counter - (strlen(packet_size_string) + 1)] = packet[counter];
}
}
// printf("%c", unmarshaled_data[0]);
// printf("%c", unmarshaled_data[1]);
// printf("%c", unmarshaled_data[2]);
// printf("%c", unmarshaled_data[3]);
// printf("%c", unmarshaled_data[4]);
// printf("%c", unmarshaled_data[5]);
// printf("%c", unmarshaled_data[6]);
// printf("%c", unmarshaled_data[7]);
// printf("%c", unmarshaled_data[8]);
// printf("%c", unmarshaled_data[9]);
printf("%s", unmarshaled_data);
unmarshaled_data[counter - (strlen(packet_size_string) + 1)] = '\0';
strcpy(packet, unmarshaled_data);
}
Function call:
strcpy(buffer, " \nWellcome to Yes / No game server! \n");
unmarshal_packet(buffer);
Upvotes: 0
Views: 109
Reputation: 49805
Strings are sequences of non-NULL (ASCII value 0) characters, followed by such a character indicating the end of the string. If, for example, your first character where a NULL, that would represent an empty string, and so nothing would be printed. Not knowing what you put in your array before printing, I cannot say what should be printed.
Upvotes: 1