Discussion Forums > Technology
C++ help
xShadow:
--- Quote ---This won't even compile due to the error:
1>c:\documents and settings\my documents\visual studio 2008\projects\bkttst\bkttst\bkttst.cpp(9) : error C2100: illegal indirection
Line 9 is the one with *array[0].
Comment out that line and the output:
--- End quote ---
...Well, yeah, no shit sherlock, you put a double pointer in for a variable that only had one pointer in the first place. Of course it won't compile. What's your point? I'm guessing you're totally missing what I'm saying? Like for the past 2-3 times now?
I was just establishing that arr[] is the same as an *arr. It's simply that *arr does not have a defined number of memory locations set aside to it, which can be fixed (at least in c) by something like:
double *arr= double *malloc(5);
That sets aside room for 5 doubles in a modifiable area of the memory.
You can then proceed to go
arr[0]= 41.41;
Or whatever the hell you want.
Also, indexing with double pointers should be something like this:
arr[0][0]
is equivalent to
**arr
and,
arr[1][0]
is equivalent to
*(arr[1])
or
**(arr+1)
and
arr[1][1]
is the same as
*(*(arr+1)+1)
or
*(arr[1]+1)
I'm honestly quite rusty with my programming, so you can go test it out for me, if you wish.
--- Quote ---So can you now at last give up on *double[] ?
Here's the somewhat unrelated reason to why
I brought up the declaration of main and when
such syntax is used:
--- End quote ---
... Wtf? Give up on it? What the hell are you talking about? Unless C++ is just VASTLY different from C, I don't see where you're taking the *double[] argument to?
Moreover... his main function isn't even where the problem is. What the hell are you getting at?
I looked over your console function that takes inputs in from the user, and I don't see what point you're trying to make. You do realize that any NORMAL char declaration in C can only have one letter by itself? A mere word is already an array. For the first level of your character array's pointers, you put in space for 5 character pointers. Then, you proceeded to give each of those character pointers their own array. In essence, this is a double pointer. I believe you're just proving exactly what I said, so I don't see what your point is.
You do **mystrings after the user input and you'll just get out the letter h, which makes sense, because mystrings is an array of pointers to arrays of pointers which point to letters, and you're pretty much saying mystrings[0][0].
--- Quote ---And while I'm at it why not poke a hole at your
array-gets-copied-and-can't-be-modified statement:
--- End quote ---
Poke a hole in? If you look at edit 2 at the bottom of my post, you'll see that I realized that it may just copy addresses and thus make it modifiable, and I said that. Thanks for making sure, though. I've personally never had a situation where I've had to modify an array like that, but that's because most of the stuff I was dealing with were dynamically allocated data structures like binary trees, nary trees, and linked lists. Something as simple as modifying a numerical array isn't a part of my (recent) experience.
--- Quote ---Please, do test your stuff before you start
giving advice if you're not sure you're right.
--- End quote ---
... No, I don't really feel like opening Visual Studio to test something like this.
I mean, I don't know where the hell you're headed with YOUR argument (nowhere?), but the topic creator's original problem (which I am addressing) was that he was sticking in a pointer into a function that took a double pointer. For that branch, everything I said makes sense. For the corrections I suggested in my post, the a few can be cut off now that I know that arrays can be modified inside of the function. That's about it. On a side note, though, C++ has some nice convenient little things. Lol @ the "delete" and "new" commands. In C, this would have been more of a pain.
However, I have to applaud this:
--- Quote ---call scores like this
sort(&scores, numtest);
--- End quote ---
That's brilliant. I totally forgot about the & (address) operator. That should mostly fix your stuff, Gogeta. There might be a few errors inside the actual function, but this should fix the error you're getting right now.
Fr0steh:
--- Quote from: Pzc on July 22, 2009, 12:14:14 PM ---&(*arry)[startscan] ? wut ?
--- End quote ---
that's simply because you need to deference arry ("(*arry)"), get the position of the array given by startscan ([starscan]) and return a pointer to that position (&).
You can also do ::)
(*arry) + startscan*sizeof(double);
btw, my code works on visual studio...
Pzc:
--- Quote from: xShadow on July 22, 2009, 01:32:30 PM ---
--- Quote ---This won't even compile due to the error:
1>c:\documents and settings\my documents\visual studio 2008\projects\bkttst\bkttst\bkttst.cpp(9) : error C2100: illegal indirection
Line 9 is the one with *array[0].
Comment out that line and the output:
--- End quote ---
...Well, yeah, no shit sherlock, you put a double pointer in for a variable that only had one pointer in the first place. Of course it won't compile. What's your point? I'm guessing you're totally missing what I'm saying? Like for the past 2-3 times now?
--- End quote ---
Good, we agree on that one then. So, next. Where
in the OP's code do you see any pointer to pointers ?
I aint missing what you write but you're all over the
place. And I do hope you're not confusing the type
double (double precision floating point) with what many
call a double pointer, pointer-to-pointer (double **g) ?
--- Quote from: xShadow on July 22, 2009, 01:32:30 PM ---I was just establishing that arr[] is the same as an *arr. It's simply that *arr does not have a defined number of memory locations set aside to it, which can be fixed (at least in c) by something like:
double *arr= double *malloc(5);
That sets aside room for 5 doubles in a modifiable area of the memory.
You can then proceed to go
arr[0]= 41.41;
Or whatever the hell you want.
--- End quote ---
No, C-arrays (double[]) is not the same as
the one allocated with new/malloc (double *).
C-arrays can decay to pointers but not the
other way. First link on google just for you.
Maybe not always very important for 1D arrays
but extremely important for 2+D arrays.
--- Quote from: xShadow on July 22, 2009, 01:32:30 PM ---Also, indexing with double pointers should be something like this:
arr[0][0]
is equivalent to
**arr
and,
arr[1][0]
is equivalent to
*(arr[1])
or
**(arr+1)
and
arr[1][1]
is the same as
*(*(arr+1)+1)
or
*(arr[1]+1)
I'm honestly quite rusty with my programming, so you can go test it out for me, if you wish.
--- End quote ---
No need for testing, really. You can only use the
syntax arr[x1][y2] if you have declared arr like arr[10][10].
Not when you allocate it with new/malloc.
--- Quote from: xShadow on July 22, 2009, 01:32:30 PM ---I mean, I don't know where the hell you're headed with YOUR argument (nowhere?), but the topic creator's original problem (which I am addressing) was that he was sticking in a pointer into a function that took a double pointer. For that branch, everything I said makes sense. For the corrections I suggested in my post, the a few can be cut off now that I know that arrays can be modified inside of the function. That's about it. On a side note, though, C++ has some nice convenient little things. Lol @ the "delete" and "new" commands. In C, this would have been more of a pain.
--- End quote ---
new and delete are essential to c++ since they
do things malloc and free don't.
--- Quote from: xShadow on July 22, 2009, 01:32:30 PM ---However, I have to applaud this:
--- Quote ---call scores like this
sort(&scores, numtest);
--- End quote ---
That's brilliant. I totally forgot about the & (address) operator. That should mostly fix your stuff, Gogeta. There might be a few errors inside the actual function, but this should fix the error you're getting right now.
--- End quote ---
That's not needed here.. sure, you can do that but
it only adds indirection as you create a pointer to
a pointer, useless in the OP's post since (s)he's only
allocating a 1D array.
--- Quote from: Fr0steh on July 22, 2009, 02:37:43 PM ---
--- Quote from: Pzc on July 22, 2009, 12:14:14 PM ---&(*arry)[startscan] ? wut ?
--- End quote ---
that's simply because you need to deference arry ("(*arry)"), get the position of the array given by startscan ([starscan]) and return a pointer to that position (&).
You can also do ::)
(*arry) + startscan*sizeof(double);
btw, my code works on visual studio...
--- End quote ---
Ah, yes, now I get what you mean. On the other hand
if you would've treated the 1D array as a 1D array and
not pass the pointer-to-the-pointer-to-the-array and
just went with the pointer-to-the-array there's less
indirection. But yeah, it works I suppose. But do fear
my double ************arrpointer ;)
As for the rest, I give up. You can add as many
&'s, *'s and []'s as you want, I don't care any more.
Thread is far enough off-topic by now anyways.
Fr0steh:
I didn't want to change the function parameters because assuming that this is a educational exercise probably using a pointer to an array is needed. And yes passing simply the array which is a pointer by itself would suffice : )
And also if you want to make even more changes :D you didnt need to do a sort function, simply get everything in a vector and use the sort function from the algorithm library :p
And to the OP, if u're TL;DR XD simply use the code on my post, it works and only changes 2 things.
xShadow:
--- Quote ---No, C-arrays (double[]) is not the same as
the one allocated with new/malloc (double *).
C-arrays can decay to pointers but not the
other way. First link on google just for you.
Maybe not always very important for 1D arrays
but extremely important for 2+D arrays.
--- End quote ---
He's not doing a 2+D array, and what do you mean "the other way"? The article you're linking to admits that as far as the compiler is concerned they are treated the same way in functions. I know full well that *char and char[] are different. One is a modifiable entity while the other is placed in a subsection of memory that you can't directly access. However, you can still use *char for either case to access either one, just as you can use char[0] to access either one.
--- Quote ---Good, we agree on that one then. So, next. Where
in the OP's code do you see any pointer to pointers ?
I aint missing what you write but you're all over the
place. And I do hope you're not confusing the type
double (double precision floating point) with what many
call a double pointer, pointer-to-pointer (double **g) ?
--- End quote ---
Look at the change Frosteh made and then tell me you don't understand what my point was.
Here, look:
double *scores;
This is what he's putting into his sorting function. It's just a regular pointer, meaning he intends to have the user fill in what else is needed.
Then, this is his sorting function prototype:
void sort(double *arry[], int size)
Now look at the error he had. Makes some sense, maybe?
Notice the *arry[]? Now you can be all picky and say that's not actually a double pointer and blablablah, but his problem is that he was just putting a regular array (which is on the level of a single pointer) into a function that asked for a pointer to an array. Frosteh fixed this by changing it so the function call took up the address of scores[], thus pretty much putting in an extra pointer where there was none before, just for the function to use.
--- Quote ---That's not needed here.. sure, you can do that but
it only adds indirection as you create a pointer to
a pointer, useless in the OP's post since (s)he's only
allocating a 1D array.
--- End quote ---
...???? Read above.
--- Quote ---As for the rest, I give up. You can add as many
&'s, *'s and []'s as you want, I don't care any more.
Thread is far enough off-topic by now anyways.
--- End quote ---
Who's the one that derailed it? I'm pretty sure it wasn't me, as my first post (which you misunderstood multiple times and them proceeded to argue with me over) was made entirely with the intention of helping him.
--- Quote ---I didn't want to change the function parameters because assuming that this is a educational exercise probably using a pointer to an array is needed. And yes passing simply the array which is a pointer by itself would suffice : )
And also if you want to make even more changes Cheesy you didnt need to do a sort function, simply get everything in a vector and use the sort function from the algorithm library :p
And to the OP, if u're TL;DR XD simply use the code on my post, it works and only changes 2 things.
--- End quote ---
Did you test it with every vector imaginable though? I mean, it should work, but uhh... I don't know, something about it just makes me a bit skeptical. >_>
Anyways, your fix is pretty simple. I tend to like to just get rid of pointers where they aren't needed (which is what my fix suggested), to make things less complex.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version