结构,指针和获取 [英] Structures, Pointer and gets

查看:58
本文介绍了结构,指针和获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试学习C,但我正在努力使用scanf和struct。我想要定义一个结构并在int main中声明该类型的变量。

这必须传递给一个函数,它的值必须读取并返回

来自这个函数。你如何使用gets()?我只是无法想象

out。


这里有一些代码


typedef struct sStudent

{

char *姓名;

char * ID;

} t学生;


void ReadStudents(t学生[10])


int main()

{

tStudent学生[10];

ReadStudents(学生);

}


void ReadStudents(tStudent student [10])

{

????????????

}

解决方案

首先,如果你想要发回一些东西,你的功能必须是

无效或你可以使用它无效但通过引用打电话(传递地址)

因为您可能知道是否按值传递变量,它会创建一个

副本,该副本在被调用函数本身的末尾消失:所以或返回值

或者在地址上工作。


其次,在ReadStudent原型中你应该声明一个10的数组

元素。数组只是C的指针。当你最后将数组传递给

函数时,你只需传递第一个元素的位置内存,

然后你就可以访问其他人用指针算术。

struct sStudent {

char *姓名;

char * ID;

} t学生[10];


typedef struct s学生; //对不起我讨厌typedef ... :)

void ReadStudents(student *);


void ReadStudents(student * myStud){


printf(" Plz输入你的名字:");

得到(myStud->名字);

}

我正在尝试学习C,但我正在努力使用scanf和struct。
我想定义一个结构并在int
main中声明该类型的变量。必须将其传递给函数,并且必须读取其值并从此函数返回
。你如何使用gets()?我只是想不到它。

这里有一些代码

typedef struct sStudent
{
char *姓名;
char * ID;
} tStudent;

void ReadStudents(t学生[10])

int main()
{
学生[10];
ReadStudents(学生);
}
void ReadStudents(t学生[10])
{
???????????



Riaan Cillie写道:
< blockquote class =post_quotes>嗨

我正在努力学习C,但我正在努力使用scanf和struct。
我想定义一个结构并声明一个变量在int
main中的那种类型。这必须传递给一个函数,它的值必须从这个函数读取并返回。你如何使用gets()?我只是无法弄明白。


首先,你不要使用gets()。永远。它无法保护您的

缓冲区不被过多的数据覆盖。


其次,char *不是C-speak forstring 。它只是指向

字符的指针。它/可以/用于指向一堆字符中的第一个

连续,但它实际上并没有保留任何空间。您必须将
指向有效空间,或使用正确的数组。现在,让我们使用

数组:


这里有一些代码


很棒!让我们来看看......

typedef struct sStudent
{
char *姓名;
char * ID;
} tStudent;


我建议暂时将其替换为:


#define NAME_LEN 32

#define ID_LEN 16

typedef struct sStudent

{

char name [NAME_LEN];

char ID [ID_LEN];

} t学生;

void ReadStudents(t学生[10])

int main()
{
t学生[10];
ReadStudents(学生);
}
void ReadStudents(t学生[10])

??????????
}




而不是将ReadStudents函数锁定为10个元素的数组,为什么

没有传递大小?这将需要将main()中的呼叫更改为:


ReadStudents(学生,sizeof学生/ sizeof学生[0]);


这是一个可能的ReadStudents实现。它不是完全健壮,

但它会告诉你一般的想法:


void ReadStudents(tStudent student [],size_t NumStudents)

