3

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.

But when i am trying to find size in function it is giving 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);
}
QtUser
  • 185
  • 2
  • 2
  • 12

4 Answers4

4

If it points to a NULL-terminated string, use 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.

Callidior
  • 2,899
  • 2
  • 18
  • 28
  • Thanks for your suggestion. I cannot ask user to pass number of array elements . I should write a method so that user can pass Unsigned char pointer as an argument to my method. In my method i have to find its length. – QtUser Apr 03 '14 at 06:53
  • 5
    @QtUser: If it's a string, then you have a solution. If it's not a string (i.e. not null-terminated), then there is no solution. – Oliver Charlesworth Apr 03 '14 at 06:55
  • Nitpicking: C-"strings" are either `NUL`- or `0`- or `null`-terminated. `NULL` is something different. – alk Apr 03 '14 at 11:04
3

You can not use sizeof in this condition. You should be using the strlen if the char array is NULL terminated.

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. The sizeof will give you 4 bytes (assuming 32 bit machine).

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
***********************************/
1
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. That's why 'sizeof' returns 4.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

pointers size sizeof(any_pointer_variable) will not depend on the datatype to which it is going to point. The size of the pointer is mostly 4 that is what you are getting in function. and it doesnt depend on what it points to. all pointers will have same size independent of what type they point to.

LearningC
  • 3,182
  • 1
  • 12
  • 19