C 获取大值的分段错误

C 获取大值的分段错误,c,pointers,segmentation-fault,C,Pointers,Segmentation Fault,下面的程序是崩溃与分割大n(n>200),你能帮我在这里 #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> struct node { char name[16]; char num[8]; }; int main() { int n, i,j; struct node *hash; scanf("%d"

下面的程序是崩溃与分割大n(n>200),你能帮我在这里

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

struct node {
    char name[16];
    char num[8];
};
int main() {
    int n, i,j;
    struct node *hash;
    scanf("%d",&n);
    hash = (struct node *) malloc(n * sizeof(struct node));
    for (i=0; i<n ; i++) {
        scanf("%s %s", (hash + (i *sizeof(struct node)))->name,
                        (hash + (i *sizeof(struct node)))->num);
    }   
 for (i=0; i<n ; i++) {
        printf("%s=%s\n",(hash + (i *sizeof(struct node)))->name,
                        (hash + (i *sizeof(struct node)))->num);
  }
 return (0);
}
#包括
#包括
#包括
#包括
结构节点{
字符名[16];
字符数[8];
};
int main(){
int n,i,j;
结构节点*散列;
scanf(“%d”和“&n”);
hash=(结构节点*)malloc(n*sizeof(结构节点));
对于(i=0;iname,
(hash+(i*sizeof(struct node))->num);
}   
对于(i=0;iname,
(hash+(i*sizeof(struct node))->num);
}
返回(0);
}

向指针添加整数时,编译器将执行指针算术。因此,
hash+i
被编译器翻译成类似于
(char*)hash+i*sizeof(struct node)
。以字节为单位计算偏移量,然后以字节为单位应用偏移量

因此,您的代码相当于

(char*)hash + i * sizeof(struct node) * sizeof(struct node)
这将很快超出数组边界,调用未定义的行为


正如注释总结的那样,要么使用
(hash+i)
,要么使用更简洁的(在我看来)
hash[i]
当您向指针添加整数时,编译器将执行指针算术。因此,
hash+i
被编译器翻译成类似于
(char*)hash+i*sizeof(struct node)
。以字节为单位计算偏移量,然后以字节为单位应用偏移量

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

struct node {
    char name[16];
    char num[8];
};

int main(void) {
    size_t n;
    if (scanf("%zu", &n) != 1) {
      return 1;
    }

    struct node *hash = malloc(n * sizeof *hash);
    if (hash == NULL) {
      return 1;
    }

    for (size_t i = 0; i < n; i++) {
        if (scanf("%15s %7s", hash[i].name, hash[i].num) != 2) {
          free(hash);
          return 1;
        }
    }

    for (size_t i = 0; i < n; i++) {
        printf("%s %s\n", hash[i].name, hash[i].num);
    }

    free(hash);
}
因此,您的代码相当于

(char*)hash + i * sizeof(struct node) * sizeof(struct node)
这将很快超出数组边界,调用未定义的行为

正如评论总结的那样,要么使用
(hash+i)
,要么使用更简洁的(在我看来)
hash[i]

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

struct node {
    char name[16];
    char num[8];
};

int main(void) {
    size_t n;
    if (scanf("%zu", &n) != 1) {
      return 1;
    }

    struct node *hash = malloc(n * sizeof *hash);
    if (hash == NULL) {
      return 1;
    }

    for (size_t i = 0; i < n; i++) {
        if (scanf("%15s %7s", hash[i].name, hash[i].num) != 2) {
          free(hash);
          return 1;
        }
    }

    for (size_t i = 0; i < n; i++) {
        printf("%s %s\n", hash[i].name, hash[i].num);
    }

    free(hash);
}
#包括 结构节点{ 字符名[16]; 字符数[8]; }; 内部主(空){ 尺寸; 如果(scanf(“%zu”,&n)!=1){ 返回1; } 结构节点*hash=malloc(n*sizeof*hash); if(hash==NULL){ 返回1; } 对于(大小i=0;i
#包括
#包括
结构节点{
字符名[16];
字符数[8];
};
内部主(空){
尺寸;
如果(scanf(“%zu”,&n)!=1){
返回1;
}
结构节点*hash=malloc(n*sizeof*hash);
if(hash==NULL){
返回1;
}
对于(大小i=0;i
您输入的字符串是什么?任何大于15个字符的名称或大于7位的数字都将导致未定义的行为,这可能导致seg故障。您想了解有关的信息。这个
(hash+(i*sizeof(struct node)))…
应该是
(hash+i)…
或者只是
&hash[i]…
。此外,指针偏移量很难读取(除了错误和超出分配的内存之外)。让编译器处理
struct node
的大小,只需编写
hash[i]
。此外,不需要在C中强制转换
malloc()
&friends的结果,无论如何都不建议这样做。不要编辑帖子来问其他问题。这使得下面的答案无关紧要。。只需使用“提问按钮”。您输入的字符串是什么?任何大于15个字符的名称或大于7位的数字都将导致未定义的行为,这可能导致seg故障。您想了解有关的信息。这个
(hash+(i*sizeof(struct node)))…
应该是
(hash+i)…
或者只是
&hash[i]…
。此外,指针偏移量很难读取(除了错误和超出分配的内存之外)。让编译器处理
struct node
的大小,只需编写
hash[i]
。此外,不需要在C中强制转换
malloc()
&friends的结果,无论如何都不建议这样做。不要编辑帖子来问其他问题。这使得下面的答案无关紧要。。只需使用“提问按钮”。这并不能向OP解释他/她的程序为什么不起作用,因此也无助于OP进一步理解和学习如何避免它。这也不能向OP解释他/她的程序为什么不起作用,因此,这无助于OP进一步理解和学习如何避免它。