指向struct的指针 [英] pointer to struct

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

问题描述

无法理解为什么上面的程序不起作用。

谢谢所有

#include< stdio.h>

#include < stdlib.h>


#define SIZE 3

struct peoples

{

int code;

char name [20];

浮动值;

};


int main()

{

int a;

struct peoples * people;

people = (struct peoples *)malloc(SIZE * sizeof(struct peoples));

for(a = 0; a< SIZE; a ++)

{

people-> code = a;

scanf("%s",people-> name);

scanf("%f", people-> value);

people ++;

}

for(a = 0; a< SIZE; a ++)

{

printf("%d",people-> code);

printf("%s",people-> name) ;

printf("%f",people-> value);

people ++;

}

}

解决方案

你只有struct和一个指针。为什么要递增

指针?你为什么要循环?也许你需要一个结构数组3

(SIZE)指针并需要遍历数组来分配结构,

循环来赋值,然后循环来访问值。


12月6日上午9:48,povoa ?? o < okle ... @ gmail.comwrote:


无法理解为什么上面的程序不起作用。

谢谢所有


#include< stdio.h>

#include< stdlib.h>


#define SIZE 3


struct peoples

{

int code;

char name [20];

浮动值;


}; int main()

{

int a;

struct peoples * people;

people =(struct peoples *)malloc(SIZE * sizeof(struct peoples));

for(a = 0; a< SIZE; a ++)

{

people-> code = a;

scanf("%s",people-> ;姓名);

scanf("%f",people-> value);

people ++;

}

for(a = 0; a< SIZE; a ++)

{

printf("%d",people-> code);

printf("%s",people-> name);

printf( %f,people-> value);

people ++;

}


}

"波瓦12 O" < ok ***** @ gmail.comwrites:


int main()



I ''建议使用原型表格:

int main(void)


