Stroustrup 5.3.1, navigating arrays

A

arnuld

Stroustrup says:

"Arrays are not self describing because the number of elements of an
Array is not guarnteed to be stored with Array"

"this implies that o traverse an Array that does not contain a
terminator the way character string do, we must somehow supply the
number of elements"

i don't understand the relation of these 2 statements because if an
Array does not contain a '\0' terminator in the end, it is not an
Array, it is just a data-structure that looks-like an Array but it is
not an Array. Arrays always end in '\0'.

right ?


void fp(char v[], unsigned int size)
{
for(i=0; i<size; i++)
use (v);

const int N=7;
char v2[];

for((int i=0; i<N; i++)
use (v2);
}

so v[] and v2[] are not arrays because they do not contain '\0' in the
end.
 
A

Alf P. Steinbach

* arnuld:
Stroustrup says:

"Arrays are not self describing because the number of elements of an
Array is not guarnteed to be stored with Array"

"this implies that o traverse an Array that does not contain a
terminator the way character string do, we must somehow supply the
number of elements"

i don't understand the relation of these 2 statements because if an
Array does not contain a '\0' terminator in the end, it is not an
Array, it is just a data-structure that looks-like an Array but it is
not an Array. Arrays always end in '\0'.

right ?

No.

An array is a number of elements laid after one another in memory.

The element values can be anything.
 
J

Jacek Dziedzic

arnuld said:
Stroustrup says:

"Arrays are not self describing because the number of elements of an
Array is not guarnteed to be stored with Array"

"this implies that o traverse an Array that does not contain a
terminator the way character string do, we must somehow supply the
number of elements"

i don't understand the relation of these 2 statements because if an
Array does not contain a '\0' terminator in the end, it is not an
Array, it is just a data-structure that looks-like an Array but it is
not an Array. Arrays always end in '\0'.

right ?

Nope. Null-terminated strings, often stored in arrays of char,
end in '\0'. Arrays themselves don't.

HTH,
- J.
 
B

Bo Persson

arnuld wrote:
:: Stroustrup says:
::
:: "Arrays are not self describing because the number of elements of an
:: Array is not guarnteed to be stored with Array"
::
:: "this implies that o traverse an Array that does not contain a
:: terminator the way character string do, we must somehow supply the
:: number of elements"
::
:: i don't understand the relation of these 2 statements because if an
:: Array does not contain a '\0' terminator in the end, it is not an
:: Array, it is just a data-structure that looks-like an Array but it is
:: not an Array. Arrays always end in '\0'.
::
:: right ?
::

No.

He means that to use an array properly, you must either know the number of
elements (somehow), or it must have a special terminator that you can
recognize when you reach it. C-style strings use '\0' as a terminator. Other
arrays in general do not.


In C++ you can use std::string and std::vector to avoid this problem. They
know their own sizes all the time, and you can ask them for it.


Bo Persson
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top