-1

I am currently learning c++ from books, but I found some code, which I can't quite describe. It is also possible it's a mixture of c/c++.

It's this one:

structure  *readThingsFromFile(structure *arrayOfThings, int &arraySize)
{
}

I understand that it returns structure (or ir returns pointer to the structure?). Basically I have no idea what the first star '*' means and also what does the '&' means.

Thanks

Paradoxis
  • 49
  • 2
  • 5

3 Answers3

1

There is no such thing as a "mixture of C and C++". Code is either C, or C++, or some other language of course.

This is C++, since the use of & declares arraySize as being a reference. This means that the code in readThingsFromFile() can change the value of arraySize, and the caller will "see" this change too since the function has a reference to the caller's argument.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

Just for the sake of it, let's replace structure with char (for the moment), since I think you have also some troubles with the type structure. Then we have a function like this:

char *readThingsFromFile(char *arrayOfThings, int &arraySize)
{
// Some Code
}

The first char * describes the type of the return value from your function readThingsFromFile.

The return value is the value you pass to the function-caller, when you close your function. This is what you usually see in Code as return var;

In your particular case, this return value is from the type char *. The * determines it as a pointer, so when the function closes it returns a character-pointer.

Next, there are your two function arguments:

char *arrayOfThings:

Obviously, this is also an character pointer, only that at this point it's an function argument, which means you have to supply it to the function.

int &arraySize:

The & determines the required parameter as a so called reference (in this case to an integer). Reference means, that the functions expects a normal variable (here: just a normal integer, no pointer), but it will treat it as a reference, which means that internally it actually just uses your original variable. This is important for you to know, since this means that changes to the variable in your function are global and not only local in the function itself (remember: when you pass normal parameters to a function you can just change them, without seeing any of that in the calling function).

Now to come back to structure - regarding the * and & operators, it behaves the same for structure as it would do for char or int (it's just a pointer/reference to a structure theN). The structure itself just reprents a data type in this case.

Toby
  • 3,815
  • 14
  • 51
  • 67
0

It returns pointer to something named structure (a type). It takes two arguments - pointer to something named structure (a type) and reference to int.

nyrl
  • 769
  • 5
  • 15