Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does it mean "to declar an array of chars and an array of numbers using the same identifier"?

Hi there, i`m learning C. And there is a task where i have to declar an array of characters and an array of numbers using the same identifier. For an array of numbers determine the number of repetitions of each number. For an array of symbols to find the position and value of the character for a given character code. And I have to use a conditional compilation.

So I have to do something like that(?):

#define SIZE 100
char symbols[SIZE];
int numbers[SIZE];

Or I have to do something like that(it is probably wrong, but i don`t really understand the task):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#define SIZE 100
char arr[SIZE];
int arr[SIZE];

>Solution :

An identifier is a name for something.
So you are correct in what is being asked is something like:

char arr1[10];  // array of chars identifier "arr1"
int  arr1[10];  // array of ints  identifier "arr1"   

But as Eugene mentioned you can’t do this in C as you will get an error as you can have an identifier only identify one type of variable. Other languages are different. Some replace
the object some keep track of them seperately.

For conditional compile you need #define/#ifdef/#endif
So for example

#define ARR_IS_INT
#ifdef ARR_IS_INT
// give only int def to compiler
int arr1[10];
#else
// give only char def to compiler
char arr1[10];
#endif

Conditional compile statements like the above are handy ways to hide code from the compiler in specific cases. Say you have some old code you still want to support, but you have new stuff that won’t work with the old. You can keep both in the code base and select between them.
There are multiple ways to define these to the pre-processor.
Essentially your code is read by a pre-processor and all the #defines /#ifdefs are replaced with the code that the compiler will read. This pre-processor pass through your code is hidden, but you can typically force the compiler to spit it out with a compiler option so you can decipher whats going on.

Hope this was helpful.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading