Initializing a list

T

Tagore

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?
 
K

Keith Thompson

Tagore said:
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);
 
I

Ian Collins

Keith said:
Tagore said:
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>
 
N

Nick Keighley

  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?
 
F

Flash Gordon

Malcolm said:
Tagore said:
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.

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

Keith Thompson

Malcolm McLean said:
Tagore said:
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.

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?
 
K

Keith Thompson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top