0

I want to ask the user to give me five questions, and then I want to print out these five questions. I am using a for loop. It works for the first questions and then it stops working. Thank you in advance.

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

    int main (void)
    {
        char *question [5];
        question[5] = (char*) malloc(5 * sizeof(char));

        for (int i = 0; i < 5; ++i)
        {
             printf("enter question number %i\n", i+1);

             scanf("%[^\n]%*c", question[i]);

             printf("%s\n", question[i]);
        }

        free (question[5]);

       return 0;
   }
  • 2
    `question[5] = (char*) malloc(5 * sizeof(char));` - this is allocating space for 5 `chars` and assigning to a pointer which is out of `questions` bounds. Definitely not what you want to do. You need to allocate space for each one of `question[0]...question[4]` – Eugene Sh. Nov 13 '20 at 21:51
  • See also: [c - Do I cast the result of malloc? - Stack Overflow](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – MikeCAT Nov 13 '20 at 22:20

2 Answers2

0

Couple things wrong with your code here.

First, question[5] = malloc(5 * sizeof(char)), allocates 5 chars to the array of char pointers at the last index. This is not what you want, what you want is an array of 5 char pointers with all have sufficient memory to hold the strings.

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

int main (void) {
        char *question [5];
        
        for (int i = 0; i < 5; ++i)
        {
            question[i] = malloc(sizeof(char) * 256); //n is the maximum length of the string
            printf("enter question number %i\n", i+1);

            fgets(question[i],256 ,stdin);
            
            printf("%s\n", question[i]);
        }

        for (int i = 0; i < 5; i++) {
            free(question[i]);
        }


       return 0;
}

So, in the above code, I am allocating 256 characters for every index of the array of strings (char pointers), and then writing the user input on them. Now every index contains one string inputted from the user.

And also in the end freeing the same way by traversing through each index and freeing the memory allocated.

adgerger
  • 41
  • 3
0

it seems that you did not understand what is dynamic allocate & what 2D array is.

To do things that you want, you need to create a 2D array for save your '5 strings' into each 1D array.

[0] => ㅁㅁㅁㅁㅁ... question1

[1] => ㅁㅁㅁㅁㅁ... question2

[2] => ㅁㅁㅁㅁㅁ... question3

[3] => ㅁㅁㅁㅁㅁ... question4

[4] => ㅁㅁㅁㅁㅁ... question5

try this

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

int main (void)
{
    char **question = (char **) malloc(5 * sizeof(char*));

    for (int i = 0; i < 5; ++i)
    {
         question[i] = (char*) malloc(sizeof(char) * length you want);

         printf("enter question number %i\n", i+1);

         scanf("%[^\n]%*c", question[i]);

         printf("%s\n", question[i]);
    }

    for (int i = 0; i < 5; ++i) free(question[i]);

    free(question);

   return 0;

}

seenblee
  • 52
  • 4