Software-OK
≡... News | ... Home | ... FAQ | Impressum | Contact | Listed at | Thank you |

  
HOME ► Faq ► FAQ - Windows-CPP-und-C ► ««« »»»

ARRAYSIZE Error creating, fix!


It's simple - ARRAYSIZE Error creating, fix error message: : error C2065: 'ARRAYSIZE' : undeclared identifier!
 


Contents:

1.) ... Fixing the ARRAYSIZE error!
2.) ... Example with ARRAYSIZE in a small C program!
3.) ... What are the disadvantages and advantages of ARRAYSIZE?

1.) Fix the ARRAYSIZE error!



Simply define it yourself if it is not defined! Especially if you are working in VC6 or others, this is not predefined! ( ... see image-1 )


#ifndef ARRAYSIZE
#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
#endif



(Image-1) ARRAYSIZE Error when creating, fix it yourself!
ARRAYSIZE Error when creating, fix it and define it yourself!


The problem is that the 1989/1990 version of the C standard does not allow mixing declarations and statements within a block. All declarations must appear first, followed by all statements. The 1999 C standard changed this, but Microsoft's C compiler has very little support for post-1990 C standards. I expect that in a future version they may allow mixed declarations and statements, since this is also a C++ feature.


-
▲ Back to the top ▲


2.) Example with ARRAYSIZE in a small C program!

In this code, we used the ARRAYSIZE macro to determine the size of the array myArray, and then we used a loop to iterate over the elements of the array. The comments explain what each part of the code does.


#include <iostream>
 
// Definition of the ARRAYSIZE macro
#ifndef ARRAYSIZE
#define ARRAYSIZE(A) (sizeof(A) / sizeof((A)[0]))
#endif
 
int main()
{
 // An example integer array
 int myArray[] = {1, 2, 3, 4, 5};
 
 // Use the ARRAYSIZE macro to determine the size of the array
 int size = ARRAYSIZE(myArray);
 
 // Output the size of the array
 std::cout << "The size of the array is: " << size << std::endl;
 
 // Iterate over the array using the determined size
 for (int i = 0; i < size; ++i)
 {
  std::cout << "Element " << i << ": " << myArray[i] << std::endl;
 }
 
 return 0;
}


3.) What are the advantages and disadvantages of ARRAYSIZE?

The ARRAYSIZE macro has both advantages and disadvantages:

Advantages:

Automatic calculation of array size: ARRAYSIZE allows you to automatically calculate the size of an array without having to set it manually. This makes the code more maintainable because changes in the array size are automatically taken into account.

Avoid hard coding: The macro avoids hard coding constant values, which reduces errors. When you change the size of an array, the code adjusts automatically.

Readability and clarity: Using ARRAYSIZE in the code makes the purpose clearer and helps other developers understand that you need the size of the array.

Disadvantages:

No argument type checking: The macro does not check if the argument passed is actually an array. Accidentally passing a variable or pointer can cause unexpected behavior.

Not for dynamically allocated arrays: The macro does not work for dynamically allocated arrays because they can change their size at runtime. It is only suitable for static arrays.

Compiler dependency: Using sizeof in macros can cause problems when porting the code between different compilers or platforms, as the size calculation of data types can vary.

Alternatives in C: There are better alternatives in C, such as using standard containers like std::vector, which already have built-in sizing and other useful features.



Overall, the ARRAYSIZE macro can be very useful in certain cases, especially in C, to determine the size of static arrays. However, one should be aware of the disadvantages mentioned above and make sure that it is suitable for the specific use case. In modern C, alternative approaches such as using std::vector or range-based for loops are usually preferred.





FAQ 21: Updated on: 4 September 2024 10:18 Windows
Windows-CPP-und-C

joyGetPosEx function to query the joystick / controller position!


It is easy to use with joyGetPosEx function to query the joystick / controller position under Windows Required:  Identifier of the joystick to be queried.
Windows-CPP-und-C

Alternative to RegDeleteTree!


You can easily write your own routine for RegDeleteTree, or RegDeleteTreeW and RegDeleteTreeA Alternatively you can also  use SHDeleteKeyA or  SHDeleteKeyW
Windows-CPP-und-C

Replace string for unicode in c++ visual studio!


Often, when transitioning from ANSI to Unicode, you need to replace string literals in your C++ code. This is especially important for making your applications
Windows-CPP-und-C

Difference between _ttof, atof, _atof_l, _wtof, _wtof_l in C++ and C?


Heres the translation and explanation of the differences between _ttof, atof, _atof_l, _wtof, and _wtof_l in C++ and C, focusing on string conversion functions:
Windows-CPP-und-C

Invert color RGB COLORREF InvertColor(COLORREF lColor)!


Heres the translation and explanation of how to invert a color using RGB values and the COLORREF structure with the InvertColor  function: 1. Macros for
Windows-CPP-und-C

Disable / enable MS Visual Studio tabs!


Its easy to disable or re-enable tabs in MS Visual Studio Especially in MS Visual Studio 2008, the tabs cause a window focus problem, but it doesnt hurt
Windows-CPP-und-C

BSCMAKE: error BK1506 : File ... cannot be opened: No such file or directory!


The error message BSCMAKE: error BK1506 : File cannot be opened: No such file or directory Contents: 1. The error message BSCMAKE 2. Other

»»

  My question is not there in the FAQ
Asked questions on this answer:
Keywords: cpp, windows, arraysize, error, creating, simple, message, c2065, undeclared, identifier, contents, fixing, Questions, Answers, Software




  

  + Freeware
  + Order on the PC
  + File management
  + Automation
  + Office Tools
  + PC testing tools
  + Decoration and fun
  + Desktop-Clocks
  + Security

  + SoftwareOK Pages
  + Micro Staff
  + Freeware-1
  + Freeware-2
  + Freeware-3
  + FAQ
  + Downloads

  + Top
  + Desktop-OK
  + The Quad Explorer
  + Don't Sleep
  + Win-Scan-2-PDF
  + Quick-Text-Past
  + Print Folder Tree
  + Find Same Images
  + Experience-Index-OK
  + Font-View-OK


  + Freeware
  + DirPrintOK
  + FontViewOK
  + MeinPlatz
  + DesktopOK
  + IsMyMemoryOK
  + StressTheGPU
  + Brightness.Manager.OK
  + ProcessKO
  + WinBin2Iso
  + PAD-s


Home | Thanks | Contact | Link me | FAQ | Settings | Windows 10 | English-AV | Impressum | Translate | PayPal | PAD-s

 © 2025 by Nenad Hrg softwareok.de • softwareok.com • softwareok.com • softwareok.eu


► Make Windows-10 Standard User to Guest-Account, how to change rights? ◄
► Don't the protected folders in Windows cause more problems than benefits? ◄
► How to Install Windows 10 or 11 without Product Key? ◄
► Disable Quick Access in Windows 10, 11, 12 Solution or Reg Hack!? ◄


This website does not store personal data. However, third-party providers are used to display ads,
which are managed by Google and comply with the IAB Transparency and Consent Framework (IAB-TCF).
The CMP ID is 300 and can be individually customized at the bottom of the page.
more Infos & Privacy Policy

....