Olba12
Olba12

Reputation: 335

The difference between arrays in Java and C

In my book there is an example which explains the differences between arrays in Java and C.

In Java we can create an array by writing:

int[] a = new int[5];

This just allocates storage space on the stack for five integers and we can access them exactly as we would have done in Java

int a[5] = {0};
int i;
for (i = 0, i < 5; i++){
    printf("%2d: %7d\n", i, a[i]);
}

Then the author says the following

Of course our program should not use a number 5 as we did on several places in the example, instead we use a constant. We can use the C preprocessor to do this:

#define SIZE 5

What are advantages of defining a constant SIZE 5?

Upvotes: 3

Views: 1901

Answers (5)

SergeyA
SergeyA

Reputation: 62603

I find comments here to be superflous. As long as you use your constant (5 in this case) only once, it doesn't matter where it is. Moreover, having it in place improves readability. And you certainly do not need to use the constant in more than one place - afterall, you should infer the size of array through sizeof operator anyways. The benefit of sizeof approach is that it works seamlessly with VLAs.

The drawback of global #define (or any other global name) is that it pollutes global namespace. One should understand that global names is a resource to be used conservatively.

Upvotes: 0

Claudio Cortese
Claudio Cortese

Reputation: 1380

Well, imagine that you create a static array of 5 integer just like you did int my_arr [5]; ,you code a whole programm with it, but.. suddenly you realise that maybe you need more space. Imagine that you wrote a code of 6-700 lines, you MUST replace every occurence of you array with the fixed number of your choice. Every for loop, and everything that is related with the size of this array. You can avoid all of this using the preprocessor command #define which will replace every occurence of a "keyword" with the content you want, it's like a synonymous for something. Eg: #define SIZE 5 will replace in your code every occurence of the word SIZE with the value 5.

Upvotes: 0

lost_in_the_source
lost_in_the_source

Reputation: 11237

You should use a constant, because embedding magic numbers in code makes it harder to read and maintain. For instance, if you see 52 in some code, you don't know what it is. However, if you write #define DECKSIZE 52, then whenever you see DECKSIZE, you know exactly what it means. In addition, if you want to change the deck size, say 36 for durak, you could simply change one line, instead of changing every instance throughout the code base.

Upvotes: 3

davmac
davmac

Reputation: 20641

Using a named constant is generally considered good practice because if it is used in multiple places, you only need to change the definition to change the value, rather than change every occurrence - which is error prone.

For example, as mentioned by stark in the comments, it is likely that you'll want to loop over an array. If the size of the array is defined by a named constant called SIZE, then you can use that in the loop bounds. Changing the size of the array then only requires changing the definition of SIZE.

There is also the question of whether #define is really the right solution. To borrow another comment, from Jonathan Leffer: see static const vs #define vs enum for a discussion of different ways of naming constants. While modern C does allow using a variable as an array size specifier, this technically results in a variable-length array which may incur a small overhead.

Upvotes: 4

kinORnirvana
kinORnirvana

Reputation: 1715

#define SIZE 5

This looks like an old outdated way of declaring constants in C code that was popular in dinosaur era. I suppose some lovers of this style are still alive.

The preferred way to declare constants in C languages nowadays is:

const int kSize = 5;

Upvotes: -2

Related Questions