0

I am learning c, however i understand the concept of pointers but here i am having trouble.What is mean by a pointer in the definition of a function. Here v is a local pointer defined within the function so returning it means that only the values will be copied and returned or a pointer to that block will be returned because what i also learnt that local variables have only life time until the function executes. Will someone like to explain, will be a great help.

struct verpl * nieuw (int van, int naar, int aantal, register struct verplaatsing * lijst)
{
   register struct verpl * v = (struct verpl *) alloc(sizeof(struct verpl));
   aNieuw++;
   v->van = van;
   v->naar = naar;
   v->aantal = aantal;
    v->volg = lijst; // plaats het element vooraan in de lijst
     return (v);
}

Function call

 lijst = nieuw (via, huidig->naar, huidig->aantal - 1, lijst);
Afraz Salim
  • 57
  • 1
  • 7

1 Answers1

0

What is returned is the pointer value returned from alloc().

v is local and returning a pointer pointing to v is not good, but what v is pointing is not local, so returning it is valid.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • `alloc()` ? isn't that `malloc` ? if it's `alloca` then there's a problem. – Jean-François Fabre Aug 19 '17 at 13:35
  • @Jean-FrançoisFabre `alloc()` exists as non-standard library. [c - alloc, malloc, and alloca — What's the difference? - Stack Overflow](https://stackoverflow.com/questions/32685851/alloc-malloc-and-alloca-whats-the-difference) I don't know if it is typo of `malloc` or `alloca`. – MikeCAT Aug 19 '17 at 13:37
  • if it's a typo of `alloca` then the code is wrong as it returns a reference on temporary memory. – Jean-François Fabre Aug 19 '17 at 13:40