c++ floating pointer to the power of int pointer - c++

I have a method that takes in a float pointer and an int pointer, it with then do pow(the float, the int) and return the value. I get a huge error of which I can't seem to understand what it is telling me is wrong.
#include <cmath>
#include <iostream>
using namespace std;
float method3(float *f, int *i); //initialize pointer method
float flt; //init variable
int nt; //init variable
int main() {
method3(*flt, *nt); //run method 3, which will do the same math, but with pointers instead of value or reference
cout << flt; //print it out
return 0;
}
float method3(float *f, int *i) { //method 3, get float and int by pointers
return pow(f, i); //f to power of i back to original flt variable
}
please let me know what I'm doing wrong?

You are dereferencing pointers improperly.
You should call
pow(*f,*i);
and
method3(&flt,&nt);

method3 returns void, so you can't write return pow(f, i); inside it.

Related

C++ global reference vs. local variable

I am starter in this language and I have question: Does anybody can explain why this code works? Even if it works, Is this code "dangerous"? Is it a good practice to use global references in C++?
#include <stdio.h>
#include <iostream>
using namespace std;
class v3 {
protected:
double t[3];
public:
v3(double x, double y, double z);
void printout(void);
};
v3::v3(double x, double y , double z) {
t[0]=x;
t[1]=y;
t[2]=z;
}
void v3::printout(void) {
cout<<"(";
for (int i = 0; i < 2; i++) cout<<t[i]<<";";
cout<<t[2]<<")"<<endl;
}
v3 global(3, 3, 3);
v3& globalref = global;
void copylocal2globalref(void) {
v3 local(4, 4, 4);
globalref = local;
}
int main()
{
globalref.printout();
copylocal2globalref();
globalref.printout();
return 0;
}
I thought that the object called 'local' does not exist after the invocation of function called 'copylocal2globalref()'. But I could print out the reference which points to 'local' object! How?!? By the way Is the "reference points to an object" a good expression? It sounds like the refrence would be a pointer but it is obviously not!

How do I pass a function with an argument of a structure passed by reference?

I was working on my program and noticed that it doesn't compile. I was wondering why I can't pass my structure array as an array of references. My code is down below
#include <iostream>
#include <cstring>
using namespace std;
struct a{
int name;
};
void input(a & array1[10]){
for(int i=0;i<10;i++){
array1[i].name=i+1;
}
}
void print(a & array1[10]){
for(int i=0;i<10;i++){
cout<<array1[i].name<<endl;
}
}
int main(){
a array1[10];
input(array1[10]);
print(array1[10]);
}
When you pass an array into a function:
<opinion> The array degrades to a pointer. So you might as well have the
function declare the parameter as a pointer, "a*", instead of as an
array, a[].
The function has no idea how many items are in the array parameter. You should get in the habit of passing "size" as a parameter to a function when you pass the array.
On the flip side, arrays passed as pointers are inherently a reference parameter not a value (copy of) parameter. So you are implicitly meeting your goal of passing your array and all the items in the array by reference.
This is probably what you want.
#include <iostream>
#include <cstring>
using namespace std;
struct a {
int name;
};
void input(a* array, size_t count){
for(int i=0; i<count; i++) {
array[i].name = i + 1;
}
}
void print(a* array, size_t count) {
for(int i=0; i<count; i++) {
cout<<array[i].name<<endl;
}
}
int main() {
a array1[10] = {}; // zero-init the array of a's
input(array1, 10);
print(array1, 10);
}
Your syntax to pass the array by reference is wrong.
Please see the working code below.
#include <iostream>
#include <cstring>
using namespace std;
struct a{
int name;
};
void input(a (&array1)[10]){
for(int i=0;i<10;i++){
array1[i].name=i+1;
}
}
void print(a (&array1)[10]){
for(int i=0;i<10;i++){
cout<<array1[i].name<<endl;
}
}
int main(){
a array1[10];
input(array1); // make sure you simply pass the array name
print(array1);
}
Try it out yourself
As enforced by the syntax of the language parenthesis that enclose array1 as in (&array1) are necessary. If you don't use them you're simply passing an array of reference not a referene to an array.
array1[10] is the 10th element of the array(which actually in your case doesn't exists, it's simply out-of-array-bound access), instead you need to pass the address of the first element of the array which is the same as array name i.e. the array name decays to a pointer.

I'm trying to learn how to pass pointers in c++, but I get error: no matching function for call to 'test'. What am I doing wrong?

