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;
}
9
  • 1
    printf("%s\n", &A[i]); should be printf("%s\n", A[i]); Commented Apr 14, 2015 at 15:37
  • 1
    What does "didn't work" means? What were the problems you noticed? Commented Apr 14, 2015 at 15:38
  • the result of malloc() should never be cast. Commented Apr 14, 2015 at 15:43
  • The malloc() must be cast! It returns a void* but i have char* and char**! Commented Apr 14, 2015 at 16:19
  • "malloc() must be cast": No, in C this is not necessary: stackoverflow.com/q/605845/694576 Commented Apr 14, 2015 at 16:20

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

-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 ...

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.