【问题标题】:Relation between Segmentation fault and StructSegmentation fault 与 Struct 的关系
【发布时间】:2019-04-01 05:11:48
【问题描述】:

当我尝试将scanf 数字添加到结构时,我得到了一些Segmentation fault。 我不知道scanf 是否与这种情况下的故障有关。 我认为我分配的内存很好,我在过去 2 小时内阅读了有关此故障的信息,并且在任何地方阅读了有关内存分配问题的信息,但在我的代码中没有看到这一点。 我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <time.h>

#define MAX_STRING_LEN 80
#define PHONE_NUMBER 15

struct order {
    time_t   systime;
    char     name[MAX_STRING_LEN];
    char     email[MAX_STRING_LEN];
    int      phonenumber;
    int      size;    
};


//functions
void readName(struct order *current);
void checkValues(struct order *current);
void readEmail(struct order *current);
void readPhone(struct order *current);
void readSize(struct order *current);

//read name
void readName(struct order *current){
    printf("name: ");
    scanf("%80[^\n]", current->name);
   // scanf("%s",current->name);
}

//read email
void readEmail(struct order *current){
    printf("e-mail: ");
    char tmp[80];
    scanf("%s[^\n]",current->email);
}

//read phone number
void readPhone(struct order *current){
    printf("phone: ");
    scanf("%15i[^\n]", current->phonenumber);
}

//read size of order
void readSize(struct order *current){
    printf("size: ");
    scanf("%i", current->size);
}

void checkValues(struct order *current){
    printf("Name: %s \n",current->name);
    printf("e-mail: %s \n", current->email);
    printf("tel: %d \n", current->phonenumber);
    printf("size: %d \n", current->size);
    printf("time: %ld \n", current->systime);
}

//***

int main(k)
{
   struct order current; //struct init

   //read values
   readName(&current);
   readEmail(&current); 
   readPhone(&current); // I got the error here, but only if I try this with numbers, with letters save only 0
   readSize(&current);
   current.systime = time(NULL);

   // ** //
    checkValues(&current);

    return 0;
}

【问题讨论】:

  • 在您的readPhone 函数中,您是要读取字符串还是整数?对于读取整数,您需要传递给scanf 的参数是什么?
  • 请启用警告;他们可以帮助您解决scanf 格式不匹配的问题。每当您使用%s%[ 扫描字符串以外的内容时,您必须传递存储结果的变量的地址。所以它是scanf("%i", &amp;current-&gt;size) 等等。
  • @Someprogrammerdude 我想保存电话号码,所以最多 15 个号码长度整数
  • @MOehm 是的,这对我有帮助,请写下它作为答案,然后我可以接受它
  • 一般来说,对于要计算的数字,例如年龄、价格或数量,最好使用数字类型(例如 int)。真正作为标签的数字——例如电话号码或街道号码——最好保存为字符串。这样,您就可以捕捉到准确的表示,而不会丢失任何内容。

标签: c struct segmentation-fault scanf


【解决方案1】:

scanf("%15i[^\n]", current-&gt;phonenumber);

正如已经指出的,这应该是:

scanf("%15i", &current->phonenumber);

(您在readSize 中也重复了同样的错误。)

一般来说,您应该在编译时使用可用于您的编译器的最大警告。一个好的编译器会警告你,当需要一个指针时,你正在将 int 传递给 scanf

我想保存电话号码,所以最多 15 个数字长度整数

在典型系统上,sizeof(current-&gt;phonenumber) == 4。这意味着您可以存储在该变量中的最大值是INT_MAX == 2147483647。这只有 10 位数字,并且不是每个 10 位数字都适合。

如果您希望能够存储所有可能的 15 位数字,则需要以不同的方式存储它们(在 char phone[16]int64_t 中)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多