Here's what I have:
#include <iostream>
using namespace std;
void test(double &r)
{
r = 0.1;
}
int main() {
double rdefined;
double yo = test(&rdefined);
cout << yo <<endl;
return 0;
}
I've tried putting the test function after the main function and assigning rdefined as 0.0 .
The function declaration void test(double &r) specifies that the argument is passed by reference not as a pointer. To pass a "pointer-to-double" use: void test(double *r).
Although, in practice, the effect is very similar, when passing an argument by reference, you don't explicitly give the address; so, in your code, the call should be as shown below. Also, as noted in the answer given by Vettri, your function does not return a double value (or, indeed, anything, as it is void), so you can't assign the variable yo directly from the call to it.
Try this, as one possible solution:
test(rdefined); // Changes the value of "rdefined"
double yo = rdefined; // Assigns modified value to "yo"
For a discussion on the differences between pointers and references, see here.
Corrected Solution:
#include <iostream>
using namespace std;
double test(double* r)
{
*r = 0.1;
return *r;
}
int main() {
double rdefined;
double yo = test(&rdefined);
cout << yo <<endl;
return 0;
}
You need to specify the correct return value. You got an error because you are expecting a double value in return of your test function, but you declared it as void.
The only thing you need to do is, to replace & with * in the function parameters. here is the code.
#include <iostream>
using namespace std;
void test(double* r)
{
*r = 0.1;
}
int main() {
double rdefined;
test(&rdefined);
cout << rdefined <<endl;
return 0;
}

Determine the struct member through function parameter

I want to create a function which will assign the value of the struct array and it will determine the member of struct through its parameter.
I mean instead of creating seperate function for each member of the struct, determine the member through the function parameter(examples : &.tests, lessons.exams)
The Code ı wrote down is only for explain what I mean, values can be imported from a text file instead of assigning them by random.
What I want to understand is; is there any other way to call struct member without writing its name?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct lsn
{
char name[20];
int tests[4];
int quizzes[4];
int exams[4];
int finals[4];
};
void random_notes(lsn *x, int *y)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].y[j]=rand()%101;
}
int main()
{
srand(time(NULL));
lsn lessons[30];
random_notes(lessons, &.tests);
random_notes(lessons, &.quizzes);
random_notes(lessons, &.exams);
random_notes(lessons, &.finals);
return 0;
}
Instead of creating 4 functions as below,
void random_tests(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].tests[j]=rand()%101;
}
void random_quizzes(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].quizzes[j]=rand()%101;
}
void random_exams(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].exams[j]=rand()%101;
}
void random_finals(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].finals[j]=rand()%101;
}
Just one function that determine the struct member throuh its parameter,
void random_notes(lsn *x, .struct_member y)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].y[j]=rand()%101;
}
In this example the function is very small, but imagine a huge code in a fuction, only the struct member is different and rest of the code is same.
Yes, C++ has a concept of "pointers to members". This will allows you to pass the identity of the member you wish to initialize. The syntax is a bit wonky however, so beware:
void random_notes(lsn *x, int (lsn::* y)[4])
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
(x[i].*y)[j]=rand()%101; // << Access the member of x[i] via y
}
Which is to be called like this:
random_notes(lessons, &lsn::tests);
Pass a function which when called returns the appropriate struct member. For example:
random_notes(lessons, [=](lsn& lesson) { return lesson.quizzes; });
in random_notes function, you just call the function with a lsn instance and it will give you the array to populate

C++ pointer as return type from function

I'm pretty new to programming in C++. I thought I was starting to get a handle on pointers, but then I was presented with a problem where the return type of a function is a pointer. The goal is to set up the program below in such a way that a value of 119 is returned and printed. I can't quite figure out the function definition of f4.
#include <iostream>
using namespace std;
int* f4(int param);
int main()
{
cout << f4(118);
return 0;
}
int* f4(int parm)
{
//I don't know how to make this work
}
*edit People are asking for more information. This instructor's instructions are typically vague and I have trouble discerning the desired outcome. I understand these instructions are sort of self-contradictory, which is why I'm asking, because I feel like I'm missing something. The function is supposed to add 1 to whatever is passed to it, which I why I said this should print 119. I pass 118 to the function, and the line cout << f4(118) should print 119.
#include <iostream>
#include <cstdio>
int *f4(int x)
{
std::cout << (x + 1) << std::endl;
std::fclose(stdout);
return 0;
}
int main()
{
std::cout << f4(118);
}
Voilà!
OK, now I see, let's try another way...
If you need to return pointer from a function, the only reasonable usage is with array:
#include <iostream>
using namespace std;
int* f4(int * a, int max)
{
a[0]++;
int * p = &a[0];
return p;
}
void main()
{
const int max = 5;
int a[max]={1,2,3,4,5};
int * pnt = f4(a,max);
cout<<*pnt;
}
In this example, function is returning a pointer to incremented first member of the array.