How do I get the number of elements a char array points to?

cimbomlu1905

I was researching but could not find a way. I am passing 2 char array pointers to a function and fill those 2 arrays with values. Is there a way to get the size of the filled arrays?

I tried size_t size = sizeof(*arguments)/sizeof(arguments[0]) already but that gave me 1 as a result, I know why its giving me 1 as result by researching but I just couldn't find a way to get the proper array length(the value how much elements are in the array). If that is not possible how can I work around this ? I need to know this because, I wanna give the last value of any array a NULL for my exec functions. My programs works as follows:

An user inputs 2 program names which are executed. But the 2 programs are separated by a ";". So I have 2 arrays which can vary in size , which depends on the input in the terminal.

void getArguments(char * [],char * [], int ,char * []);

  int main(int argc, char * argv[]){
     pid_t pid,pid2;
     char * arguments2[argc/2+1];
     char * arguments[argc/2+1];

     getArguments(arguments, arguments2,argc,argv);

     if((pid=fork())==-1){
        printf("Error");
     }
     else if(pid == 0){

        execvp(arguments[0],arguments);
     }

     else{
      if((pid2 = fork())== -1){
        printf("Error" );
      }
      else if(pid2 == 0 ){

        execvp(arguments2[0],arguments2);
      }
      else{
        int status;
        wait(&status);
        exit(0);
      }
     }
    return 0;
  }


  void getArguments(char * arguments[], char * arguments2[],int argc, char * argv[]){
      int i=0;

      while(strcmp(argv[i+1],";")!=0){
      arguments[i] = argv[i+1];
      i++;
      }
      arguments[argc/2-1]= NULL;

      int j = i+2;
      int k = 0;

      for( ;j<=argc; j++){
      arguments2[k] = argv[j];
      k++;
      }
      arguments2[argc/2-1]=NULL;
  }
Paul Ogilvie

No, you can't get the size of an array if you only have a pointer. You must pass the size as a separate argument to your function.

EDIT: I now understand you need to return from your function the number elements used in the array. You can either add two integer pointer variables that receive the size, or you could set the first unused element to NULL so the caller knows when it is at the end of the filled portion.

For example:

 char * arguments2[argc];  // as you don't know the result, do not divide by 2: it may be too small!
 char * arguments[argc];
 int size1=0, size2=0;

 getArguments(arguments, &size1, arguments2, &size2, argc, argv);

And:

void getArguments(char *arguments[], int *size1, char *arguments2[], int *size2, int argc, char *argv[])
{
    // ...
    *size1= i;
    //...
    *size2= j;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get the number of elements in a list?

How do I randomly get a certain number of elements of a numpy array with at least one element from each class?

How do I find out the number of elements in a Javascript array?

MongoDB: How do I query for the number of elements in an array of documents/dictionaries?

returning a pointer that points to char array. How do i print out the entire contents of the array?

How do i get the number of specific child elements in javascript

How do I get the number of elements in a list (length of a list) in Python?

How do I get the number of elements in a list in Python?

How do I split on a regex and get my elements into an array?

How do I get an array with all elements with the same key?

How do I get the type of an array's elements?

How do I get the minimum of certain elements of an array with Python?

How do I get elements from an array without repetition?

How do I get the last elements of an array in Ruby?

How do I dynamically get the value of an element from an array of elements?

compare number elements in array of char

How do I get an Array(Number) from Firestore in Android

How do I get the total number of occurrence for each array in an object?

how do i count the number of elements in array when elements are objects in BigQuery StandardSql

How do I get specific char from an array so that i can put into another array

How to get the number of elements in a hash array?

How to get a number of random elements from an array?

How to get the number of elements in a JSON array?

How do I get the number of points in the json file to use in a for loop in python

How do i get number of tolls between two points in google direction api?

How can I count the number of elements in an array?

How do I get the points of each word?

how do I print the number of elements

how do I free this char** array?