我在编译下面的代码时得到了分段代码转储错误。我试图通过引用传递结构数组。 [英] Im getting segmentation code dumped error while compiling below code. I'm trying to pass array of structure by reference.

查看:67
本文介绍了我在编译下面的代码时得到了分段代码转储错误。我试图通过引用传递结构数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

struct student
{
    char name[20];
    int rollno;
}s1[3];

void accept(struct student *ptr[], int n)
{
    // printf("Students details are:\n");
    int i;
    for(i=0;i<n;i++)
    {
        printf("Enter student name:\n");
        scanf("%s",&ptr[i]->name);
        printf("Enter student roll no.\n");
        scanf("%d",&ptr[i]->rollno);
    }
}
void print(struct student *ptr[], int n)
{
    printf("Students details are:\n");
    int i;
    for(i=0;i<n;i++)
    {
        printf("Student name is %s:\n",ptr[i]->name);
        printf("Student roll no. is %d:\n",ptr[i]->rollno);
    }
}
void main()
{
    accept(&s1,3);
    print(&s1,3);
}

推荐答案

您不需要所有地址& )运算符位于数组名称前面。

[edit]

已删除方法声明中的解除引用星号,并替换 - > for structure refs。

[/ edit]

尝试:

You do not need all those addressof (&) operators in front of your array names.
[edit]
Alse removed dereference asterisks on method declarations, and replaced -> by . for structure refs.
[/edit]
Try:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student
{
    char name[20];
    int rollno;
}s1[3];
 
void accept(struct student ptr[], int n) // removed *
{
    // printf("Students details are:\n");
    int i;
    for(i=0;i<n;i++)>
    {
        printf("Enter student name:\n");
        scanf("%s", ptr[i].name);  // no &, name is an array
        printf("Enter student roll no.\n");
        scanf("%d", &(ptr[i].rollno));  // need & here, rollno is simple variable
    }
}
void print(struct student ptr[], int n)
{
    printf("Students details are:\n");
    int i;
    for(i=0;i<n;i++)>
    {
        printf("Student name is %s:\n", ptr[i].name);
        printf("Student roll no. is %d:\n", ptr[i].rollno);
    }
}
void main()
{
    accept(s1,3);
    print(s1,3);
}


问题是ptr的参数声明:

The problem is the parameter declaration of ptr:
void accept(struct student *ptr[], int n) // wrong



你正在告诉编译器你正在传递一个指向学生结构的指针数组。但是在main中你并没有传递一个指针数组,而只是一组学生结构。因此,请将您的声明更改为:


You are telling the compiler that you are passing an array of pointers to student structures. But in main you are not passing an array of pointers, but simply an array of student structures. So change you declaration to:

void accept(struct student *ptr, int n)



打印功能相同。



正如Richard在帖子中所说,你可以取消所有那些&符号,因为数组名称同时是指向第一个元素的指针。


Same for the print function.

And as Richard said in his post, you can do away with all those ampersands, because an array name is at the same time a pointer to the first element.


正确的代码是:

The correct code is:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student
{
    char name[20];
    int rollno;
}s1[3];
 
void accept(struct student ptr[], int n)
{
    // printf("Students details are:\n");
    int i;
    for(i=0;i<n;i++)>
    {
        printf("Enter student name:");
        scanf("%s",ptr[i].name);
        printf("Enter student roll no.");
        scanf("%d",&(ptr[i].rollno));
    }
}
void print(struct student ptr[], int n)
{
    printf("Students details are:\n");
    int i;
    for(i=0;i<n;i++)>
    {
        printf("Student name is \"%s\" - roll no.=%d\n", ptr[i].name, ptr[i].rollno);
    }
}
int main(int argc, char *argv[])
{
    accept(s1,3);
    print(s1,3);

	return 0;
}



请注意,C中的数组名称是指向数组第一个元素的指针

所以在你的情况下,因为你正在使用一组结构,你必须传递数组的名称。

scanf()想要参数的指针,再次阅读学生姓名你正在使用一个数组(一组字符)。因此,您不需要获取地址,因为数组 ptr [i] .name 的名称是一个数组,并且已经是指针。但是当你得到滚动编号时,必须使用'&'运算符来获取不是数组的 ptr [i] .rollno 中的整数地址。

print()函数的相同过程。


Please note that in C an array name is a pointer to the first element of the array.
So in you case, because you're using an array of structures, you have to pass the name of the array.
scanf() want pointers to arguments, once again when reading the student name you are using an array (an array of chars). So you don't need to get the address because the name of the array ptr[i].name, is an array and is already a pointer. But when you get the roll number you must use the '&' operator to get the address of the integer in ptr[i].rollno that is not an array.
Same procedure for the print() function.


这篇关于我在编译下面的代码时得到了分段代码转储错误。我试图通过引用传递结构数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