Re: C programming - problem with structures
Jens.Toerring_at_physik.fu-berlin.de
Date: 02/01/05
- Next message: googlinggoogler_at_hotmail.com: "Re: C programming - problem with structures"
- Previous message: Arthur J. O'Dwyer: "Re: C programming - problem with structures"
- In reply to: googlinggoogler_at_hotmail.com: "C programming - problem with structures"
- Next in thread: Dave Neary: "Re: C programming - problem with structures"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 1 Feb 2005 16:37:31 GMT
googlinggoogler@hotmail.com wrote:
> Im new to C programming, im only young so excuse my stupidity! anyway,
> ive been mucking about with structures and pointers and stuff, and
> there really begining to mess with my little incapable head! can anyone
> answer my quick question as to why the program below crashes after its
> done its stuff?
> #include <stdio.h>
> struct Vectors
> {
> int X;
> };
> void function(Vectors *);
> int main()
Since in C a function with no arguments in the definition means a
function with an unspecified number of arguments it's probably
better to write that as
int main( void )
since main() doesn't seem to use any arguments.
> {
> struct Vectors v, *p; /*= NULL;*/
Now you have a single instance of your structure plus a pointer to
such a structure.
> p = &v;
And now 'p' points to the only structure you have.
> printf("Enter: ");
> scanf("%d", &p[1].X);
Here you try to read something into the structure immediately following
the one 'p' points to (remember, array indices start with 0 in C). But
since you only have a single structure you write to memory you don't own,
possibly writing over some other important data. That's the stuff (a
buffer overflow) that can make programs crash (if you are lucky, other-
wise it may seem to work but do something sneaky some time later, making
it rather ard to figure out why that happens...)
> printf(" %d\n", p[1].X);
> printf("Enter: ");
> scanf("%d", &p[2].X);
And now you write even further over memory which isn't yours...
> printf(" %d\n", p[2].X);
> function(p);
> return 0;
> }
To correct the whole thing you need to define an array of 'Vectors'
structures, in your case with at least three elements. Then the
program should work correctly.
BTW, there's also a newsgroup called comp.lang.c which deals with
the C language (but no extensions, just the language as according
to the C standard). If you're learning C reading it from time to
time may help you and give you a few ideas what to do and what to
avoid.
Regards, Jens
-- \ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de \__________________________ http://www.toerring.de
- Next message: googlinggoogler_at_hotmail.com: "Re: C programming - problem with structures"
- Previous message: Arthur J. O'Dwyer: "Re: C programming - problem with structures"
- In reply to: googlinggoogler_at_hotmail.com: "C programming - problem with structures"
- Next in thread: Dave Neary: "Re: C programming - problem with structures"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|