d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Can Someone Explain What Is Wrong With This (c++)
123Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 30 2012 01:30am
Code
int *ip_arr[100];
for (int i = 0;i<100;i++)
{
ip_arr[i] = new int(-1);
}


Here is the question:

Quote
Allocate an array of 100 integer pointers and assign the resulting pointer to the appropriately declared variable, ip_arr. Allocate 100 integers and assign the resulting pointers to the elements of ip_arr. Initialize each integer value to -1.


The error:

Problems Detected:
⇒ You don't seem to be allocating the correct number of
elements
that is a contradiction. 100 elements, and I am filling them all with -1. How is that not correctly allocated?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 30 2012 02:34am
lots of viewers but no replies :/


anywho, here is another problem I had as well

Quote
The variables arr1 and arr2 have been declared as pointers to integers. An array of 10 elements has been allocated, its pointer assigned to arr1, and the elements initialized to some values. Allocate an array of 20 elements, assign its pointer to arr2, copy the 10 elements from arr1 to the first 10 elements of arr2, and initialize the remainder of the elements of arr2 to 0.


my code:

Code
for (int i = 0; i<20;i++) {
if (i <10) {
arr2[i] = new int;
arr2[i] = arr1[i]; }
else arr2[i] = new int(0);
}


I keep getting this error:

Code
CTest.cpp: In function ‘int main()’:
CTest.cpp:5:16: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
CTest.cpp:7:26: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]


Line 5 is the if statement. I don't understand what is the problem. My prof didn't really explain this stuff very well to us. I don't quite understand how you can copy the contents of arr1 into arr2 without getting that error.
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Aug 30 2012 12:19pm
For starters, the first question asks you to allocate two arrays. You've only made one.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 30 2012 12:34pm
Quote (irimi @ Aug 30 2012 01:19pm)
For starters, the first question asks you to allocate two arrays.  You've only made one.


I thought that too. But read the wording on this one:

Quote
The variable ip_arr has been declared as an array of 20 pointers to integer (i.e., each element of ip_arr is a pointer to an integer). Allocate 20 integer values and assign their pointers to the elements of ip_arr.


It was solved with this:

Code
for (int i = 0;i <20;i++)
{
ip_arr[i] = new int;
}



where all they did was

Code
int *ip_arr[20];


This post was edited by Eep on Aug 30 2012 12:38pm
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Aug 30 2012 12:49pm
Don't confuse the difference between "pointer to an array" vs. "array of pointers".

The answer above is wrong as well.

This post was edited by irimi on Aug 30 2012 12:50pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 30 2012 12:53pm
Quote (irimi @ Aug 30 2012 01:49pm)
Don't confuse the difference between "pointer to an array" vs. "array of pointers".

The answer above is wrong as well.


can you help clarify that? (also the website flagged that answer as correct)

int *ip_arr[100] would be an array of pointers correct?

int *ip_arr = new int[100] would be a pointer to an array correct?

also, when they say initialize.....how do you make the call to do that? The book said, for example, if you had done something like

Code
int *arr = new int[100];
arr[i] = x; // where x is some number


This post was edited by Eep on Aug 30 2012 12:53pm
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Aug 30 2012 12:56pm
oh, actually, you're right. sorry about that. the first code snippet does look correct upon rereading the question... (as does the second)


edit: rereading again - the first question may actually be asking you to allocate 100 integers but have them point to the elements in the array

This post was edited by irimi on Aug 30 2012 12:59pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 30 2012 12:59pm
Quote (irimi @ Aug 30 2012 01:56pm)
oh, actually, you're right.  sorry about that. the first code snippet does look correct upon rereading the question... (as does the second)


So do you think you could help me translate the other 2? I don't understand what the site wants from me exactly :/


edit: For example, in the 2nd question I posted, when they asked me to copy the elements of arr1 to the first 10 elements of arr2, I tried various techniques and always got that error I showed.

Am i accessing/initializing/copying wrong? Why do I always get int to int* (-fpermissive) error?

This post was edited by Eep on Aug 30 2012 01:06pm
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Aug 30 2012 01:51pm
Quote
arr2[i] = arr1[i];


this should probably be

*arr2[i] = *arr1[i];


that should fix your code for the second problem. what you were doing was actually reassigning the pointer in your array to be pointing to the same place as the pointer in the first array - whereas the question wanted you to make a copy by assigning the value being pointed to in arr2 to be the same as the value being pointed to in arr1.

This post was edited by irimi on Aug 30 2012 01:58pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 30 2012 03:09pm
Quote (irimi @ Aug 30 2012 02:51pm)
this should probably be

*arr2[i] = *arr1[i];


that should fix your code for the second problem.  what you were doing was actually reassigning the pointer in your array to be pointing to the same place as the pointer in the first array - whereas the question wanted you to make a copy by assigning the value being pointed to in arr2 to be the same as the value being pointed to in arr1.


ahh, I'll try that, thanks


unfortuately it did not work. Gave me 2x as much int* to int conversion errors

This post was edited by Eep on Aug 30 2012 03:21pm
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll