This question already has answers here:
Segmentation fault on large array sizes
(7 answers)
Fill multidimensional array elements with 0's
(12 answers)
Closed 1 year ago.
I'm very new to C++ and was trying to initialize a large 2d zero array. However, if I run this it gives no errors but "test a" is not printed. Anyone know what I'm doing wrong?
#include<iostream>
using namespace std;
int main(){
cout << "test a" << endl;
int arrone[60480][12] = {};
}
Related
This question already has answers here:
size of array passed to C++ function? [duplicate]
(7 answers)
determine size of array if passed to function
(10 answers)
Sizeof in function [duplicate]
(1 answer)
Closed 6 months ago.
#include<iostream>
#include <string>
using namespace std;
void show_length(int array1[]){
int length=sizeof(array1)/sizeof(array1[0]);
cout<<"Length inside function = "<<length<<"\n";
}
int main(){
int array1[6]={3,6,4,2,1,4};
int length=sizeof(array1)/sizeof(array1[0]);
cout<<"Length inside main = "<<length<<"\n";
show_length(array1);
system("pause");
}
i recive 6 as an answer in the main (which is the right answer) but 2 in the separate function, why this happens?
This question already has answers here:
displaying address of char variable in c++ using pointers?
(1 answer)
Why does streaming a char pointer to cout not print an address?
(4 answers)
Closed 9 months ago.
As far as my knowledge in c++, the & character can act as an address of operator(finding the address of a variable in memory) or as a bitwise AND operator or declaring references.
However if I run this code:
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "Stackoverflow";
cout<<&s[0]<<endl<<&s[1]<<endl;
return 0;
}
Output is
Stackoverflow
tackoverflow
I expected it to print the addresses of the first 2 characters of the string, however, I got the string itself starting from a different index. How does it work?
This question already has answers here:
C++ : Creating an array with a size entered by the user
(3 answers)
How to create a dynamic array of integers
(8 answers)
Closed 1 year ago.
I know variable length arrays are not allowed in c++. My code is currently as follows:
int main(){
int t;
cin >> t;
double h[t];
}
How can I create an array with length t then?
Edit: the assignment only allows char, bool, int and double. Vector isn't allowed. When I tried to compile it, it says ISO C++ forbids variable length array 'h'.
This question already has answers here:
What is the size of sizeof(vector)? C++
(4 answers)
sizeof() a vector
(6 answers)
Closed 2 years ago.
I'm going through a beginner course on C++ and like to pause the lesson and tinker around with the code on what I'm learning. I had the idea to use the sizeof() operator in C++ on a vector and an array. Ended up finding out that with the same amount of numbers in the vector and array the size of them in bytes comes of different. Here is my code.
#include <iostream>
#include <vector>
int main() {
std::vector <int> test_scores { 100,99,90,70,75 };
int test_scores_array[]{ 100,99,90,70,75 };
std::cout << sizeof(test_scores) << std::endl;
std::cout << sizeof(test_scores_array) << std::endl;
return 0;
}
with an output of this
16
20
after about 30 minutes of googling I can't figure out why the size is different. I don't really "need" to know but I just find it really interesting that it is different and would really like to know why.
This question already has answers here:
Using sizeof on arrays passed as parameters [duplicate]
(3 answers)
What is array to pointer decay?
(11 answers)
Closed 5 years ago.
I'm trying to learn some basic C++ and the moment I started to think I got a grasp of all those pointers I stumbled across this problem:
int sizeOf(string texts[]) {
return sizeof(texts);
}
int main() {
string texts[] = {"apple", "banana", "orange", "watermelon"};
cout << sizeof(texts) << endl;
cout << sizeOf(texts) << endl;
}
and this function returns
128
8
My question is: what is happening when I pass this array as an argument? Why suddenly C++ forgets that this is an array? I have tried to dereference it inside a method (return sizeof(*texts)) but it returned 32 instead which is the size of one string element, not a whole array. Is it possible to do what I want to do? Where am I mistaken?