D
D
dronreviews2015-03-23 01:07:24
C++ / C#
dronreviews, 2015-03-23 01:07:24

What is the difference between int mas[], int mas[0], int mas[100]?

int mas[100] is an array of 100 elements, but I didn't find mas[], int mas[0] in Google.
Also for char mas[] and char mas[0].

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2015-03-23
@dronreviews

int mas[] is used in three places.
1. When the size is determined by an initializer: int mas[] = { 1, 2, 3, 4, 5 };
2. To specify an "array of indeterminate length" type parameter: double vecLen(double a[], int size) {}
C cannot hardcode the size of an array in a parameter such that larger or smaller is not appropriate; even if you write write double vecLen(double a[3]) {}, still another array will do. C++ sets it like this: double vecLen(double (&a)[3]) {})
3. Prompted by jcmvbkbc , really little is needed: extern int a[].
int mas[0] creates an array of zero length, which, of course, is not needed. Instead, this code is used to overlay a data structure that ends in an array of unknown length onto some buffer in memory - like a pointer: here's an array (C++ doesn't check for out-of-bounds).

struct Packet {
  unsigned short length;
  unsigned char data[0];
}

void processPacket(void* data, unsigned length)
{
   // Простите уж, что перешёл на C++
   const Packet& packet = *reinterpret_cast<Packet*>(data);
   if (length != sizeof(Packet) + packet.length)
     throw std::logic_error("Packet size mismatch");
   for (unsigned i = 0; i < packet.length; ++i) {}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question