0

I want to insert a string to an array of strings and got a problem...I can't figure out what's the reason for it.

char* OPT[100]; 

void setOpt(char* ele){
    char* beginning = ele;      //start of index option
    int size = sizeOpt(ele);    //length of option
    char result[size];          //our OPTION

    //go to the first character of option
    while(*beginning != 0 && ((*beginning < 'a' || *beginning > 'z') && (*beginning < 'A' || *beginning > 'Z'))){
    ++beginning;
    }

    strncpy(result, beginning, size);       //get OPTION
    int i = 0;                              //insert at index
    while(OPT[i] != NULL){
        ++i;                
    }
    printf("%s\n", OPT[i]); 
    strcpy(OPT[i], result);                 //insert in array of OPTIONS
}

int main(int argc, char* argv[]){
    char* some[] = {"ls" , ".", "-maxdepth=n", "-something=123"};

    setOpt(some[2]);
   setOpt(some[3]);
   return 0;
}

My Output:

(null)
Segmentation fault: 11
4
  • Suggest you run your program in a debugger. At a minimum it will immediately tell you which line of code it is seg faulting on. And you can use it to step through your code and examine the variables to debug the program effectively. Commented Nov 28, 2019 at 18:55
  • You need to allocate memory to OPT[i] before using strcpy on it. Also, you need to add error checking. For example, in case no valid free entry is found in OPT. Commented Nov 28, 2019 at 18:56
  • So it isn't enough to declare my array? Every time I want to add a string to my array I have to allocate it in my array? Commented Nov 28, 2019 at 18:59
  • You declare an array of pointers. Each pointer is initialised to 0. So yes, of course you need to allocate some memory to set each pointer before using the pointer. See the posted answer by @selbie. Commented Nov 28, 2019 at 19:00

2 Answers 2

1

This is the issue:

strcpy(OPT[i], result);                 //insert in array of OPTIONS

You are attempting to copy to a NULL pointer. You need to allocate memory for the string copy to be placed into OPT.

OPT[i] = strdup(result);                 //insert in array of OPTIONS

Or alternatively:

OPT[i] = (char*)malloc(strlen(result)+1);
strcpy(OPT[i], result);

Now, let's cleanup your entire function to something more consice and readable:

int isLetter(char c) {
    return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
}

void setOpt(const char* ele) {

    const unsigned int maxentries = sizeof(OPT) / sizeof(OPT[0]);
    unsigned int i = 0;
    while (i < maxentries && OPT[i]) {
        i++;
    }

    if (i < maxentries)
    {
        const char* result = ele;
        while (*result && !isLetter(*result)) {
            result++;
        }

        if (*result) {
            OPT[i] = (char*)malloc(strlen(result) + 1);
            strcpy(OPT[i], result);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

C doesn't support multiple string in char array. Instead of you have to use 2d array to put multiple string in array.

1 Comment

I already use a 2D array to add a string to a char* Array o.O

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.