Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initializing a list

0 views
Skip to first unread message

Tagore

unread,
Dec 10, 2009, 7:30:22 PM12/10/09
to
hi,
If I want to initialize a list of numbers to be used in a program.
like

const int arr_List[]={34,12,67,34};

but t I have to also declare its size explicitly.
const int size=4;

Now problem is that every-time I run my program with new contents in
arr_List, I have to also change value of size..
Is there any other way for it?

bartc

unread,
Dec 10, 2009, 7:50:27 PM12/10/09
to

"Tagore" <c.lang...@gmail.com> wrote in message
news:15d00c0a-a015-4299...@j9g2000prh.googlegroups.com...

const int size=sizeof(arr_List)/sizeof(arr_List[0]);

--
bartc

Keith Thompson

unread,
Dec 10, 2009, 7:52:10 PM12/10/09
to

const int size = sizeof arr_List / sizeof arr_List[0];

You might want to wrap this in a macro:

#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr)[0])
...
const int size = ARRAY_SIZE(arr_List);

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ian Collins

unread,
Dec 11, 2009, 12:23:01 AM12/11/09
to
Keith Thompson wrote:
> Tagore <c.lang...@gmail.com> writes:
>> If I want to initialize a list of numbers to be used in a program.
>> like
>>
>> const int arr_List[]={34,12,67,34};
>>
>> but t I have to also declare its size explicitly.
>> const int size=4;
>>
>> Now problem is that every-time I run my program with new contents in
>> arr_List, I have to also change value of size..
>> Is there any other way for it?
>
> const int size = sizeof arr_List / sizeof arr_List[0];
>
> You might want to wrap this in a macro:
>
> #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr)[0])
> ....

> const int size = ARRAY_SIZE(arr_List);

<pedantry>
These should be const size_t
</pedantry>

--
Ian Collins

Nick Keighley

unread,
Dec 11, 2009, 4:22:14 AM12/11/09
to
On 11 Dec, 00:30, Tagore <c.lang.mys...@gmail.com> wrote:

>   If I want to initialize a list of numbers to be used in a program.
> like
>
> const int arr_List[]={34,12,67,34};
>
> but t I have to also declare its size explicitly.
> const int size=4;

why? Could you use the ARRAY_SIZE macro suggested by other posters?

Malcolm McLean

unread,
Dec 12, 2009, 1:31:54 PM12/12/09
to
"Tagore" <c.lang...@gmail.com> wrote in message
>
> const int arr_List[]={34,12,67,34};
>
> but t I have to also declare its size explicitly.
> const int size=4;
>
> Now problem is that every-time I run my program with new contents in
> arr_List, I have to also change value of size..
> Is there any other way for it?
>
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
*/
}

it's a bit of a round about way of doing it, but if it's a nuisance to keep
updating a lot of lists each time, it might be worth the effort.


Malcolm McLean

unread,
Dec 12, 2009, 1:32:54 PM12/12/09
to

"Ian Collins" <ian-...@hotmail.com> wrote in message
See the evil.


Keith Thompson

unread,
Dec 12, 2009, 2:28:29 PM12/12/09
to

Huh?

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?

Flash Gordon

unread,
Dec 12, 2009, 2:25:16 PM12/12/09
to

I can't see why one would go to that effort when there are the simpler
methods using sizeof.
--
Flash Gordon

Malcolm McLean

unread,
Dec 12, 2009, 5:13:33 PM12/12/09
to
"Keith Thompson" <ks...@mib.org> wrote in message

>
> 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.


Keith Thompson

unread,
Dec 12, 2009, 10:33:53 PM12/12/09
to

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.

Malcolm McLean

unread,
Dec 13, 2009, 7:44:09 AM12/13/09
to

"Keith Thompson" <ks...@mib.org> wrote in message
>
> The only things your method gains are obfuscation and inefficiency.
>
The other advantage is that if you move to read in the integer array from a
file, it's just a getline() call followed by a call to the conversion
function.


0 new messages