+ 1
pass dynamic struct to function
So my task is to create a dynamic struct, fill it in with book name, author, year and page number(i already did this). Then I have to pass that struct to the function where we need to find the oldest book, and here is my problem, i cant figure out how to pass that struct to the function. This is my code so far: https://code.sololearn.com/cBk4a90CZOXz/#cpp
1 ответ
+ 2
It seems you haven't given your parameter a name.
struct knyga for now is only the type.
So in your definition it probably has to be struct knyga x (so you have something to refer to), and you'd call it by passing your object p.
To study the pattern, here a minimal example:
#include <iostream>
using namespace std;
struct s {int n;};
void f(struct s x) {cout << x.n;}
int main() {
struct s x{5};
f(x);
return 0;
}
So this is for regular stack, passed by value.
You can just pass the heap-created one, adapting the function for pointer or reference as you see fit.