0

I kept getting the error "Command terminated by signal 11" when I try to save a string into an array of strings. After changing the code from the link below, now the name prints out ���W.

I used this link to fix my code off of but it is still causing me to get the error "Command terminated by signal 11".

    #include<stdio.h>
    #include<string.h>

    void getName(char* c[], int size);
    int main(void) {
        int yenoSize = 0;
        int strSize = 0;
    char name1[30];
    char* name[30];
    char* students[5][30];

    for(int i=0;i<1;i++){
    getName(name, strSize);
    students[i][0] = name1;
    }

    for(int k=0; k<1;k++){
    printf("Student Name: %s \n", students[k][30]);

    }

    }

    void getName(char* c[], int size){
    char name1[30];
    printf("Enter student name: ");
    fgets(name1, 30, stdin);
    c[0] = &name1[0];
    printf("%s", name1);

    }

The output is supposed to print out the name that the user inputs (Student Name: Jon) but it is currently printing ���W. How can I fix the problem? I believe the problem exists with the name pointer pointing to null. Is that the problem? I appreciate the help!

apaul
  • 308
  • 3
  • 13

1 Answers1

-1

2 major issues.

1) In main(), you are calling getName() with name but storing name1 in students variable.

2) In function getName(), name1 is a local array. The scope of this variable is limited to getName(). You can not return address of name1 by assigning it to c[0] as it may get freed by OS after function returns. You can consider using malloc() for this.

MayurK
  • 1,925
  • 14
  • 27