#include <iostream>
#include <conio>
#include <math>
using namespace std;
void main()
{
double b[1000], mxrs[1000],mnrs[1000], ls[1000]; int t, i;
clrscr();
cin>>t;
for(i=0;i<t;i++)
cin>>b[i]>>ls[i];
for(i=0;i<t;i++)
{
mxrs[i]=sqrt(ls*ls+b*b);
mnrs[i]=sqrt(ls*ls-b*b); cout<<mnrs[i]<<' '<<mxrs[i]<<'\n';
}
getch();
}
I get 4 errors in this code...
main.cpp|16|Error E2087 : Illegal use of pointer in function main()|
main.cpp|16|Error E2087 : Illegal use of pointer in function main()|
main.cpp|17|Error E2087 : Illegal use of pointer in function main()|
main.cpp|17|Error E2087 : Illegal use of pointer in function main()|
||=== Build finished: 4 errors, 0 warnings ===|
Please help me in correcting the errors, it would be helpful even if you told me only reason of errors. Thanks in advance!
When you write double b[1000], you can access the array elements either by writing b[n] or *(b + n), taking care to keep n within the bounds of the array.
So *b and b[0] are equivalent. In other words, b is a pointer to the zeroth element of the array. Writing b * b is an attempt to multiply two pointers and that makes no sense. This is what the compiler is telling you.
To multiply elements of the array, use b[n] * b[m] or *(b + n) * *(b + m).
(And fix the return type of main: it should be int.)
ls,bs refers to the base address of the array. so they are addresses not values. I think this is what you intend to do.
#include <iostream>
#include <conio>
#include <math>
using namespace std;
void main()
{
double b[1000], mxrs[1000],mnrs[1000], ls[1000]; int t, i;
clrscr();
cin>>t;
for(i=0;i<t;i++)
cin>>b[i]>>ls[i];
for(i=0;i<t;i++)
{
mxrs[i]=sqrt(ls[i]*ls[i]+b[i]*b[i]);
mnrs[i]=sqrt(ls[i]*ls[i]-b[i]*b[i]); cout<<mnrs[i]<<' '<<mxrs[i]<<'\n';
}
getch();
}
let's take an simple example to understand the concept:
int a[5]={1,2,3,4,5};
cout<<*a; //prints the value at base address which is 1 as a refers to a[0]
cout<<a[0]; //prints 1
cout<<*(a+1); //prints 2 ... as a+1 refers to the address next to its base which is a[1] and which is 2
cout<<a[1];//prints 2
You put #include <math>, it should have been #include <math.h>. Same thing for <iostream> and <conio>. You forgot .h on all of them.
Related
See the following code:
#include <iostream>
#include <stdlib.h>
using namespace std;
class ex
{
public:
int x;
int y;
double z;
ex()
{
cout<<"constructor";
}
~ex()
{
cout<<"destructor";
}
};
int main()
{
void *pt=malloc(sizeof(ex)*2);
ex *p,*p1;
p=new(pt) ex();
p1=new(pt+sizeof(ex)) ex();
p->x=4444;
p->y=3333;
p->z=65.87879898;
p1->x=55555;
p1->y=66666;
p1->z=6666.6666666;
cout<<"\nP: "<<p->x<<"\n"<<p->y<<"\n"<<p->z<<"\n";
cout<<"\np1: "<<p1->x<<"\n"<<p1->y<<"\n"<<p1->z<<"\n";
p->~ex();
p1->~ex();
free(pt);
return 0;
}
It shows warning.
warning: pointer of type 'void *' used in arithmetic [-Wpointer-arith]|
There is any way to overcome that or the code is wrong.
note:That code displays correct output.
Thank you for helping.
This line is problematic:
pt+sizeof(ex)
Since pt is a void* it's not known what the size of one element is. Some compilers will compile it as if the size were 1, which makes your code run successfully. But this is not standards compliant. Instead, do this:
p+1
That is, use the memory address which is one element (of type ex) after the first one.
Or, cast to char* so you know you're operating with elements of size 1:
static_cast<char*>(pt)+sizeof(ex)
Header:
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
int i[8];
#endif
Main:
#include <iostream>
#include <string>
#include "header.h"
using namespace std
int main(){
int *test;
string bits = "10011011";
*test = func(bits); // ERROR 2 HERE
//my goal here is to have a pointer in main that
//points to the 1 address of the global var array i
} //if im totally missing the point and there is a
//better way to do this please let me know
Function:
#include <iostream>
#include <string>
using namespace std
int *func(string str){
int l = str.size();
int *ptr;
for(int k=0; k < l; ++k){
i[k] = s[k] - '0';
}
*ptr = i; // ERROR 1 HERE
return ptr;
}
Hi all,
When I attempt to compile the above code, I get two errors as labeled. They are:
Error 1: invalid conversiopn from int* to int
Error 2: invalid conversion from int to *int
It seems I must have a fundamental understanding of what I am working with here. Am I not setting *ptr to point at the first memory address of the array i[]? Why is the compiler telling me that I am trying to set the value of the pointer to the value of i[]? I want to set the value of the pointer to the ADDRESS of i[]. If I add an & I get the same error.
Error 2 obviously follows error 1. It is the same error, just backwards.
My question is, then, what the heck am I doing wrong when I am trying to point *ptr in func() at the array i[]? I DID try finding the answers elsewhere to no avail.
Thanks.
edit: this code is a rough transcription of the original (from another PC), so if you try to compile it and there are typos I apologize.
int *ptr; declares a pointer to int, so ptr is a pointer: a variable that stores the memory location of some int. Currently it doesn't point to anything in particular, just some random place in memory that you probably don't own.
*ptr dereferences ptr, so *ptr is the int that ptr points to.
When you do *ptr = i; you are attempting to set the int pointed to by ptr equal to i, which is an array of 8 ints. In this context, the array decays into a pointer to its first element, so you're attempting to set an int equal to an int*, hence the error.
This should just be ptr = i;, but even that isn't necessary, since you could just return i; and i would decay into a pointer to the first element of the array.
Similarly, *test = func(bits); attempts to set the int pointed to by test equal to the pointer returned by func. It should just be test = func(bits);, in which case the address that test contains will become the one that func returned. In this case test will point to the first element of i.
I'm trying to write program that create squere for string. Squere has to be larger then string.length(). If there is word 'C++' I need 2x2 array to fill it inside.
So I have written code
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int pole(int &a,const int* l);
int main(){
string code;
cin >> code;
int wall=1;
pole(wall,code.length());
cout << wall;
system("PAUSE");
return 0;
}
int pole(int &a,const int* l){
if (a*a > l) return a;
else {
a+=1;
pole(a,l);
}
}
I bet that using pointer with recunrency save a lot of memory but I can't compile it. I'm trying to understand compilers error but is 2 hard for me ;/
Here is compiler list of errors
> in main()
11 25 Error] invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int (*)(int&, const int*)'
6 5> [Error] in passing argument 1 of 'int pole(int&, const int*)'
in pole() 17 12
>[Error] ISO C++ forbids comparison between pointer and
> integer [-fpermissive]
Here:
pole(pole, code.length());
You are passing as the second variable the result of length(), which is of type std::string::size_type, which the function pole accepts a pointer to int. Those two types are incompatible.
The second problem is that one branch of your if statement inside pole does not contain a return statement, thus giving your program Undefined Behavior.
You may want to change your function pole this way:
int pole(int &a, std::string::size_type l) {
// ^^^^^^^^^^^^^^^^^^^^^^
// Also, passing by reference is unnecessary here
if (a*a > static_cast<int>(l)) return a;
// ^^^^^^^^^^^^^^^^
// Just to communicate that you are aware of the
// signed-to-unsigned comparison here
else {
a+=1;
return pole(a,l);
// ^^^^^^
// Do not forget this, or your program will have Undefined Behavior!
}
}
Here you can see your modified program compile and run.
You are trying to use an unsigned integer (coming from std::string::length) as a pointer in:
pole(wall,code.length());
Change your pole declarations to:
int pole(int a, int l);
Saving memory on int is just nonsense there. Pointers are sometimes even more memory expensive than simple integers.
You should learn to save memory with huge objects instead.
int pole(int &a,const int* l){
if (a*a > l) return a;
else {
a+=1;
pole(a,l);
}
}
first, you cannot initialize int* l with size_t argument.
Also you do later comparison between address, not value pointed too.
Is this what you wanted?
#include <iostream>
#include <string.h>
using namespace std;
class A
{
private:
int a;
int b;
public:
A():a(10),b(20){};
A(int ad,int bd):a(ad),b(bd){};
void printvalues()
{
cout<<a << " " <<b<<endl;
}
};
int main()
{
A a(5,12);
memset(&a,sizeof(A),0);
a.printvalues();
return 0;
}
memsetting the object to 0, do not seem to have any effect on the object. Can anyone help me understand this behavior.
output:
5 12
You have the arguments to memset the wrong way round. It's memset(addr, value, number).
Note: In C++, memset is usually avoided.
You got the memset wrong:
void * memset ( void * ptr, int value, size_t num );
it should be:
memset(&a,0,sizeof(A));
In your example, you're setting 0 bytes of a to sizeof(A), so, obviously, no change.
The correct syntax of memset is memset(&a,0,sizeof(A)) because the first parameter is the array or the variable, the second is the value and the third parameter is the number of bytes.
For more details about memset visit http://www.cplusplus.com/reference/clibrary/cstring/memset/.
You have misplaced the arguments to memset. At first I thought this is stunning!
Write this:memset(&a,0,sizeof(A));
And all will be as expected.
Here is a link to the code and I have posted it below too.
#include<math.h>
void pentagon(int n)
{
int k,p[10],a[10],b[10];
if(n<0)
p[n]=0;
if(n==0)
p[n]=1;
for(k=1;k<n;k++)
{
a[k]=((3*pow(k,2))-k)/2;
b[k]=((3*pow(k,2))+k)/2;
}
for(k=1;k<n;k++)
{
p[n]=pow(-1,k-1)(pentagon(n-a[k])+pentagon(n-b[k]));
}
cout<<p[n];
}
int main()
{
pentagon(4);
return(0);
}
I am getting the following error :
In function 'void pentagon(int)':
Line 11: error: call of overloaded 'pow(int&, int)' is ambiguous
compilation terminated due to -Wfatal-errors
Replace 2 with 2.0 as the second argument to pow (line 11, 12).
See also: http://www.cplusplus.com/reference/clibrary/cmath/pow/
Collecting and correcting all the errors (and warnings) leads to the following code (codepad). I made some comments about what changed.
#include <math.h>
#include <iostream> // Some compilers will complain if missing
using namespace std; // Some compilers will complain if missing
// returns int instead of being void
int pentagon(int n)
{
int k,p[10],a[10],b[10];
// recursion end - we want to jump out here right away
if(n<0) return 0;
if(n==0) return 1;
for(k=1;k<n;k++)
{
a[k]=((3*(int)pow(k,2.0))-k)/2; // pow needs double as second argument
b[k]=((3*(int)pow(k,2.0))+k)/2; // and returns double
}
for(k=1;k<n;k++)
{
// pow casting and double as second argument, multiplication with pentagon
p[n]=(int)pow(-1,k-1.0)*(pentagon(n-a[k])+pentagon(n-b[k]));
}
cout<<p[n]<<endl; // cleaner output
return p[n]; // return the calculated value
}
int main()
{
pentagon(4);
return(0);
}
but I guess the underlying algorithm is still wrong, as the output is:
-1084535312
-1084535311
1074838088
0
3
4
0
Here are some errors or improvements I spotted:
Added iostream & using namespace std:
#include <cmath>
#include <iostream>
using namespace std;
Changed pow(k,2) to k*k:
a[k]=((3*(k*k))-k)/2;
b[k]=((3*(k * k))+k)/2;
Add multiplication symbol to p[n] assignment:
p[n] = pow(-1.0,k-1) * (pentagon(n-a[k]) + pentagon(n-b[k]));
The pentagon method needs to return a value in order to use it in the above statement.
You're missing the summation part of the equation.
See http://en.wikipedia.org/wiki/Pentagonal_number_theorem.
Your pentagon function only calculates one term at a time. There is no code that sums all of the terms.