-1

Consider i have two function headers, where both functions returns array of structs by parameter:

void FunctionOne(int *count, MyStruct **my_structs);

void FunctionOne(int *&count, MyStruct *&my_structs);

What is the difference between these two notation? I searched but couldn't find these particular options compared and explained.

Ignas
  • 389
  • 2
  • 13
  • 1
    Once could be by coincidence valid C as well. The other is pure C++, and contains a subtle mistake. – StoryTeller - Unslander Monica Dec 07 '17 at 13:16
  • 4
    Do you know about *references*? Perhaps you should [get a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which will explain the concept? – Some programmer dude Dec 07 '17 at 13:17
  • 1
    What, **exactly**, is unclear about it? First one takes pointer to a pointer as a second argument, while other takes references to pointer arguments. – Algirdas Preidžius Dec 07 '17 at 13:17
  • I don't think it's useful to expect a reference question for every arbitrary combination of different types of basic syntax. – underscore_d Dec 07 '17 at 13:20
  • 2
    The difference between `int*` and `int*&` is the same as the difference between `int` and `int&`. The difference between `MyStruct**` and `MyStruct*&` is the same as the difference between `int*` and `int&`. – molbdnilo Dec 07 '17 at 13:50

1 Answers1

0

The first one is just pointers which means that you can't do count = new int(4) or assign it to new memory if you wish to return a changed memory location. The second one allows you to assign the pointers to new memory locations. This is because without a reference you can not use the assignment operator on the parameters.

Jake Freeman
  • 1,700
  • 1
  • 8
  • 15
  • You could very well use `count = new int(4)` in the first function. It's not necessarily what you want to do since you will lose the pointer to the data once you return from the function (unless you specifically make some other (member / global) variable point to the new data), but it's not incorrect. – xEric_xD Dec 07 '17 at 13:23
  • @xEric_xD you can but then you can't change the value of count at the end so it would not change to `*count == 4`. – Jake Freeman Dec 07 '17 at 13:26
  • If by "the end" you mean when the function has returned, then yes; that's true. But I think your answer might be clearer if you explain that the first functions makes a copy of the pointer to `count`, whereas the second one, as you explained, takes it by reference, which means that if you change what address `count` is pointing to in the second function, it will hold even after returning from the function. – xEric_xD Dec 07 '17 at 13:33
  • @xEric_xD ok I clarified in my answer. – Jake Freeman Dec 07 '17 at 13:35