Re: pointer to struct
- From: "matevzb" <matevzb@xxxxxxxxx>
- Date: 6 Dec 2006 11:25:56 -0800
povoação wrote:
I modified to people[a] instead pointer and works well, but I´mWell, think of it this way. You are allocating memory that will hold
learning pointers and I can´t understand cause was not working with
pointer.
three structures. malloc() returns the pointer to this memory and you
assign it to variable people. When you increment people (people++),
people will then point to the next element and you no longer have a
pointer to the allocated block:
| 0 | 1 | 2 | | 0 | 1 | 2 |
^ ^
people people
But in order to print out the values in the second loop, you do need
the original pointer, however people is not pointing there anymore.
Either access the allocated memory as an array (people[a]), or remember
the original pointer and after before the second loop, set the people
to point there.
I try modify also the line people=malloc(SIZE * sizeof *people); andI cannot comment on the sizeof usage, as my perception differs a lot
i got error at compile time invalid conversion from void* to peoples*.
thanks
from what Ben said. I never use sizeof without ()s and I never use it
on variables, but types insetad. The reason for that is the following:
char buf[10];
...
some_function (sizeof (buf));
If in future the buf type is changed from an array to a char *,
sizeof(buf) will get a _very_ different meaning. In small projects, the
bug will be easy to find, however once you have > 100,000 lines of code
and the crash only happens on a customer's machine to which you don't
have access (and, of course, the bug is not reproducible in the lab),
you could spend days before the problem is found. Been there, done
that. So, where Ben said "Don't do this!", I always do =)
--
WYCIWYG - what you C is what you get
.
- References:
- pointer to struct
- From: povoação
- Re: pointer to struct
- From: matevzb
- Re: pointer to struct
- From: povoação
- pointer to struct
- Prev by Date: Re: pointer to struct
- Next by Date: Re: pointer to struct
- Previous by thread: Re: pointer to struct - TPA
- Next by thread: Re: pointer to struct
- Index(es):
Relevant Pages
|