What does it mean int (*a)[10]

D

Dave

Quick Function said:
What does it mean:

int (*a)[10];

a is a pointer to an array of 10 ints. So, for example, a++ will increase a
by the size of 10 ints (40 bytes on most platforms, but I can't speak for
yours).
 
R

Rob Williscroft

Quick Function wrote in
in comp.lang.c++:
What does it mean:

int (*a)[10];

'a' points to an array of 10 intergers:

#include <iostream>

int main()
{
int array[ 10 ];
int (*a)[10] = &array;

for ( int i = 0; i < 10; ++i ) (*a) = i;

for ( int i = 0; i < 10; ++i ) std::cerr << array << '\n';
}



Rob.
 
K

Karl Heinz Buchegger

pvsp said:
for ( int i = 0; i < 10; ++i ) std::cerr << array << '\n';
}



Rob.

sorry, it isn't on topic;
what is difference between cerr and cout ??
thanks,


cerr is the error output stream
cout is the output stream

On some operating systems it is possible to redirect
output to eg a file. This is handy because then
the program by itself doesn't need to deal with files, but
yet can be used to produce a file (by redirecting
its output into a file). But then it is also possible
to redirect the error stream into a different file, such
that you can easily seperate the real output from the
error messages in different files.

cout is also usually buffered while cerr is not.
 
R

Rob Williscroft

pvsp wrote in in
comp.lang.c++:
for ( int i = 0; i < 10; ++i ) std::cerr << array << '\n';
}



Rob.

sorry, it isn't on topic;
what is difference between cerr and cout ??
thanks,


They are different streams which on most systems go to the same place,
i.e. the "console".

However std::cerr, differs from std::cout in that every time a '\n' char
is sent to it, it calls flush(), so the output should be immediatly
visible (or writen to the file if its been redirected).

For std::cout you need to call flush() (std::cout.flush()) or
alternativly use std::endl (std::cout << std::endl) which sends
a '\n' char and then calls flush() all by itself.

Rob.
 
K

Karl Heinz Buchegger

Gernot said:
Richard Herring said:
Mabden said:
int (*a)[10];


'a' points to an array of 10 intergers:

At least eleven? [0...10]

Nope. 0..9
10.5 : you can point at [10] but not access it.

In a release version you can even access it.

You can, but it is undefined behaviour. Just the same
as you would access [11], [12], [13], [14], etc ...
 
G

Gernot Frisch

In a release version you can even access it.
You can, but it is undefined behaviour. Just the same
as you would access [11], [12], [13], [14], etc ...

That's why I'd only try reading - it if at all... :))
 
R

Rob Williscroft

Gernot Frisch wrote in in comp.lang.c++:
In a release version you can even access it.

You can, but it is undefined behaviour. Just the same
as you would access [11], [12], [13], [14], etc ...

That's why I'd only try reading - it if at all... :))

Reading is accessing, reading is UB, don't read (aka access) an object
beyond the end of an array (or any other container for that matter).

If your compiler lets you "Get Away With It(tm)" today it may not
tomorrow, its UB anything can, and by Murphy's Law will, happen.

Rob.
 
R

Ron Natalie

Rob Williscroft said:
However std::cerr, differs from std::cout in that every time a '\n' char
is sent to it, it calls flush(), so the output should be immediatly
visible (or writen to the file if its been redirected).,

Actually, cerr has unitbuf set. Every output operation is flushed.
There is no "linebuffering" mode defined in C++ although some
non-unibuf stream implementations work that way on terminals.
 
R

Rob Williscroft

Ron Natalie wrote in in comp.lang.c++:
Actually, cerr has unitbuf set. Every output operation is flushed.
There is no "linebuffering" mode defined in C++ although some
non-unibuf stream implementations work that way on terminals.

Thanks, this is a detail I should have remembered, its been said here
enough times :).

Rob.
 

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,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top