0

i have to insert strings in array of string in C. I wrote a function but it didn't work. Can you help me?

When i'm going to print the array, program crashes.

Thanks

int leggi(char **a, int *len) {
    int i;
    scanf("%d", len);
    if(*len <= 0) return 1;

    a = (char **) malloc(*len * sizeof(char *));
    if(a == NULL) return 1;

    for( i = 0; i < *len; i++ ) 
    {
        a[i]=(char *)malloc(101*sizeof(char));
        scanf("%s", &a[i]);
    }
    printf("Saved\n");
    return 0;
}

int main() {

    int i, n;
    char **A;

    if(leggi(A, &n)) return 1;
    printf("%d\n",n);




    for( i = 0; i < n; i++ ) 
    {
        printf("printf\n");
        printf("%s\n", &A[i]);
    }
    return 0;
}
SimC
  • 105
  • 2
  • 8
  • 1
    `printf("%s\n", &A[i]);` should be `printf("%s\n", A[i]);` – Ayushi Jha Apr 14 '15 at 15:37
  • 1
    What does "didn't work" means? What were the problems you noticed? – Cristik Apr 14 '15 at 15:38
  • the result of malloc() should never be cast. – Peter Miehle Apr 14 '15 at 15:43
  • The malloc() must be cast! It returns a void* but i have char* and char**! – SimC Apr 14 '15 at 16:19
  • "*malloc() must be cast*": No, in C this is not necessary: http://stackoverflow.com/q/605845/694576 – alk Apr 14 '15 at 16:20
  • to start, 'read()' is a system function. In general, the code should never have a function with the same name as a system function. The posted code fails to list the #include statements. However, if the header file stdio.h is included, then the compiler will raise a fuss about the duplicate function name, with different parameters. Similar complaints will be raised about the 'built-in' function printf(). Similar complaints will be raised about implicit declaration of built-in function malloc() – user3629249 Apr 14 '15 at 22:42
  • this line: 'scanf("%s", &a[i]);' 1) always check the returned value from scanf() and family, to assure the input/conversion operation was successful. 2) this line raises a compiler warning about a[i] being 'char**' when the expected type is 'char*'. a similar problem exists with this line: printf("%s\n", &A[i]);' 'Suggest enabling all compiler warnings, fix the warnings, then re-post the updated code. – user3629249 Apr 14 '15 at 22:47
  • in general, when the code wants an input from the user, the code should first output a statement to the user (printf() works well for this) indicating what the user needs to input. Otherwise the user is looking a a blinking cursor with no idea as to what to do. – user3629249 Apr 14 '15 at 22:52
  • the partial parameter to malloc: ''sizeof(char)" is always 1, so for code clarity and to reduce code clutter, sizeof(char) should not be used when calling malloc (and family) – user3629249 Apr 14 '15 at 22:56

1 Answers1

1

Change scanf("%s", &a[i]) to scanf("%s", a[i]), a[i] is the pointer to the first character in your string, getting the address of it would just return the address of the pointer, not the actual first char. Another thing to note is that you are not actually modifying the pointer in the main function, only the local function pointer, as such, it would have no effects on the one in main. Here's the edited version:

int read(char ***a, int *len) {
    int i;
    scanf("%d", len);
    if( *len <= 0 ) return 1;

    (*a) = (char **) malloc(*len * sizeof(char *));
    if((*a) == NULL) return 1;

    for( i = 0; i < *len; i++ )
    {
        (*a)[i]=(char *)malloc(101*sizeof(char));
        scanf("%s", (*a)[i]);
    }
    printf("Saved\n");
    return 0;
}

int main() {

    int i, n;
    char **A;

    if(read(&A, &n)) return 1;
    printf("%d\n",n);

    for( i = 0; i < n; i++ )
    {
        printf("printf\n");
        printf("%s\n", A[i]);
    }
    return 0;
}
Nazara
  • 124
  • 4
  • 1
    -1 because: this proposed answer still has most of the problems of the original posted code. 1) casting the returned call from malloc 2) naming the subroutine 'read()' 3) using 'sizeof(char)' note: sizeof(char*) is ok. 4) using "%s" as a format conversion parameter in scanf() means there is no size limit, so the user could enter enough characters to overflow the input buffer. 5) the returned value from scanf() is not being checked to assure the input/conversion was successful ... – user3629249 Apr 14 '15 at 23:03