0

I have an array of n strings where n is not known at compilation time.

The real input is a giant string that I will splice, and add the parts to each position of the array.

In the example I've simulated a sentence with n=3 , but n can be any number.

void addWords(char *array[][300], int n) {
    char p[] = "Hello ";
    char p1[] = "World ";
    char p2[] = "!";

    strcpy(array[0],p);
    strcpy(array[1],p1);
    strcpy(array[2],p2);

    printf("%s%s%s\n",array[0],array[1],array[2]);

}


int main(int argc, char const *argv[])
{
    int n = 3;
    char array[n][300];

    addWords(array,3);

    return 0;
}

The code gives segmentation fault and I cannot identify the cause.

5
  • @BillLynch sorry, already edited main message, the error is segmentation fault Commented Sep 30, 2021 at 15:37
  • in printf("%s%s%s\n",p[0],p[1],p[2]); you're passing the wrong arguments to printf. "%s" format specifier expects a string, but you're passing a char. Do you mean that to be printf("%s%s%s\n",p0,p1,p2);? Commented Sep 30, 2021 at 15:37
  • 1
    When compiled, your code produces numerous warnings that are fairly concerning. gist.github.com/sharth/b55c8a02f42a42a53c45a6863b5b6e05 Commented Sep 30, 2021 at 15:39
  • @yano my bad, already fixed the code Commented Sep 30, 2021 at 15:39
  • 1
    Enable compiler warnings... and fix them Commented Sep 30, 2021 at 15:51

3 Answers 3

2
//void addWords(char *array[][300], int n) {
void addWords(char array[][300], int n) { // <== use `char array[][300]`
    char p[] = "Hello ";
    char p1[] = "World ";
    char p2[] = "!";

    strcpy(array[0],p);
    strcpy(array[1],p1);
    strcpy(array[2],p2);

    //printf("%s%s%s\n",p[0],p[1],p[2]);
    printf("%s%s%s\n",array[0],array[1],array[2]); // <== I think you meant `array` instead of `p`
}
Sign up to request clarification or add additional context in comments.

2 Comments

but I want to modify the array in the main, this only works on the function subspace, doesnt it? once the addWords is finished, the array is gone.
@digolira2 No, char array[][300] is already a pointer (because arrays decay to pointers), therefore when you modify array[0], you're modifying the same array that is in main. It's just like using char str[] as a function parameter in order to modify a given string.
1

This

void addWords(char *array[][300],
              ^^^^^^^^^^^^^^^^^^

means

pass a pointer to an array containing 300 char pointers

What you want to say is

pass a pointer to an array containing 300 char

So all you need is:

void addWords(char *array[][300], --> void addWords(char array[][300],

Comments

0

I see that you declared 'addWords' with argument of type "char *array[][300]", but pass value of type "char array[n][300]". Different types.

Comments

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.