Pregunta

I'm trying to pass a char array to another function but some of the chars may be \0. The array is output of 'xor' of two strings, and therefore an example of \0 could be: \0 = 'A' ^ 'A'

If I for example try sending arr[3] = {\0,1,2} to func foo:

void foo(char* c){
...

}

Then c will be equal to "" .

What would be the correct way to do it in C?

Edit: I eventually solved it using @AntonH and @Cody grey tips, I sent the size of the string I wanted to copy and used it in my foo function.

for example for concatenating my string in function:

void catMsg(char* dest, char* source, int sourceSize)
{
    int i,destLen;
    destLen= strlen(dest) ;
    for(i=0;i<sourceSize-1;i++)
        dest[destLen+i] = source[i];
    dest[destLen+i] = '\0';
}
¿Fue útil?

Solución

To summarise mine and @cody-gray's answer.

The null string terminator is only applicable if you're using it as a string.

If you're using it as an array of char, then it's a value like any other.

To be able to use it, you will need to pass the size of the array as an additional parametre, and loop through the array using the size, rather than according to any terminator.

void foo(char* c, int size){
    ...
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top