Initializing an array belonging to a class [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
CODE1(input):
#include <cstdio>
struct College {
char name[256];
};
void print_name(College* college_ptr) {
printf("%s College\n", college_ptr->name);
}
int main() {
College best_colleges[] = { "Magdalen", "Nuffield", "Kellogg" };
print_name(best_colleges);
}
OUTPUT:
Magdalen College
I have read that "At the slightest provocation, an array will decay into a pointer.A decayed
array loses length information and converts to a pointer to the array’s first element."
For example:
int key_to_the_universe[]{ 3, 6, 9 };
int* key_ptr = key_to_the_universe; // Points to 3
QUESTION1 :
I have also learned how a class and array are initialized but i dont understand how an array in a class can be initialized. According to me array in a class should be initialized ( referring CODE1) this way :
College best_colleges;
best_colleges.name[]={"Magdalen", "Nuffield", "Kellogg"};
QUESTION2:
(Referring CODE1)I also dont understand that if best_colleges array gets converted to pointer college_ptr which points to first element of it i.e
College* college_ptr = &best_colleges[0];
then in function "print_name" , "college_ptr->name" should be equal to "&best_colleges[0]->name". why we need to write "college_ptr-> name" ?
QUESTION 3:
I dont understand if best_colleges is a class or an array according to how it has been initialized.

Related

In C++, why can int initialize a variable using new operator but double cannot? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int* i = new int(75);
double* d = new double(3.14159);
printf("%d\n",*i);
printf("%d\n",*d);
}
In the above code i returns a value of 75 however, d returns 1.
I tried explicitly initializing it as
*d = 3.14159
But the value is still returned as 1.
Can anyone explain what I am doing wrong here?
Use this for printing.
cout<<*i;
cout<<*d
"%f" is the (or at least one) correct format for a double if you want to use printf for printing the value of the double in C++.

casting a vector's size() as int does not work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am learning C++ on the fly and am having problems with vectors so I am writing some programs that use vectors to familiarize myself with them.
I was following the advice from this post regarding printing out the value of a vector's size() call:
How can I get the size of an std::vector as an int?
My code is a simple C++ code:
#include <vector>
int main(int argc, char ** argv) {
/* create an int vector of size 10, initialized to 0 */
std::vector<int> int_list[10];
int int_list_size;
int_list_size = static_cast<int>(int_list.size()); // <-- compilation error here
} // End main()
I'm on Ubuntu 16.04 and I get this error:
"error: request for member 'size' in 'int_list', which is of non-class type 'std::vector<int> [10]'
Since the size of the vector int_list is 10, shouldn't size() return 10, which I can then cast to an int?
You are not creating a vector, you are creating an array of vector:
std::vector<int> int_list[10];
You should use:
std::vector<int> int_list(10);
See:
https://www.cplusplus.com/reference/vector/vector/vector/
https://en.cppreference.com/w/cpp/container/vector/vector

Pointer in c++for scanning an array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
How to scan an array in c++ using pointer?
I have learned c programming .so i tried that way.
include<iostream>
using namespace std;
main()
{
int a[5],*p,i;
p=&a[5];
for(i=0;i<5;i++)
{
cout<<"enter"<<i+1<<endl;
cin>>(p+i);
}
for(i=0;i<5;i++)
{
cout<<*(p+i)<<endl;
}
}
I am expecting to scan using pointer just like in c programming
p=&a[5];
&a[5] is the address to one past the last element of the array. Incrementing this pointer, as well as indirecting through the pointer have undefined behaviour.
What you need is a pointer to the first element. You can use:
p = &a[0]; // this
p = a; // or this
Or you could simply not use p in the first place, and access a[i] directly.
cin>>(p+i);
This is wrong. p+i is a pointer. You cannot extract into an int pointer. You should extract into the integer object instead:
cin >> p[i];
cin >> a[i]; // or without p, as I pointed out above

Getting iterator for an array with a constant length in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I read on another stackoverflow post (variable length array error when passing array using template deduction) that the following should be possible:
#include <iostream>
int input() {
int a;
std::cin>>a;
return a;
}
int main()
{
const int b = input();
int sum[b];
std::begin(sum);
}
Except that it doesn't seem to work, I still get an similar error.
In function 'int main()':
16:17: error: no matching function for call to 'begin(int [b])'
16:17: note: candidates are:
Followed by information on possible templates it could fit.
You can use std::begin(sum) only when sum is regular array, not when it is a variable length array.
The following is OK.
const int b = 10;
int sum[b];
std::begin(sum);
In your case, b is not known at compile time. For arrays whose length are not known at compile time, it's better to use std::vector instead of relying on a compiler specific extension. The following is OK.
const int b = input(); // You can use int b, i.e. without the const, also.
std::vector<int> sum(b);
std::begin(sum);

Use pointers to create a class member [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am really confused about pointers. Read some articles about them and thought i got them, but i seems i didn't. I want to create a Team "Team1" which i normally would create by Team Team1; I have the name Team1 already stored as a string from an reading input of a textfile and thought i could create this team by pointers.
#include <iostream>
#include <string>
#include <vector>
class Team
{
private:
std::string m_teamname;
};
int main()
{
std::string wort = "team1";
std::string* pointer;
pointer = &wort;
std::string wort2 = *pointer;
std::cout << wort2;
Team *pointer;
std::cin.get();
}
I got the error C2371: 'pointer' : redefinition; different basic type " which is quite selfexplaning but still i dont get why it does not work as *pointer shows to the adress where the string "team1" is stored. Is there a way to do it ?
This has nothing to do with pointers, per se. You are defining the same variable twice with different types (as the error says: "pointer: redefinition").
std::string* pointer;
...
Team *pointer;
You define a variable named pointer with type std::string*, but then you try and define another variable with the same name. You'll have to rename one of them to something else.
You'd run into the same problem with any type:
int x;
double x; // <- error: there's already an x