0

I have an array of strings which is char**. It has 4 elements, and when I try to find the number of elements using sizeof(array) / sizeof(array[0]), it always return 1 which leads to for loop unable to loop through all the elements in the char**.

char** departmentList;

departmentList = malloc(sizeof(*departmentList)); // allocate

getAllDepartments(departmentList, recordsPtr[0], numOfDepartments);

printf("%d",sizeof(departmentList)/sizeof(departmentList[0])); // return 1

printf("%d",sizeof(departmentList[0])); // return 8 

sizeof(departmentList) yields 8 too.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
CloudWave
  • 913
  • 9
  • 16
  • 6
    You cannot get the size of a pointed to array from a pointer. You need to store the length explicitly somewhere – UnholySheep Feb 04 '21 at 16:44
  • ...or store flag value NULL as the final element of the array – fukanchik Feb 04 '21 at 16:44
  • dupe of [How can I find the number of elements in an array?](https://stackoverflow.com/questions/10290610/how-can-i-find-the-number-of-elements-in-an-array) or https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c or etc. – underscore_d Feb 04 '21 at 16:49

1 Answers1

0

You should call a malloc explicitly to make sure the compiler knows the length of each "string". You allocated just the pointer which points to another pointer...