0

In my personal C work, I am trying to create a function that will append a process identifier to an array set to contain my own structure. It has to assign the data and then check if it was added to the array. If it wasn't, it will not return okay.

I cannot find anything about getting the actual number of items inside an array, and if there are, please let me know.

I have tried using sizeof and sizeof(arr) / sizeof(whatnot), and it returns a static number no matter the number of items in the array.

What I am trying to make is like the len function in Python.

Structure:

typedef struct process {
    char*        process_name;
    unsigned int process_uid;
    unsigned int parent_process_uid;
} Process;

Code:

#define max_processes 100 /* I should set it higher, but this is fine (for now) */

unsigned int arrlen(Process array); /* tbd */

unsigned int total_processes = 0;
Process all_processes[max_processes] = {};

int assignPID(char* name, Process parent) {
    unsigned int newPID = parent.parent_process_uid + 1;
    Process newProcess = { 
        name,
        newPID,
        parent.parent_process_uid
    };
    if (arrlen(all_processes) >= max_processes) { /* if we entered the max amount of processes, */
        return 2; /* then bail with a different exit code to signal max processes reached */
    }
    all_processes[total_processes] = newProcess; /* assigning */
    total_processes++; /* amount of processes raised one more */
    if (arrlen(all_processes) != total_processes) { /* if the process wasn't added, */
        total_processes--;
        return 1; /* then bail */
    } else {
        return 0; /* or say 'it worked!' */
    }
}

Any help or direction would be appreciated.

jarmod
  • 46,751
  • 9
  • 81
  • 86
llamaking136
  • 322
  • 2
  • 11

0 Answers0