Reputation: 115
#include <iostream>
#include <cstring>
using namespace std;
class student {
int roll_no;
char name[56];
public:
void setdata(int roll_no_in, const char* name_in) {
roll_no = roll_no_in;
strcpy(name, name_in);
}
void outdata() {
cout << "rollno is " << roll_no << endl << "name is " << name << endl;
}
};
int main() {
student s1;
s1.setdata(12, "robin");
s1.outdata();
return 0;
}
i have some doubts in this program
const
in the method function set data if i don't use
that then it shows me error. It is optional right?char name_in[34]
in the place of char *name_in
in the setdata
method function?Upvotes: 0
Views: 58
Reputation: 254461
how can we store strings into pointer like above program storing the string johnson into
char *name_in
since pointers are used only to store address.
You're not storing it in name_in
, you're storing it into name
, which is an array. name_in
is a pointer to the array passed to the function; the array is automatically converted to a pointer when passed to a function. (More generally, a pointer can point to the start of an array, and be used to access the array; C-style functions like strcpy
do exactly that.)
In idiomatic C++, you'd use a class rather than this dangerous mucking about with pointers and arrays:
std::string name;
name = name_in;
why should i add const in the method function set data if i dont use that then it shows me error.
I assume you mean in const char * name_in
. You're passing a pointer to a string literal, which is constant; and the language doesn't allow you to take a non-constant pointer to a constant object.
It is optional right?
Before C++11, it was optional; leaving it out was merely a very bad idea. Now it's mandatory.
why can't i use
char name_in[34]
in the place ofchar *name_in
in the setdata method function
You can; as a function argument, both are equivalent. Again you'll need const
to be able to pass a string literal or other constant string. It would be somewhat misleading though, implying that name_in
is an array, of a particular size, when neither are guaranteed.
Again, this is C++, so std::string
is almost certainly a better option than either.
Upvotes: 1
Reputation: 780974
C strings are arrays of char
. When you pass an array as a function argument, it's converted to a pointer to the first element.
The const
modifier indicates that the function will not modify the contents of name_in
. Since string literals are constants, you need this to permit the function to be called with a literal argument.
You can. But since the function doesn't actually have a limit on the size of the string it will accept, that would be misleading. Declaring a parameter as an array with a length is treated just like declaring it as a pointer; the length you specify is ignored. Note: This is only true for the first dimension; when passing a multi-dimensional array, you can omit the first dimension's length, but need to specify all the other dimensions.
Upvotes: 1
Reputation: 10733
*how can we store strings into pointer like above program storing the string johnson into char name_in since pointers are used only to store address.Can we store even strings?
You are actually storing pointer to the base address of the string. You can use this pointer to traverse whole string.
why should i add const in the method function set data if i dont use that then it shows me error
"Ravi" is a string literal which is stored in memory which is read only. So, if you try to pass this literal to function accepting char *
, it would be the violation of the constraint compiler is trying to establish.
*why can't i use char name_in[34] in the place of char name_in in the setdata method function?
You are passing pointer to char to this function, so you have to use pointer to char to accept it. However, arrays , when passed to function as argument decays to a pointer.
Upvotes: 0