To implement an array of C + + from non-zero integers using a macro

Source: Internet
Author: User

I believe that when we first learned C + +, will be the array subscript starting from the 0 numbering there doubts. Although I like to start numbering "0 party" from 0, there are a lot of people who like to start numbering from 1.

Having realized the similarity between C + + arrays and pointers, I began to conceive of how to mimic Pascal to implement a declarative approach similar to array[1..100].

Today, after discussion with the great God Gjs, he gives the first implementation plan:

int buf[]; int a[-ten;    // equivalent to a = &b[10]

The goal is to access a[0] as the equivalent of accessing b[10], so a[-10] is equivalent to access b[0], so that we can access the BUF by accessing a, which is equivalent to getting an array of length 100, starting from 10.

Unfortunately, the compilation does not pass. It is said that arrays cannot be left values.

So we use pointers instead.

  

int buf[]; int Ten ; a[-33;

Compilation succeeded.

We decided to rewrite it as a macro.

#define Array (type, name, LB, ub) type buf[ub-lb + 1]; type* name = buf-lb

In this way, we can use array (int, ABC,-5, 5) to declare an "array" with a subscript range of 5 to 5.

There seems to be no problem.

However, it is problematic to encounter multiple array declarations:

int Main () {    array (int1);    Array (int, B,-11);}

The compiler protested:

Ten:5'int buf [3]'
9:5: Error: ' BUF ' has a previous declaration as ' int BUF [100] '

This is caused by the use of two temporary variables. Expand Definition:

int buf[11int1; int buf[1 --11int* b = buf--1;

Two buf! were declared

To solve this problem, we use this method: renaming.

This is the new array declaration:

  

#define Array (type, name, LB, ub) type name# #buf [ub-lb + 1]; type* name = name# #buf-lb

Notice the # #号了吗? This is the meaning of the connection. In this way, they begin to get the following:

int abuf[11int1; int bbuf[1 --11int* b = bbuf--1;

Of course, this does not guarantee that no bugs are available. For example, I declare a char abuf[30].

The best way to do this is to make it more complicated, add a lot of random characters, and then add the file name and number of rows to minimize the likelihood of a conflict. Of course, we can't stop people who deliberately die.

OK, now use is a bit of a problem. But using memset ...

You might say, "No, it's not memset (a, 0, sizeof a), but with memset (a + lb, 0, sizeof a). ”

That's not right. We declare a is a pointer, and sizeof A's result is generally 4, because the pointer size is 4. This fails to achieve the result of the initialization.

GJS: If you are not too troublesome, you can write a macro, the original variable name back, and then ...

I'm not doing it.

  

To implement an array of C + + from non-zero integers using a macro

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.