{

size_t ThisStudent = 0;


while(ThisStudent< NumStudents)

{

put(请输入学生的姓名。);

fgets(学生[ThisStudent] .Name,

sizeof学生[ThisStudent] .Name,

stdin);

学生[ThisStudent] .Name [strlen(学生[ThisStudent] .Name) - 1] =''\ 0 '';

put(请输入学生的ID。);

fgets(学生[ThisStudent] .ID,

sizeof学生[ThisStudent] .ID,

stdin);

学生[ThisStudent] .ID [strlen(学生[ThisStudent] .ID) - 1] = ''\''';

++ ThisStudent;

}

}


我没有讨论过的问题:


1)fgets可能会失败(如果有的话会返回NULL)。

2)用户可能输入的数据多于缓冲区可容纳的数据。与gets()不同,

fgets()不允许这会超出你的缓冲区,但是你会在后续调用中得到奇怪的结果(因为剩下的数据将是

留在缓冲区中。


-

Richard Heathfield: bi **** @ eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


RAiN写道:

首先,如果你想要发回一些东西你的功能不能无效或者你可以使用它无效,但通过引用打电话(传递地址)


在C中没有参考号召唤。


碰巧,他正在使用一个数组,所以无论如何它都是一个没有实际意义的点(因为一个数组的

名称会衰减成指向其第一个元素的指针)。

原因正如您可能知道的那样,如果您按值传递变量,它会创建一个
副本,该副本会在被调用函数本身的末尾消失:所以或者返回
值或者在地址。

其次,在ReadStudent原型中你应该声明一个包含10个元素的数组。数组只是C的指针。


不正确。如果是真的,数组将具有与指针相同的大小,并且

它们不是(除非巧合)。

当您将数组传递给<时br />函数到底你只是传递了第一个
元素的位置内存,然后用指针算术访问其他元素。


这至少是正确的。


< snip>

void ReadStudents(student * myStud ){

printf(" Plz insert your name:");
获取(myStud-> Name);
}




未定义的行为,保证。除了使用gets()函数

(严重破坏)之外,您忘记为Name保留任何存储空间。


-

Richard Heathfield: bi****@eton.powernet.co.uk
Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


Hi

I''m trying to learn C, but I am struggling with using scanf and a struct. I
want to define a structure and declare a variable of that type in int main.
This has to be passed to a function and its values have to read and returned
from this function. How do you use gets() for this? I just can''t figure it
out.

Here is a bit of code

typedef struct sStudent
{
char *Name;
char *ID;
} tStudent;

void ReadStudents(tStudent student[10])

int main()
{
tStudent student[10];
ReadStudents(student);
}

void ReadStudents(tStudent student[10])
{
????????????
}

解决方案

Well first if you want something to be sent back your function mustnt be
void or you can use it void but call by reference (pass the address)
Cause as you probably know if you pass a variable by value, it creates a
copy which dies at the end of the called function itself: so or return value
or work on the address.

Second, in the ReadStudent prototype you should declare an array of 10
elements. Arrays are just pointers for C. When you pass an array to a
function in the end you just pass the location memory of the first element,
then you access to the others with pointers arithmetics.
struct sStudent{
char *Name;
char *ID;
} tStudent[10];

typedef struct sStudent student; //sorry i hate typedef... :)
void ReadStudents(student *);

void ReadStudents(student *myStud){

printf("Plz insert your name: ");
gets(myStud->Name);
}

I''m trying to learn C, but I am struggling with using scanf and a struct. I want to define a structure and declare a variable of that type in int main. This has to be passed to a function and its values have to read and returned from this function. How do you use gets() for this? I just can''t figure it
out.

Here is a bit of code

typedef struct sStudent
{
char *Name;
char *ID;
} tStudent;

void ReadStudents(tStudent student[10])

int main()
{
tStudent student[10];
ReadStudents(student);
}

void ReadStudents(tStudent student[10])
{
????????????
}



Riaan Cillie wrote:

Hi

I''m trying to learn C, but I am struggling with using scanf and a struct.
I want to define a structure and declare a variable of that type in int
main. This has to be passed to a function and its values have to read and
returned from this function. How do you use gets() for this? I just can''t
figure it out.
For a start, you don''t use gets(). Ever. It has no way to protect your
buffer from being overrun with too much data.

Secondly, char * is not C-speak for "string". It is merely a pointer to a
character. It /can/ be used to point at the first of a bunch of characters
in a row, but it doesn''t actually reserve any space itself. You must either
point it to valid space, or use a proper array instead. For now, let''s use
an array:


Here is a bit of code
Great! Let''s take a look...

typedef struct sStudent
{
char *Name;
char *ID;
} tStudent;
I suggest replacing this, for the time being, with:

#define NAME_LEN 32
#define ID_LEN 16

typedef struct sStudent
{
char Name[NAME_LEN];
char ID[ID_LEN];
} tStudent;

void ReadStudents(tStudent student[10])

int main()
{
tStudent student[10];
ReadStudents(student);
}

void ReadStudents(tStudent student[10])
{
????????????
}



Rather than lock the ReadStudents function down to a 10-element array, why
not pass in the size? This will entail changing the call, in main(), to:

ReadStudents(student, sizeof student / sizeof student[0]);

Here''s a possible ReadStudents implementation. It''s not completely robust,
but it will show you the general idea:

void ReadStudents(tStudent student[], size_t NumStudents)
{
size_t ThisStudent = 0;

while(ThisStudent < NumStudents)
{
puts("Please enter the student''s name.");
fgets(student[ThisStudent].Name,
sizeof student[ThisStudent].Name,
stdin);
student[ThisStudent].Name[strlen(student[ThisStudent].Name) - 1] = ''\0'';
puts("Please enter the student''s ID.");
fgets(student[ThisStudent].ID,
sizeof student[ThisStudent].ID,
stdin);
student[ThisStudent].ID[strlen(student[ThisStudent].ID) - 1] = ''\0'';
++ThisStudent;
}
}

Problems I haven''t discussed:

1) fgets might fail (it returns NULL if it does).
2) User might type in more data than the buffer can hold. Unlike gets(),
fgets() will not allow this to overrun your buffer, but you will get
strange results on subsequent calls (because the remaining data will be
left in the buffer).

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


RAiN wrote:

Well first if you want something to be sent back your function mustnt be
void or you can use it void but call by reference (pass the address)
There is no such thing as call by reference in C.

As it happens, he''s using an array, so it''s a moot point anyway (since the
name of an array decays into a pointer to its first element).
Cause as you probably know if you pass a variable by value, it creates a
copy which dies at the end of the called function itself: so or return
value or work on the address.

Second, in the ReadStudent prototype you should declare an array of 10
elements. Arrays are just pointers for C.
Not true. If it were true, arrays would have the same size as pointers, and
they don''t (except by coincidence).
When you pass an array to a
function in the end you just pass the location memory of the first
element, then you access to the others with pointers arithmetics.
That, at least, is correct.

<snip>
void ReadStudents(student *myStud){

printf("Plz insert your name: ");
gets(myStud->Name);
}



Undefined behaviour, guaranteed. Quite apart from using the gets() function
(which is severely broken), you forgot to reserve any storage for Name.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


这篇关于结构,指针和获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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