I want to store a user entered string using struct, I'm facing an issue with my code, where it returns the same error, or the output window closes without returning anything.
My code:
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
struct details
{
char* name;
}student1,student2,student3,student4,student5;
int main()
{
char* temp = malloc(256);
printf("Enter Name: ");
fgets(temp, 255, stdin);
strcpy_s(student1.name,255, temp);
printf("\n");
puts(student1.name);
free(temp);
}
I wanted to input a string from the user and store it using the struct, and this error is popping up: (it pops up after typing in the string, and pressing ENTER)
Visual C++ Runtime Library
Debug Assertion Failed!
Program: ...C programming\C00S_Practical10\x64\Debug\C00S_Practical10.e xe
File: minkernel\crts\ucrt\ine\corecrt_internal_string_templates.h Line: 67
Expression: ((destination))
NULL && ((size_in_elements)) > 0
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application}
I tried to directly input a string into the struct variable
scanf_s("%s",student1.name);
but it didn't work, so I tried storing the input in a temp variable, and copying it to the struct variable, but that is also working incorrectly.
I coudn't find a post with my exact problem, so I decided to post here