d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Can Someone Explain What Is Wrong With This (c++)
Prev123
Add Reply New Topic New Poll
Member
Posts: 98
Joined: Aug 3 2009
Gold: 283.38
Sep 1 2012 06:46pm
Quote (Eep @ Aug 30 2012 07:08pm)
my biggest issue is when I stop getting compiler errors, the website keeps telling me I allocated the wrong number of elements. Like I did 100 fucking loops and I am pretty sure I understand how arrays work by the end of cs1250.

Code
int *ip_arr[100];
int *a = new int[100];
for (int i = 0; i < 100; i++)
{
a[i] = -1;
ip_arr[i] = a+i;
}


is this not filling every (100) elements up?

this website is like a shitty person who tells you to make something and then bitches when you do exactly as they say.


This may be completely incorrect, but when you do

ip_arr[i] = a+i;

you're directly adding 0-99 to a memory location, correct? This is entirely neglecting the fact that integers are going to take up a larger space, right? So in reality it should be something like:

ip_arr[i] = a+(i*[insert memory size of your data type here]);

Normally using the syntax of arrays, the computer does this for you since it knows what data type your array is, so you can do things like myArray[a+i], but since you're adding directly to a memory location you have to factor in the size between each element according to the data type...?
If I'm wrong in my understanding of this please tell me, I'm new at this and would be happy to learn.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 1 2012 07:17pm
Well I didnt finish that problem in time so whatever. Anyways,I checked the solution posted today and it was like


Int **iparr = new *int [100];
Then a for loop

This is beyond fucked up because the text never ever covered double pointers and our professor did not show us how to use double pointers
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Sep 1 2012 08:09pm
double pointers aren't all that special if you understand pointers, but that question was phrased wayyyy off
Member
Posts: 98
Joined: Aug 3 2009
Gold: 283.38
Sep 1 2012 09:21pm
Quote (irimi @ Sep 1 2012 10:09pm)
but that question was phrased wayyyy off


Quote (Eep @ Aug 30 2012 06:21pm)
Allocate an array of 100 integer pointers and assign the resulting pointer to the appropriately declared variable, ip_arr


...?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 1 2012 11:01pm
Quote (SoulScythe @ Sep 1 2012 10:21pm)
...?


from that you would draw int **ip_arr = new int*[100]??

also, I hope this is your first time viewing this thread or fuck you for not giving me that hint :[
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Sep 2 2012 12:34pm
Quote (SoulScythe @ Sep 1 2012 08:21pm)
...?


yea, you don't know what you're talking about

int* foo[size] is in fact an array of integer pointers, and the result is foo being a pointer to that array

This post was edited by irimi on Sep 2 2012 12:35pm
Member
Posts: 98
Joined: Aug 3 2009
Gold: 283.38
Sep 2 2012 01:06pm
Quote (irimi @ Sep 2 2012 02:34pm)
yea, you don't know what you're talking about

int* foo[size] is in fact an array of integer pointers, and the result is foo being a pointer to that array


The assignment is clearly dealing with dynamic memory allocation. Statically declaring an array of pointers and using the array name as the "resulting pointer" would get you the correct results, in a 'black box' way of thinking. However, dynamically allocating an array of pointers and declaring a double pointer to hold its location would be the reasonable thing to do for this assignment. I don't understand the confusion you're having.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 2 2012 09:24pm
Quote (SoulScythe @ Sep 2 2012 02:06pm)
The assignment is clearly dealing with dynamic memory allocation. Statically declaring an array of pointers and using the array name as the "resulting pointer" would get you the correct results, in a 'black box' way of thinking. However, dynamically allocating an array of pointers and declaring a double pointer to hold its location would be the reasonable thing to do for this assignment. I don't understand the confusion you're having.


it made sense AFTER I saw the solution and thought about it for awhile....but how come you couldn't have told me this before I lost credit :/
Member
Posts: 98
Joined: Aug 3 2009
Gold: 283.38
Sep 2 2012 10:06pm
Quote (Eep @ Sep 2 2012 11:24pm)
it made sense AFTER I saw the solution and thought about it for awhile....but how come you couldn't have told me this before I lost credit :/


Because I saw the thread at the time of my first post, and I was pointing out a problem with the code you had posted. Sorry :/
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 2 2012 10:17pm
Quote (SoulScythe @ Sep 2 2012 11:06pm)
Because I saw the thread at the time of my first post, and I was pointing out a problem with the code you had posted. Sorry :/


lol it's okay man. Important thing is I understand it now~
Go Back To Programming & Development Topic List
Prev123
Add Reply New Topic New Poll