简体   繁体   English

C 使用 printf 和 scanf 的程序在输入时崩溃

[英]C program using printf & scanf crashes on input

I am writing following c code and getting an error:我正在编写以下 c 代码并收到错误:

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

int main()
{
char *prot;
char addr[20];
FILE *fp;
int i = 0;
int tos,pld;

prot = (char *)malloc(sizeof(char *));
//addr = (char *)malloc(sizeof(char *));

printf("\n enter the protocol for test::");
scanf(" %s",prot);
printf("\n enter the addr::");
scanf(" %s",addr);
printf("\n enter the length of the payload::");
scanf(" %d",pld);
printf("\n enter the tos :: ");
scanf(" %d",tos);

I am getting the following error while entering the value.There is a segmentation fault coming could anyone tell me why this segment fault is coming:输入值时出现以下错误。出现分段错误谁能告诉我为什么会出现此分段错误:

enter the protocol for test::we

enter the addr::qw

enter the length of the payload::12

Segmentation fault
prot = (char *)malloc(sizeof(char *));

Should be:应该:

prot = malloc(sizeof(char) * SIZE); // SIZE is the no. of chars you want

Another problem is: You should use & for integers in scanf() !另一个问题是:您应该在scanf()中使用&作为整数!

With changes:随着变化:

printf("\n enter the length of the payload::");
scanf(" %d",&pld);
printf("\n enter the tos :: ");
scanf(" %d",&tos);

The segmentation fault is because scanf expects a pointer to the variable the scanned value shall be stored in, but you pass the variable pld itself.分段错误是因为scanf需要一个指向扫描值应存储在的变量的指针,但是您传递了变量pld本身。 That is uninitialised and hence when interpreted as a pointer points into the wild.这是未初始化的,因此当被解释为指向野外的指针时。 The same happens with the tos . tos发生同样的情况。 And of course, you should allocate the proper amount of space for prot as has otherwise been pointed out.当然,您应该为prot分配适当的空间量,这在其他方面已被指出。

Your memory allocation for prot has allocated 4 bytes (on a 32-bit system) or 8 bytes (on a 64-bit system) for the string.您为prot分配的 memory 已为字符串分配了 4 个字节(在 32 位系统上)或 8 个字节(在 64 位系统上)。 If you read more than that into it, you are overflowing your buffer.如果您读取的内容不止于此,那么您的缓冲区就会溢出。

Unless there was a good reason to do otherwise, I'd simply use:除非有充分的理由不这样做,否则我只会使用:

char prot[128];

for any suitable size for the string.对于任何合适的字符串大小。

You should also check all your scanf() calls to ensure they succeed;您还应该检查所有scanf()调用以确保它们成功; you should probably apply a limit to the size of the strings.您可能应该对字符串的大小应用限制。 For a char prot[128];对于char prot[128]; , the safe conversion is %127s ; , 安全转换是%127s ; the null is not counted in the conversion specification. null 不计入转换规范。

If your compiler was not warning you about these lines:如果您的编译器没有警告您这些行:

scanf(" %d",pld);
scanf(" %d",tos);

you either need to turn on more warnings or get a better compiler.您要么需要打开更多警告,要么获得更好的编译器。 If it was warning you, pay heed to your compiler;如果它警告您,请注意您的编译器; it knows more about C than you do (and probably more about it than I do, too).它比你更了解 C(而且可能比我更了解它)。

scanf(" %d", &pld);
scanf(" %d", &tos);

This is probably not the source of your current problem, but it is a bug:这可能不是您当前问题的根源,但它是一个错误:

prot = (char *)malloc(sizeof(char *));

I doubt you meant to make a buffer the size of one character pointer.我怀疑您打算制作一个字符指针大小的缓冲区。

Anyway, to pinpoint your immediate issue, please run your program under valgrind and/or a debugger.无论如何,要查明您的直接问题,请在 valgrind 和/或调试器下运行您的程序。 In this particular case just enabling compiler warnings would have caught your problem, which is that you're passing integers by value where you should be passing by pointer to scanf.在这种特殊情况下,仅启用编译器警告就会发现您的问题,即您按值传递整数,而您应该按指向 scanf 的指针传递整数。 This could have been solved by the compiler, instead of coming to us, if only you enable the relevant options.这本可以由编译器解决,而不是来找我们,只要您启用相关选项。

scanf expects pointers to the variables you're filling (except in the case of strings, which are already pointers to char ). scanf需要指向您正在填充的变量的指针(字符串除外,它们已经是指向char的指针)。

Try尝试

scanf(" %d", &pld);

and the same with tos .tos一样。

scanf family of functions are main source of problems in homework. scanf 函数族是作业中问题的主要来源。

  1. They always expect addresses, so they can be used as OUTPUT.他们总是期待地址,所以他们可以用作 OUTPUT。
  2. They cannot be type-checked because the prototype of that part is..., so you can put anything there.它们无法进行类型检查,因为该部分的原型是...,因此您可以将任何东西放在那里。 compiler does not complain.编译器不会抱怨。

When things don't work, check the receiver arguments, they need to be address of items to be written into, and the type has to match what your specify in the format string.当事情不起作用时,检查接收器 arguments,它们需要是要写入的项目的地址,并且类型必须与您在格式字符串中指定的相匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM