Problems encountered in exercises when learning C

CSDN Q & A 2022-06-24 10:06:40 阅读数:515

problemsencounteredexerciseslearning
The phenomenon and background of the problem

Exercises done , It has been modified according to the standard answer that can be run , But it will still report an error

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// Enter an integer number , Then apply for the corresponding size of memory , Then read a string , The input length of the string is less than the size of the initial input integer , Finally, output the input string
int main()
{
int i;
scanf("%d", &i);
char* p;
p =(char*)malloc(i);
char c ;
scanf("%c",&c);
gets(p);
puts(p);
return 0;
}

Operation results and error reporting contents

Run to the puts(p) You will report an error in the wrong place
0x00007FFEE220F130 (ucrtbased.dll) It's about ( be located Homework .exe in ) Thrown exception : 0xC0000005: Write location 0xFFFFFFFFC3A0EDB0 Access conflict occurred when .

img

My solution ideas and tried methods

Tried puts(p) Switch to printf("%s",p), The result will still be printf I'd like to report a mistake

What I want to achieve

Solve the problem of error reporting , I wonder if this is the problem with my computer

版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。 https://primo.wiki/2022/175/202206171810067051.html