开发者

C Noob: Define size of a char array while copying contents of char* into it

I have a basic question in C.

I need to print the contents of a char pointer. The co开发者_高级运维ntents are binary and therefore I use hex format to see the contents.

Would detecting a null still work?

unsigned char *input = "������";
printf("input =");
int count = 0;
while(*input != '\0'){
    printf("%02x", *input);
            input++;
}
printf("\n");

Now what happens if I have to copy the pointer to a char array? How can I assign the size of the char array? I understand sizeof returns only the size of datatype that char points to. But is there any way?

unsigned char copyInput[size??];
strcpy(copyInput, input);
for (i=0, i <size?, i++)
{
printf("copyInput[%d]= %02x", i, copyInput[i]);
}

Thanks in advance!


1) To the extent that C has strings at all, they are defined as "an arbitrary contiguous sequence of nonzero bytes, terminated with a zero byte". Therefore, if your binary data is guaranteed never to contain bytes whose value is zero, you can safely treat it as a C string (use the str* functions with it, etc). But if your binary data might have zero bytes somewhere in the middle, you need to track the length separately and operate on it with the mem* functions instead.

2) You use strlen to find the length of the string (without the terminating zero byte). However, in standard C89 you can't use the result of strlen to set the size of a char[] variable, because the size has to be known at compile time. If you're using C99 or GNU extensions, you can define the size of an array at runtime:

size_t n = strlen(s1);
char s2[n+1];
memcpy(s2, s1, n);

The n+1 is necessary, or you won't have space for the terminating NUL. If you can't use C99 nor GNU extensions, your only option is to allocate space on the heap:

size_t n = strlen(s1);
char *s2 = malloc(n+1);
memcpy(s2, s1, n);

or, with a common library extension, just

char *s2 = strdup(s1);

Either way, don't forget to free(s2) later. By the way, this is a case where it would have been safe to use strcpy, because you know by construction that the destination buffer is big enough. I used memcpy because it may be slightly more efficient and it means human readers won't see "strcpy" and start worrying.


If it's a bunch of chars terminated with a 0, just use strlen() since that is C's definition of a string. It doesn't matter than some (or most) of the characters might be unprintable, as long as 0 is the terminator.


You will have problems if any of the input bytes are 0. In this case the loop will stop at that character. Otherwise, you can treat it as a string.

Treating it as a string, you can use strlen() to get the input's size and then dynamically allocate memory to your copy. The copy can be made with strcpy as you did, but it is safer to use strncpy.

char *input = "input binary array";
int count = strlen(input)+1; // plus '\0'
char *copy = (char *) malloc(count*sizeof(char));
strncpy(copy, input, count+1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