Re: C programming - problem with structures

Jens.Toerring_at_physik.fu-berlin.de
Date: 02/01/05


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


Relevant Pages

  • Re: I need a new compiler...
    ... which is the definition of the C language. ... Pointers, and address and indireciton operators were ... and are maintained through all current implementations. ... indirection operators provide access to system memory. ...
    (comp.lang.cobol)
  • Re: new here, my lang project...
    ... I feel general, as I am unsure, eg, how a language like c would map to ... on the way structs are layed out in memory, the way pointers are ... such a vm will typically manage it's own memory, ... one can't put any more data in a struct, doing so would break the app. ...
    (comp.object)
  • OT: concrete and clay WAS: Re: I need a new compiler...
    ... which is the definition of the C language. ... find that it does not require implementations to "provide ... indirection operators provide access to system memory. ... Pointers in C very much are address operators, in every way, shape, ...
    (comp.lang.cobol)
  • Re: Newbie look at Python and OO
    ... Python, however, feels strange. ... addresses as pointers to data, and also as pointers to pointers. ... Although, when I first started assembly language, I think it took me a ... You shouldn't need to think about memory addresses in Python. ...
    (comp.lang.python)
  • The Decline of C/C++, the rise of X
    ... There have been many languages that have tried to become that language ... 100+MB memory consumption in any non-trivial desktop apps - Java sure ... It made some improvements to C/C++ (e.g automatic ... **Pointers and the clinically insane ...
    (comp.programming)