Home > OS >  How to define an array depending on the OS?
How to define an array depending on the OS?

Time:12-11

My problem is that, depending on the OS, I need to declare an array containing the error text, but I can't figure out how to implement this using header files and preprocessor directives.

I have tried the following implementation but I am unable to access the desired array:

errorno.c:

#include "s21_errorno.h"

#ifdef __linux__
os_error[] = {"",
               "Operation not permitted",
               "No such file or directory",
               "No such process",
               "Interrupted system call",
               "I/O error",
               "No such device or address",
               "Argument list too long",
               "Exec format error",
               "Bad file number",
               "No child processes"};

#elif __APPLE__
os_error[] = {"",
              "Operation not permitted",
              "No such file or directory",
              "No such process",
              "Interrupted system call",
              "Input/output error",
              "Device not configured",
              "Argument list too long",
              "Exec format error",
              "Bad file descriptor",
              "No child processes"};

#endif

errorno.h:

#ifndef C2_S21_STRINGPLUS_0_S21_ERRORNO_H
#define C2_S21_STRINGPLUS_0_S21_ERRORNO_H

#define const char* os_error[]

#endif  // C2_S21_STRINGPLUS_0_S21_ERRORNO_H

main.c:

#include "stdio.h"
#include "s21_errorno.h"

int main() {

 for (int i = 0; i < 50 ;   i) {
    printf("%s",os_error[i]);
  }
  return 0;
}

CodePudding user response:

Let's start from the header file. You need a declaration there: extern const char *os_error[];. This allows you to use that incomplete array of char pointers in every .c file that includes it.

Then in the .c file you need a definition: const char *os_error[] = { /*stuff*/ };

But your os_error is an incomplete array, so you cannot get its size with sizeof. It's probably useful to also define a size_error variable to report on the size of that array.

s21_errorno.h:

#ifndef S21_ERRORNO_H
#define S21_ERRORNO_H

#include <stdlib.h>

extern const char *os_error[];
extern size_t size_error;

#endif  // S21_ERRORNO_H

s21_errorno.c:

#include "s21_errorno.h"

#ifdef __linux__
const char *os_error[] = { "",
               "Operation not permitted",
               "No such file or directory",
               "No such process",
               "Interrupted system call",
               "I/O error",
               "No such device or address",
               "Argument list too long",
               "Exec format error",
               "Bad file number",
               "No child processes" };
size_t size_error = sizeof os_error / sizeof *os_error;

#elif __APPLE__
const char *os_error[] = { "",
              "Operation not permitted",
              "No such file or directory",
              "No such process",
              "Interrupted system call",
              "Input/output error",
              "Device not configured",
              "Argument list too long",
              "Exec format error",
              "Bad file descriptor",
              "No child processes" };
size_t size_error = sizeof os_error / sizeof *os_error;

#endif

main.c:

#include <stdio.h>
#include "s21_errorno.h"

int main(void)
{
    for (size_t i = 0; i < size_error;   i) {
        printf("os_error[%d] = %s\n", i, os_error[i]);
    }
    return 0;
}

CodePudding user response:

As you export the symbols anyways, you might as well just define the variable in the header file like this:

const char *os_error[] = {
    "",
    "Operation not permitted",
    "No such file or directory",
    "No such process",
    "Interrupted system call",
#ifdef __linux__
    "I/O error",
    "No such device or address",
#elif __APPLE__
    "Input/output error",
    "Device not configured",
#endif
    "Argument list too long",
    "Exec format error",
    "Bad file number",
    "No child processes"
};
  • Related