{

int a;

struct peoples * people;



这是一个非常有创意的命名,为指向

数组的开头提供一个相对单一的名称,并且

结构类型,数组包含相对复数的名称。

大多数程序员可能会采用不同的方式,例如

struct person * people;


people =(struct peoples *)malloc(SIZE * sizeof(struct peoples));



我不建议转换malloc()的返回值:


*演员表不是必需的ANSI C.


*转换它的返回值可以掩盖#include

< stdlib.h>的失败,这会导致未定义的行为。


*如果你偶然输入了错误的类型,奇怪的失败可以

结果。


在特殊情况下它可能有意义来转换

malloc()的返回值。例如,PJ Plauger有充分的理由想要将他的

代码编译为C和C ++,并且C ++需要演员,因为他在文章中解释了&b
。 9s******************@nwrddc01.gnilink.net>。

然而,Plauger的情况确实很少见。大多数程序员应该把他们的代码编写为C或C ++,而不是两者的交集。


调用malloc()时,我建议使用sizeof运算符在

上分配的对象,而不是类型。例如,

*不要写这个:


int * x = malloc(128 * sizeof(int)); / *不要这样做! * /


相反,请这样写:


int * x = malloc(128 * sizeof * x);


这样做有几个理由:


*如果你改变了'x'指向的类型,它'''更改malloc()调用也不需要



这在大型程序中更是一个问题,但它仍然是

方便小一点。


*考虑一个对象的大小使得编写语句

不易出错。您可以验证sizeof语法是否正确无需查看声明。


for(a = 0; a< SIZE ; a ++)

{

people-> code = a;

scanf("%s",people-> name) ;



您需要指定最大长度,否则可能导致

缓冲区溢出。


scanf("%f",people-> value);



您忘了带地址:& people-> value。


people ++;

}

for(a = 0; a< SIZE; a ++)

{

printf("%) d",people-> code);

printf("%s",people-> name);

printf("%f",people - > value);

people ++;

}



这不会起作用,因为在你的第一个循环你已经

高级人过去你想要打印的记录。我会给b $ b建议放弃people ++。来自两个循环并使用数组

索引符号代替每个成员引用,

例如人[a] .code。


}



-

在我的Egotistical意见中,大多数人的C程序应该向下缩进六英尺b $ b英尺向下并覆盖着污垢。 - Blair P. Houghton


povoa ?? o写道:


无法理解为什么程序以上不起作用。



出于多种原因。


people =(struct peoples *)malloc(SIZE * sizeof(struct人们));

for(a = 0; a< SIZE; a ++)

{

people-> code = a;

scanf("%s",people-> name);

scanf("%f",people-> value);

people ++;

}



你们正在增加人们(指向结构人的指针),这是在
之后
首先为循环点指向哪里?你会更好:

人[a] .code = a;

其次,scanf()参数应该是指针所以使用:

scanf("%f",& people [a] .value);

对于char数组,您可以使用people [a] .name或

& people [a] .name [0]。有些人更喜欢第二个版本。

第三,你没有检查scanf()返回值。如果某人

没有按预期进入浮点数,你的程序就行为不端。

另外,你的人[a] .name限制为20个字符,如果

有人输入的内容超过了 - 请查看scanf()手册页。


}

for(a = 0; a< SIZE; a ++)

{

printf("%d",people-> code);

printf("%s",people-> name);

printf("%f",people-> value);

people ++;

}



再次,不要增加人数,使用人[a]。现在输出换行符

然后也可能是一件好事。


can′t understand why the program above not works.
thanks all
#include <stdio.h>
#include <stdlib.h>

#define SIZE 3

struct peoples
{
int code;
char name[20];
float value;
};

int main()
{
int a;
struct peoples *people;
people=(struct peoples *)malloc(SIZE*sizeof(struct peoples));
for(a=0;a<SIZE;a++)
{
people->code=a;
scanf("%s",people->name);
scanf("%f",people->value);
people++;
}
for(a=0;a<SIZE;a++)
{
printf("%d",people->code);
printf("%s",people->name);
printf("%f",people->value);
people++;
}
}

解决方案

You only have on struct and one pointer. Why are you incrementing the
pointer? Why are you looping? Maybe you need an array of structs 3
(SIZE) pointers and need to loop through the array to allocate structs,
loop to assign values, then loop to acces the values.

On Dec 6, 9:48 am, "povoa??o" <okle...@gmail.comwrote:

can′t understand why the program above not works.
thanks all

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

#define SIZE 3

struct peoples
{
int code;
char name[20];
float value;

};int main()
{
int a;
struct peoples *people;
people=(struct peoples *)malloc(SIZE*sizeof(struct peoples));
for(a=0;a<SIZE;a++)
{
people->code=a;
scanf("%s",people->name);
scanf("%f",people->value);
people++;
}
for(a=0;a<SIZE;a++)
{
printf("%d",people->code);
printf("%s",people->name);
printf("%f",people->value);
people++;
}

}


"povoa??o" <ok*****@gmail.comwrites:

int main()

I''d recommend using the prototype form:
int main(void)

{
int a;
struct peoples *people;

This is very creative naming, to give the pointer to the
beginning of an array a relatively singular name, and the
structure type that the array contains a relatively plural name.
Most programmers would probably do it differently, e.g.
struct person *people;

people=(struct peoples *)malloc(SIZE*sizeof(struct peoples));

I don''t recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.

In unusual circumstances it may make sense to cast the return value of
malloc(). P. J. Plauger, for example, has good reasons to want his
code to compile as both C and C++, and C++ requires the cast, as he
explained in article <9s*****************@nwrddc01.gnilink.net>.
However, Plauger''s case is rare indeed. Most programmers should write
their code as either C or C++, not in the intersection of the two.

When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don''t* write this:

int *x = malloc (128 * sizeof (int)); /* Don''t do this! */

Instead, write it this way:

int *x = malloc (128 * sizeof *x);

There''s a few reasons to do it this way:

* If you ever change the type that `x'' points to, it''s not
necessary to change the malloc() call as well.

This is more of a problem in a large program, but it''s still
convenient in a small one.

* Taking the size of an object makes writing the statement
less error-prone. You can verify that the sizeof syntax is
correct without having to look at the declaration.

for(a=0;a<SIZE;a++)
{
people->code=a;
scanf("%s",people->name);

You need to specify a maximum length, or this can lead to a
buffer overflow.

scanf("%f",people->value);

You forgot to take the address: &people->value.

people++;
}
for(a=0;a<SIZE;a++)
{
printf("%d",people->code);
printf("%s",people->name);
printf("%f",people->value);
people++;
}

This isn''t going to work, because in your first loop you already
advanced "people" past the records you''re trying to print. I''d
suggest dropping the "people++" from both loops and using array
index notation instead for each member reference,
e.g. people[a].code.

}


--
"In My Egotistical Opinion, most people''s C programs should be indented six
feet downward and covered with dirt." -- Blair P. Houghton


povoa??o wrote:

can′t understand why the program above not works.

For many reasons.

people=(struct peoples *)malloc(SIZE*sizeof(struct peoples));
for(a=0;a<SIZE;a++)
{
people->code=a;
scanf("%s",people->name);
scanf("%f",people->value);
people++;
}

You''re incrementing people (pointer to struct peoples), which after the
first for loop points to where? You''d be better of with:
people[a].code = a;
Secondly, scanf() arguments should be pointers so use:
scanf ("%f", &people[a].value);
For char arrays, you can use either people[a].name or
&people[a].name[0]. Some prefer the second version.
Thirdly, you''re not checking for scanf() return values. If someone
doesn''t enter a float when you expect it, your program will misbehave.
Also, your people[a].name is limited to 20 characters, it won''t work if
someone enters more than that - check the scanf() man page.

}
for(a=0;a<SIZE;a++)
{
printf("%d",people->code);
printf("%s",people->name);
printf("%f",people->value);
people++;
}

Again, don''t increment people, use people[a]. Outputting a newline now
and then may also be a good thing.


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

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