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?
Related
#include <iostream>
using namespace std;
const int up=18;
int realArray[up]={12,28,75,16,66,6,121,19,195,56,108,221,19,93,104,127,73,22}; //definition of a random array
void shakersort(int formArray[up]);//shakersort declared
void tauschen(int *a,int *b){int zw= *a; *a=*b; *b=zw;}
int main()
{
shakersort(realArray[up]); //here happens the error of the conversion
return 0;
}
void shakersort(int formArray[up]){ //the code
for(int i=0; i<up/2;i++){
for (i=0; i<up-1;i++){
if(formArray[i]>formArray[i+1]){
tauschen(&formArray[i], &formArray[i+1]);
}
}
for (int k=up-1; k>0;i--){
if(formArray[k]>formArray[k-1]){
tauschen(&formArray[k], &formArray[k-1]);
}
}
}
}
Not sure why there is a conversion error. Used the same code at declaration and application so not sure why it's not working.
up is an int with value 18 so in this line
shakersort(realArray[up]);
you are basically writing
shakersort(realArray[18]);
which will index a single value from your array (which also happens to be out of bounds, which would therefore be undefined behavior). Instead just pass the array itself
shakersort(realArray);
though I would encourage you to look into using std::vector instead
void shakersort(std::vector<int>& formArray);
so you don't need a global variable floating around to determine the array size, you can just use formArray.size()
I was doing the Coin change problem, I am trying to do it using Dynamic Programming. But I am getting this compilation which I don't quite understand. Someone told me that I have to assign the 'dp' array dynamically, but he was not sure why. PLease explain this concept .
#include<bits/stdc++.h>
using namespace std;
int solve(int *d, int size, int n , int ** dp){
if(n==0)
return 1;
if(n<0)
return 0;
if(size == 0)
return 0;
if(dp[n][size]>-1)
return dp[n][size];
int x = solve(d,size,n-d[0],dp);
int y = solve(d+1, size - 1, n, dp );
dp[n][size] = x+y;
return x+y;
}
int countWaysToMakeChange(int denominations[], int numDenominations, int value){
int dp[value+1][numDenominations+1];
memset(dp, -1, sizeof dp);
return solve(denominations, numDenominations, value, dp );
}
Error :
Compilation Failed
In file included from Runner.cpp:3:0:
Solution.h: In function 'int countWaysToMakeChange(int*, int, int)':
Solution.h:28:60: error: cannot convert 'int (*)[(numDenominations + 1)]' to 'int**' for argument '4' to 'int solve(int*, int, int, int**)'
return solve(denominations, numDenominations, value, dp);
^
Here is my Main file code:
#include<iostream>
using namespace std;
#include "Solution.h"
int main(){
int numDenominations;
cin >> numDenominations;
int* denominations = new int[numDenominations];
for(int i = 0; i < numDenominations; i++){
cin >> denominations[i];
}
int value;
cin >> value;
cout << countWaysToMakeChange(denominations, numDenominations, value);
}
There are two problems in the code.
First, in the function int countWaysToMakeChange(int denominations[], int numDenominations, int value)
int dp[value+1][numDenominations+1];
is illegal. Array bounds must be compile-time constants. Some compilers allow this sort of things as an extension (and it's legal in C), but it is not valid C++.
Second, the type of dp is "array of array of int". It is not a "pointer to pointer to int", and the compiler is complaining that it can't make that conversion when the code tries to pass dp as the fourth argument to solve.
Arrays are confusing. In most contexts, the name of an array decays into a pointer to its first element. That's why you can write code like this:
void f(int*);
void g() {
int array[20];
f(array);
}
Since dp is an array, its name decays into a pointer to its first element. But this is where it's easy to get lost: as I said earlier, the type of dp is "array of array of int"; when its name decays, the resulting type is "pointer to array of int".
If you want to pass dp to solve, solve has to take the same type: "pointer to array of int". But since you don't know the size of that array when you write solve you can't write that type in the argument list.
That's one reason why multi-dimensional arrays are often represented as one-dimensional arrays, with code to convert the two dimensions into one. The offset is x * width + y, or some minor variant on that. When you do that, your two-dimensional array becomes a one-dimensional array, and its name decays into a pointer to its first element, so you can pass it to a function that expects int*.
I’m trying to get a pointer to an array that’s in a struct, but doing *(struct->array) gives me an error.
Here’s the code:
#include <iostream>
using namespace std;
struct Numbers {
int highest;
int lowest;
int list[10];
};
void print_pointer_to_first_number(const Numbers* num) {
cout << num.list << endl;
}
int main() {
Numbers numbers;
numbers.highest = 10;
numbers.lowest = 1;
print_pointer_to_first_number(numbers);
return 1;
}
I’m getting an error saying:
error: member reference type 'const Numbers *' is a pointer; did you mean to use '->'?
I am not allowed to change the argument in the function.
You'll want to pass Numbers as reference rather than a pointer (essentially the same, but with nicer syntax):
void print_pointer_to_first_number(const Numbers &num)
Alternatively you can keep it as a pointer, but then you'll need to (1) dereference it properly:
num->list
and (2) pass a pointer when you call it:
print_pointer_to_first_number(&numbers)
Here is the code.
#include <iostream>
using namespace std;
int w=0;
class A{
int k,n;
public:
int z;
A(){w+=3; k=3+w; n=4+w; z=w;}
A *fun1(){z=k*n; return this;}
A *fun2(){z=n*k; return this-1;}
friend int fun (A *a,int &b);
};
int fun(A *a,int &b)
{ b=a->z+=4;
return a->k+a->n;
}
int main()
{ int m;
A a[2];
cout<<fun(a[1].fun1(),m)<<"\n";
cout<<m<<"\n";
cout<<fun(a[1].fun2(),m)<<"\n";
cout<<m<<"\n";
cout<<a[0].z+a[1].z<<"\n";
return 0;
}
When return this-1 happens, what does it mean? Does it mean that the object returned is a[0]? I can't understand..
Thanks!
Like any pointer, subtracting 1 from this assumes *this is an element of an array, gives the address of the preceding object in the array. There is an implicit assumption in A::fun2() that *this is an element of an array, and that there is at least one preceding element in that array.
So, in your sample code, a[1].fun2() returns the the address of a[0]. i.e. &a[0].
a[0].fun2() would return a (pointer) value equal to &a[-1]. Notionally, that is a pointer to a non-existent object. Computing this-1 in A::fun2() would give undefined behaviour just as much as computing a-1 would in main(). One common (but not guaranteed) practical symptom of such undefined behaviour would be a program crashing when later dereferencing the pointer.
A fun2(){z=nk; return this-1;}
'this' is a self-referential pointer. You cannot subtract 1 from this.
It will then point to a memory location which is one before 'this' pointer
int main()
{
int a[]={2,3,4,5,6};
int j;
for(j=0;j<5;j++)
{
printf("%d\n",*a);
a++;
}
return;
}
gives "Lvalue required" error but
int main()
{
int a[]={2,3,4,5,6};
int *p,j;
p=a;
for(j=0;j<5;j++)
{
printf("%d\n",*p);
p++;
}
return;
}
doesn't. why????
Though closely related, arrays are not pointers. The name of the array is just a label to identify some allocated memory (hence, the Lvalue error when you try to modify it).
An array is not a pointer. In most expressions, an array is converted to a pointer automatically. The result of this conversion is no longer the array; it is just a pointer value.
The ++ operator cannot operate on a mere value. It must have an object to act on.
For example, consider int x = 3; (x+5)++;. The result of x+5 is 8. It is not x. The result is just a value, not an object, so there is no object containing 8 that ++ can operate on. This is an error.
Similarly, if a is an array of int, then a++ is equivalent to ((int *) a)++. The ++ is not trying to act on the a; it is trying to act on the result of converting a to a pointer.
An array expression is always converted to a pointer to the first element except when the array expression is the operand of sizeof, &, or _Alignof or is a string literal used to initialize an array.
This will display all the array data.
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
int a[]={2,3,4,5,6};
int *p,j;
p=a;
for(j=0;j<5;j++)
{
printf("%d\n",p[j]);
}
return 0;
}