+ 2
It keeps throwing errors, I can't seem to understand the problem.
So this code should add an element to the end of the array/remove the first element/print the whole array. For some reasons it doesn't work and throws some errors which honestly I don't understand. https://code.sololearn.com/c0OY5Yj4eKwz/?ref=app
19 odpowiedzi
+ 2
Hint:
You pass an unsized array to a function. Thus you have also to pass the size of an array to each function.
+ 2
Bob_Li
OH WAIT, now it makes sense! "It decays into a pointer" so basically it decays into an address given that the array name in itself is an address for index 0 so when it is passed it is passed as an address.....so basically all I have been doing was passing index 0 from the array to the function which will definitely result in a wrong size which is why it has only displayed the first 2 items.
+ 2
Intermediate Depression
One of important things is to know how work a compiler. The compiler here don‘t know what is the size of this unsized parameter array.
There is a way to solve it in a deep c++, that can be done with a template.
You have closed already here intermediate course of c++.
+ 2
Intermediate Depression
And the corrected code:
#include <iostream>
using namespace std;
void print(int x[6]){
//int n = sizeof(x)/sizeof(x[0]);
for(int i=0;i<6;i++){
cout<<x[i]<<" ";
}
cout<<"\n"<<endl;
}
void remove(int x[6]){
//int n = sizeof(x)/sizeof(x[0]);
for(int i=0; i<6; i++){
x[i]=x[i+1];
}
}
void add(int queue[6],int x){
//int size = sizeof(queue)/sizeof(queue[0]);
queue[6] = x;
return;
}
int main(){
int arr[6]{1,2,3,4,5,6};
print(arr);
remove(arr);
print(arr);
add(arr, 89);
print(arr);
}
+ 2
Bob_Li this is one of possible solutions.
+ 2
Try This One
#include <iostream>
using namespace std;
void print(int n, const int* x){
for(int i = 0; i < n; i++){
cout << *(x + i) << " ";
}
cout << "\n";
}
void remove(int n, int* x){
for(int i = 0; i < n - 1; i++){
*(x + i) = *(x + i + 1);
}
}
void add(int size, int* queue, int x){
*(queue + size - 1) = x;
}
int main(){
int arr[]{1, 2, 3, 4, 5, 6};
print(6, arr);
remove(6, arr);
print(6, arr);
add(6, arr, 89);
print(6, arr);
}
+ 1
Intermediate Depression
to get it to work, do something like this:
#include <iostream>
using namespace std;
void print(int n, int x[]){
for(int i=0;i<n;i++){
cout<<x[i]<<" ";
}
cout<<"\n";
}
void remove(int n, int x[]){
for(int i=0; i<n-1; i++){
x[i]=x[i+1];
}
}
void add(int size, int queue[],int x){
queue[size-1] = x;
}
int main(){
int arr[]{1,2,3,4,5,6};
print(6, arr);
remove(6, arr);
print(6, arr);
add(6, arr, 89);
print(6, arr);
}
+ 1
Intermediate Depression
Of course sized array as the parameter of a function, means for example
void fun(int arr[4])
make the difference.
Please show what did you tried then can be said where is the problem.
+ 1
The concept of an array decaying to a pointer is a hard one to grasp. But this is the basic reason why doing it your current way will never work.
The size of the array is not passed along when you pass it to a function. What the function see is that it is an array and it points to the first item. No size info. How many items it contains is never passed along.
see my first code, the size calculation inside the function is always wrong.
+ 1
Also the sizeof() function behaves differently outside main(). Lol I have a lot to learn.
+ 1
Bob_Li
JaScript
UPDATE : sizeof() doesn't behave differently, but it's the fact that arr decays into a pointer which it's size is always 8
My code divided it by 4 so the n value was always 2
Which is why it only outputs the first 2 items only
arr will always decay into a pointer making the idea of my code next to impossible .
THANK YOU EVERYONE ❤❤❤❤
+ 1
JaScript
yes, that could work, but now you have functions that only work for int array of size 6.
+ 1
Intermediate Depression
yes, you have unlocked an understanding on a core quirk of arrays. 😎
JaScript
ok, this may be more complicated than simply using vectors, but maybe you can create a custom struct that have an array internally.
Then you can pass along it's size value to functions because it is encapsulated in the struct.
Being generic also have the advantage of working for different data types.
I renamed the functions to better describe what they are doing.
https://sololearn.com/compiler-playground/chY5i6iqkpK1/?ref=app
0
Intermediate Depression
arrays decay to pointers when passed to functions. You cannot compute the number of items in it by dividing.
It may work when you compute it in main, but it will not work if you do the same computation inside a function.
https://stackoverflow.com/questions/21034327/array-size-is-being-reduced-when-passed-to-a-function-for-unkown-reasons
also, array is not a good choice if you are going to change the capacity often. Your add method is going to be problematic. Appending to array is not as trivial as that. Better to use vectors, since you are already using c++.
0
JaScript
But I did with the sizeof() function
Also, what if they were sized ? Would that have made any difference? Because I tried but still same errors 😥
0
Intermediate Depression
here is a simple demo why you cannot do the calculation inside a function
you have to also pass the size of the array as a separate function argument.
https://code.sololearn.com/cmDU8d9NFqi2/?ref=app
0
Bob_Li yup that was my previous code but I wanted to make it hmmmm ...better ? Idk I wanted to make c++ get the size while all i need to do is write print(arr) and that's all.
0
JaScript
Here is what I tried (I am not sure where did I go wrong.)
https://code.sololearn.com/c0OY5Yj4eKwz/?ref=app
0
Bob_Li
JaScript
Array is not a good choice, this is why I am using it! I have been told arrays must always be sized and that the only way to get an unsized array is through dynamic memory. But tbh I just couldn't understand why or how. I mean, I could easily just write an unsized array and add to it as many times as I wish. So I decided to test it out myself. But I started facing problems! Like I was not able to use for-each loop on an array inside a function. Which I still don't understand. Then there this. So what is the difference between outside the main and inside + why does the array need to be sized although I can just leave it empty and it should be infinite.