简体   繁体   English

如何在C中找到unsigned char指针的长度?

[英]how to find length of an unsigned char pointer in C?

I want to find size or length of an unsigned char pointer in a function where that pointer is an argument to that function.Near the pointers declaration size is coming correctly. 我想在函数中找到unsigned char指针的大小或长度,其中该指针是该函数的参数。接近指针声明大小正确。

But when i am trying to find size in function it is giving 4. 但是,当我试图找到功能的大小时,它给了4。

How can i do this ? 我怎样才能做到这一点 ?

#include <stdio.h>
//void writeFile(unsigned char *da);
void writeFile(char *da);
int main(int arc,char **argv)
{

 unsigned char block_bmp[]=
{
   0x0,0xff,0xff,0xff,0x0,0x0,0x0,0xff,0xff,0xff,0x0,0x0,0x0,0xff,0xff,0xff,//*16bytes*
};
printf("size::%d\n",sizeof(block_bmp));
writeFile(block_bmp);
}

//void writeFile(unsigned char *da)
void writeFile(char *da)
{
printf("%d\n",__LINE__);
printf("size::%d\n",sizeof(da));
printf("length::%d\n",strlen(da));
FILE *fp;
int i;
fp=fopen("/tmp/hexfile","wb");
for(i=0;i<3780;i++)
    fprintf(fp,"%c",da[i]); 
//  fwrite(da,sizeof(unsigned char),sizeof(da),fp);
fclose(fp);
}

If it points to a NULL-terminated string, use strlen . 如果它指向以NULL结尾的字符串,请使用strlen If not, the pointer is just some memory address for the called function, without any additional information about the size of the array (I assume, you try to pass an array). 如果没有,指针只是被调用函数的一些内存地址,没有关于数组大小的任何附加信息(我假设你试图传递一个数组)。 I suggest passing the number of array elements as additional parameter to the function. 我建议将数组元素的数量作为附加参数传递给函数。

You can not use sizeof in this condition. 在这种情况下你不能使用sizeof You should be using the strlen if the char array is NULL terminated. 如果char数组以NULL结尾,则应该使用strlen

When you pass array to a function, it decays to pointer , you can't use sizeof on this pointer to get the size of the array. 当您将数组传递给函数时,它会衰减到指针 ,您不能在此指针上使用sizeof来获取数组的大小。 The sizeof will give you 4 bytes (assuming 32 bit machine). sizeof将给你4个字节(假设32位机器)。

Example: 例:

#include <stdio.h>
void fun(int myArray[10])
{
    int i = sizeof(myArray);
    printf("Size of myArray = %d\n", i);
}
int main(void)
{
    // Initialize all elements of myArray to 0
    int myArray[10] = {0}; 
    fun(myArray);
    getch();
    return 0;
}
/**************OUTPUT**************
Size of myArray = 4
***********************************/
unsigned char array[100];
// sizeof(array) == 100

unsigned char* ptr = array;
// sizeof(ptr) == 4 for 32 bit platform.

When you call a function such as 当你调用诸如此类的函数时

 foo(unsigned char* ptr)
 {
 }

with 'array' as argument, you only see a 'unsigned char *', not an array with 100 elements. 使用'array'作为参数,您只能看到'unsigned char *',而不是包含100个元素的数组。 That's why 'sizeof' returns 4. 这就是'sizeof'返回4的原因。

pointers size sizeof(any_pointer_variable) will not depend on the datatype to which it is going to point. 指针大小sizeof(any_pointer_variable)将不依赖于它将指向的数据类型。 The size of the pointer is mostly 4 that is what you are getting in function. 指针的大小大多是4,这是你在功能上得到的。 and it doesnt depend on what it points to. 它并不取决于它指向的内容。 all pointers will have same size independent of what type they point to. 所有指针都具有相同的大小,与它们指向的类型无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM