c - Structure element is unable to assign this is asked but i coudn't get apporiate ans? -
Hello structure is unable to specify the element (string) if it is assigned as code given below during the creation of this object Why is not done? My code is below please help me understand
struct st {int i; Four f [10]; Four * ch1; }; Int main () {struct st var2 = {"hello"}; // This is a good str str; Var.ch = "Hello"; // Why this bad? // so var.i = 9; // Why is it good? Var.ch1 = "hello"; // good why? }
straight street var2 = {"hello"}; // This is good
No, it's not good. :) You are trying to start the integer varaiable i with the first letter of the string as the "Hello" indicator.
var.ch = "Hello"; // Why is it bad?
This is really bad because arrays do not have any assignment operators. You have been declared the standard function strcpy
in the header & lt; String.h & gt;
for copying that string in the Data Member ch
strcpy (var.ch, "hello");
var.i = 9; // Why is it good? Var.ch1 = "hello"; // good why?
This is good because the built-in type (elemental type) has assignment operators, including pointers.
In the last statement, the string literal "hello"
is converted to its first element in the pointer which is to the letter 'H' and this pointer to ch1 Is assigned.
Comments
Post a Comment