Re: Structures, Pointer and gets

From: Richard Heathfield (dontmail_at_address.co.uk.invalid)
Date: 11/15/03


Date: Sat, 15 Nov 2003 16:13:30 +0000 (UTC)

RAiN wrote:

> Well first if you want something to be sent back your function mustnt be
> void or you can use it void but call by reference (pass the address)

There is no such thing as call by reference in C.

As it happens, he's using an array, so it's a moot point anyway (since the
name of an array decays into a pointer to its first element).

> Cause as you probably know if you pass a variable by value, it creates a
> copy which dies at the end of the called function itself: so or return
> value or work on the address.
>
> Second, in the ReadStudent prototype you should declare an array of 10
> elements. Arrays are just pointers for C.

Not true. If it were true, arrays would have the same size as pointers, and
they don't (except by coincidence).

> When you pass an array to a
> function in the end you just pass the location memory of the first
> element, then you access to the others with pointers arithmetics.

That, at least, is correct.

<snip>

> void ReadStudents(student *myStud){
>
> printf("Plz insert your name: ");
> gets(myStud->Name);
> }

Undefined behaviour, guaranteed. Quite apart from using the gets() function
(which is severely broken), you forgot to reserve any storage for Name.

-- 
Richard Heathfield : binary@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Relevant Pages

  • Re: Realloc and pointer arithmetics
    ... through an AVL tree that managed void* data. ... the C standard does not. ... My frist thought was actually to store into the tree pointers to array ...
    (comp.lang.c)
  • Re: Linked list or realloc()?
    ... > I'm currently doing a project that has a array that supposed to be ... > LinkedListNode as needed and use freeto destroy a node. ... around the nodes, you will sort the array of pointers, search the ... int cmp(const void *v1, const void *v2); ...
    (comp.lang.c)
  • Re: how to avoid passing by reference and how to copy objects
    ... thats basically copying the pointers themselves. ... reference to the Array. ... cannot take a "reference" to a or b. ... create some NEW pointers but which point to the same memory space. ...
    (comp.lang.ruby)
  • Re: C newbie in a jam
    ... Richard Heathfield skrev: ... void, not VOID ... The first is a pointer to an array of ROW x COL doubles. ... attempt to define an array of ROW x COL pointers to double - an attempt ...
    (comp.lang.c)
  • Re: Is it possible to make void * safer?
    ... array_push(array, (void*)data);} ... The cast to (void *) looks ... I was specifically demonstrating how to mimic template in C with macros. ... That's why I added the function pointers, ...
    (comp.lang.c)