Malcolm McLean said:
Keith Thompson said:
Several of us have already posted a much simpler solution to the OP's
problem: sizeof arr_List / sizeof arr_List[0]. What advantage does
your solution have over that?
aar_List needs to be in scope for that to work. You just ned the pointer for
my method.
Not true. Here's your method again:
| char *list = "34, 12, 67, 34";
|
| int *l = makelist(list, &N);
|
| int *makelist(const char *initialisers, int *N)
| {
| /* a bit of logic here to call malloc(), and atoi() to convert
| your list */
| }
Creating the array gives you a pointer to the first element of
the array and the length of the array. Given just the pointer,
there's still no way to determine the length of the array unless
you explicitly pass the length along with the pointer.
Unless you're talking about reconstructing the int array from the
string every time you use it. You're using a character string
to represent an int array, just for the sake of getting the '\0'
sentinal value.
Or:
int arr_List[] = { 34, 12, 67, 34 };
const size_t arr_List_size = sizeof arr_List / sizeof arr_List[0];
Again, you have a pointer to the first element of the array (the
result of evaluating the array expression ``arr_List'', and a
declared constant holding the length of the array. Use program logic,
perhaps a struct, to keep them associated with each other.
The only things your method gains are obfuscation and inefficiency.