arrays - where they finish?

O

Olaf \El Blanco\

I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)
Etc...


So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???

(I want to use the minimun possible variables)
 
V

Vladimir S. Oka

Olaf "El Blanco" opined:
I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)
Etc...


So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???

(I want to use the minimun possible variables)

You can't, in general case.

If your arrays are terminated with a specific value, like strings are
with '\0' in C, you could traverse them until reaching that value.

If they were declared like:

int array[SOME_SIZE];

you could get their size like this:

sizeof array / sizeof array[0] ( == SOME_SIZE)

There is no other way in C.
 
O

Olaf \El Blanco\

Vladimir S. Oka said:
Olaf "El Blanco" opined:
I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)
Etc...


So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???

(I want to use the minimun possible variables)

You can't, in general case.

If your arrays are terminated with a specific value, like strings are
with '\0' in C, you could traverse them until reaching that value.

If they were declared like:

int array[SOME_SIZE];

No, is declared like:
int *array

So, If I want to print all the array, I can't???
 
S

santosh

Olaf said:
I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)
Etc...


So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???

(I want to use the minimun possible variables)

Your question is not clear. If you want to know the number of elements
in a fixed size array then sizeof(array)/sizeof(array[0]) should yield
the answer. If it a dynamically allocated array, then you'll have to
keep track of it's size, (and hence the number of elements it has)
yourself. For example if *array is of type pointer to long and it's
initialised to point to the start of an array of N bytes then array
points to N/sizeof *array elements that you can access.

Hope this helps somewhat.
 
S

santosh

Olaf said:
Vladimir S. Oka said:
Olaf "El Blanco" opined:
I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)
Etc...


So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???

(I want to use the minimun possible variables)

You can't, in general case.

If your arrays are terminated with a specific value, like strings are
with '\0' in C, you could traverse them until reaching that value.

If they were declared like:

int array[SOME_SIZE];

No, is declared like:
int *array

So, If I want to print all the array, I can't???

'array' as shown above is merely a variable of type pointer to int. It
will not point to any legally accessible location unless you initialise
it somehow, either via malloc() or assigning the result of the &
operator on some object. How big that object or region of memory is, is
completely upto you to know and keep track of. C has no provisions for
automatically doing it for you. If you don't know that a pointer won't
automatically point to a legal range of memory, you should pick up a
good book on C and start at the beginning.
 
K

Kenneth Brody

Olaf said:
Vladimir S. Oka said:
Olaf "El Blanco" opined:
I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)
[...]
You can't, in general case.

If your arrays are terminated with a specific value, like strings are
with '\0' in C, you could traverse them until reaching that value.

If they were declared like:

int array[SOME_SIZE];

No, is declared like:
int *array

So, If I want to print all the array, I can't???

Not unless something else has stored/passed you the length, or you have
agreed that some "magic value" is stored in the last entry. (For example,
in an array of "char*"s, you might add a NULL on the end.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Olaf \"El Blanco\" said:
I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)

"int *array" declare int as a pointer, not as an array; similarly for
"char *array". If you have a pointer to the first element of an array
(which is a very common idiom), there's no direct way to compute the
size of the array it points to.

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers". If you still have questions after that, feel
free to come back and ask them.
 
J

John F

Olaf "El Blanco" said:
Vladimir S. Oka said:
Olaf "El Blanco" opined:
I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)
Etc...


So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???

(I want to use the minimun possible variables)

You can't, in general case.

If your arrays are terminated with a specific value, like strings
are
with '\0' in C, you could traverse them until reaching that value.

If they were declared like:

int array[SOME_SIZE];

No, is declared like:
int *array

So, If I want to print all the array, I can't???

You will need to "remember" the number of elements... like

int array_el_cnt=...;

regards
John
 
J

John Bode

Olaf said:
I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)
Etc...


So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???

(I want to use the minimun possible variables)

If you have the array definition in scope:

int a[10];
size_t asize;
...
/*
* Divide the total number of bytes used by a
* by the number of bytes used in a single
* element of a
*/
asize = sizeof a / sizeof a[0];

asize contains the number of elements (10) in the array.

Note that this *will not* work if the array is passed as a function
parameter; because of how C treats arrays, what actually gets passed is
a pointer to the first element of the function. If you're passing an
array to a function, you must also pass the number of elements in the
array as a separate parameter.

One workaround for this is to wrap an array in a struct type and pass
the struct as the parameter; it's not as convenient as it sounds,
however.

If you've declared the array dynamically through malloc() or calloc(),
you have to keep track of the number of elements you allocated.
 
O

Olaf \El Blanco\

THANKS and sorry!
Is just I saw some really small codes and I want to "copy" the style...
But I understand... My questions was ridiculous!
int *array
Of course we don't know how many integers are in that part of the memory...
just go up and check which number appear in malloc!

Thanks again.
 
A

August Karlstrom

Olaf said:
THANKS and sorry!
Is just I saw some really small codes and I want to "copy" the style...
But I understand... My questions was ridiculous!

Not at all. Many languages keep track of the length of arrays, even
dynamically allocated ones.
int *array
Of course we don't know how many integers are in that part of the memory...
just go up and check which number appear in malloc!

Thanks again.


August
 

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,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top