Re: pointer to struct



povoação wrote:
I modified to people[a] instead pointer and works well, but I´m
learning pointers and I can´t understand cause was not working with
pointer.
Well, think of it this way. You are allocating memory that will hold
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); and
i got error at compile time invalid conversion from void* to peoples*.

thanks
I cannot comment on the sizeof usage, as my perception differs a lot
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

.



Relevant Pages

  • Re: Implementing an interface
    ... pointer to pointer, which is supposed to contain the version number of ... I am not correctly allocating memory ... If the caller just expects a string with the version then you ...
    (comp.unix.programmer)
  • Re: When to use pointers?
    ... If 'b' were a pointer ... upon instantiation of the 'A' object - you would have to ... What all this really boils down to, is that allocating memory ... But what efficiency are ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Object creation
    ... pointer. ... Directly adding "a" as a member variable makes the design simple as ... you do not need to worry about allocating memory for a, ... EventStudio 2.5 - Automate sequence diagram generation ...
    (comp.lang.cpp)
  • C++ Interop, _gc Strings to _nogc char*
    ... existing pointer, rather than allocating memory and returning a new pointer? ... My target buffers are persistent must must not be allocated every time when ... Prev by Date: ...
    (microsoft.public.dotnet.languages.vc)
  • Re: OT: my new PC rocks!!
    ... Microsoft basically "owns" the ACPM thing and has the hardware ... for practically everything;) doing all the work and the "Windows" bit ... is merely the pretty graphics and mouse pointer routines to stare at ... actually seen it up and running to test if this "bug" is even older ...
    (alt.lang.asm)