![]() |
|
[C] Find length of array of string - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Find length of array of string (/showthread.php?tid=6067000) |
Find length of array of string - SIM-2362662 - Apr 14, 2020 I'm trying to find the length of an array of strings. Let's say I have this : Code string my_str[] = {"a", "b", "c", "d", "e", "f"};In order to get the length of my_str, I can do: Code int n = sizeof(my_str) / sizeof(my_str[0]);And everything works fine. But if I try to do that in a function, it doesn't work. Like this: Code void myfct(string strArr[]) {So I would like to know why this happens and how else I can do this. RE: Find length of array of string - saltycracker - Apr 14, 2020 Well look at the warning my compiler generates? Quote:warning: ‘sizeof’ on array function parameter ‘strArr’ will return size of ‘std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}’ [-Wsizeof-array-argument] Hint... It becomes a pointer when you pass it to the function. If memory serves me here.. You have to pass the array size to the function too. RE: Find length of array of string - akali - Apr 14, 2020 (Apr 14, 2020 11:48 AM)♣ SoFt ♣ Wrote: I'm trying to find the length of an array of strings. sizeof(array) is done at compile time. Otherwise it would give you pointer size. Whenever you're dealing with pointers to arrays, the size needs to be passed as an argument as well. They do not know their own size. RE: Find length of array of string - SIM-2362662 - Apr 14, 2020 (Apr 14, 2020 01:20 PM)akali Wrote: sizeof(array) is done at compile time. Otherwise it would give you pointer size. Whenever you're dealing with pointers to arrays, the size needs to be passed as an argument as well. They do not know their own size. So you're saying there is no way for my function to know the length of the array without me passing it as an argument. Right? RE: Find length of array of string - saltycracker - Apr 14, 2020 (Apr 14, 2020 01:31 PM)♣ SoFt ♣ Wrote: So you're saying there is no way for my function to know the length of the array without me passing it as an argument. Right? You have to pass the array size or have some sort of sentinel value to terminate the array... Like a C string. RE: Find length of array of string - creki - Apr 14, 2020 gay but works RE: Find length of array of string - SIM-2362662 - Apr 14, 2020 (Apr 14, 2020 01:56 PM)creki Wrote: You don't necessarily get empty if you exceed the index, do you? I.think you just get whatever is in the memory RE: Find length of array of string - akali - Apr 14, 2020 (Apr 14, 2020 01:56 PM)creki Wrote: I'm pretty sure the code you posted results in undefined behavior. Because at some point, you're trying to call a method of an object that may not even exist in memory. The compiler might just be doing some trickery there and it's working. But no you shouldn't do that. And the only reason string functions in general don't need a length is becasue they check for a null terminator. (Apr 14, 2020 01:58 PM)♣ SoFt ♣ Wrote: You don't necessarily get empty if you exceed the index, do you? Exactly. His code doesn't make any sense. RE: Find length of array of string - zNinja - Apr 15, 2020 PHP Code RE: Find length of array of string - dracx - Apr 18, 2020 Well am not sure bout those C++ string, butnin C char array, you simply have to loop over the starting address then stop counting the first time you hit a null byte. This is not the case for wide char array tho, each wide char array is composed of two bytes and is terminated by two nill bytes too. Am on mobile so cant properly supply at least some pseudo-code. Google "custom strlen". RE: Find length of array of string - zNinja - Apr 23, 2020 (Apr 18, 2020 07:42 PM)dracx Wrote: Well am not sure bout those C++ string, butnin C char array, you simply have to loop over the starting address then stop counting the first time you hit a null byte. This is not the case for wide char array tho, each wide char array is composed of two bytes and is terminated by two nill bytes too. Am on mobile so cant properly supply at least some pseudo-code. Google "custom strlen". Well, C string(char array) is a little bit tricky, if you have const char array, you can get size easy. For pointer you can not use "null byte" as orientation, since array can contain binary content which can have multiple null bytes. You must track size of array trough code, Like when you reading from a file with C api, you must retrieve size of file then allocate container (char array) and then read. RE: Find length of array of string - dracx - Apr 23, 2020 (Apr 23, 2020 11:28 AM)zNinja Wrote: Well, C string(char array) is a little bit tricky, if you have const char array, you can get size easy. For pointer you can not use "null byte" as orientation, since array can contain binary content which can have multiple null bytes. We we're talkin about string A.K.A. char array, not blob of data, no? |