0

I was attempting to create an array from user input in C. My code was as follows

#include <stdio.h>
#include <stdlib.h>

char** create_array(int num);

int main(){
    char** my_array;
    int i;
    printf("%s", "Input the size of the array: ");
    scanf("%d", &i);
    my_array = create_array(i);
    for (int m = 0; m < i; m++){
        printf("%s\n", (char*)my_array[m]);
    }
}
char ** create_array(int num){
    char** array = malloc(num * sizeof(char*));
    for (int i = 0; i < num; i++) {
        char temp[32];
        printf("Input element %d of the array: ", i);
        scanf("%s", temp);
        array[i] = temp;
    }
    for (int m = 0; m < num; m++){
        printf("%s\n", array[m]);
    }
    printf("end of func\n");
    return array;
}

I was having (possibly unrelated?) issues with segmentation faults until I replaced the declaration of temp from char *temp; to char temp[32];. I am not sure of why declaring temp as a char pointer creates the segmentation fault, if that is a simple related answer let me know, if not I will ask in another question.

However, when I run this code, upon inputting

Input the size of the array: 2
Input element 0 of the array: value0
Input element 1 of the array: value1

I get

value1
value1
end of func


Segmentation fault (core dumped)

So to me it seems like temp somehow isn't changing when the for loop executes next iteration. I'm not sure why or how that would ever happen though. I also tried changing

printf("%s\n", my_array[m]);

to

printf("%s\n", (char*)my_array[m]);

but that didn't seem to help either.

Any ideas? Thanks

3
  • 1
    Why do you have the num parameter is you're going to immediately overwrite it with scanf? Commented Oct 7, 2021 at 3:20
  • @BradyDean you're right, that was leftovers from a previous attempt I forgot to remove. updated the question, thanks! Commented Oct 7, 2021 at 3:26
  • 2
    char temp[32] should only be used in the scope it's declared. Once the scope ends, accessing that memory results in Undefined Behavior. You should dynamically allocate memory for it instead: char* temp = malloc(32) Commented Oct 7, 2021 at 3:31

1 Answer 1

1

I believe this is what you're looking for. The trick is allocate the memory for the strings on the heap. Saving pointers to char temp[32]; is dangerous because it's in automatic storage. As @Spikatrix said in a comment, that memory is not guaranteed to be in a valid state between each iteration of the loop. With the data on the heap, there's a well-defined region of memory set aside and identified by the pointer returned from malloc.

There's also a lot of good reason to not use scanf. As is, your code does not do any bounds checks and can easily overwrite the 32-byte array allocated for each string.

#include <stdio.h>
#include <stdlib.h>

char** create_array(int num);

int main(){

    int num_strings;
    printf("Input the size of the array: ");
    scanf("%d", &num_strings);
    printf("\n");

    char** my_array = create_array(num_strings);

    for (int i = 0; i < num_strings; i++){
        printf("%s\n", my_array[i]);
    }

    for (int i = 0; i < num_strings; i++) {
        free(my_array[i]);
    }

    free(my_array);
}

char** create_array(int num) {

    char** array = malloc(num * sizeof(char*));

    for (int i = 0; i < num; i++) {
        array[i] = malloc(32);
        printf("Input element %d of the array: ", i);
        scanf("%s", array[i]);
        printf("\n");
    }

    printf("end of func\n");
    return array;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yep, that did it! Is there a better way you would do it to avoid exceeding the memory limit without setting it just to like 1024?

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.