why do we need strcpy()? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
struct prac
{
int name[3];
char name1[12];
} b1, b2, c2;
main()
{
int i;
struct prac b2={1,2,3};
strcpy(c2.name1,"goodmorning");
printf("%s",c2.name1);
}
here to store the values in b2 array we just need to write b2={1,2,3} but if I want to store some values in the string c2 we need to call strcpy(), why it shows error if I write c2="goodmorning", rather than using strcpy()?

Because you can't assign to an array, only initialize it (which you do when you define the second b2 variable) or by copying to it (which you do with the strcpy call).

Related

What is the difference between pointers to array of pointers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I came across these two terms
Int (*q)[3][4] and. int q[ ][3][4].
What's the difference these two terms?
And one more question .
Char a[ ]="abcd";
Char *p="abv";
a="ghj";
p="ajk";
Printf("℅s℅s",a,p);
Why this would not compile?
It won't compile because of the line:
a = "ghi"
This assigns a const char* to a char* directly. You can use strcpy to copy the string intead:
strcpy(a, "ghi")
You will still have warnings at this point, because you have not declared p as a const. You can fix this like so:
const char* p = "abv"

type casting the arguments of a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If the code is like the below,
void func(std::string str)
{
...
}
void main()
{
std::string p1 = "abcd";
char p2[SOME_LENGTH] = "abcd";
func(p1); // (1)
func(p2); // (2)
}
which way is efficient between (1) and (2)?
They are equally efficient/inefficient. Both involves copying the string and using the copy as the value of the argument 'str'. A better way would be declaring func as
void func(const std::string &str) {
}
This can avoid copying of the string.

2 value in one variable (const and const_cast) c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
what makes type cast affected this?
where is the value stored?
The program has undefined behavior: with the const_cast<int*>(b) you remove the const qualifier from an object which actually is const and the assignment to that object may have arbitrary effect.
The observed effects indicate that the implementation replaced uses of a with its immutable value while it dereferences b to determine the value. It could have arbitrary other effect, too, though. For example, a segmentation fault when trying to write a write protected location could be a possible outcome. Well, anything can happen.

C++ lack of logic on pointer initialisation & use [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is not logic :
int *ptr = &otherInt;
When we do that, ptr gives adress of otherInt, *ptr gives otherInt.
But we write explicitly that (*ptr) equals adress *ptr = &otherInt !
Logically, *ptr should give the adress, and ptr the otherInt.
Don't you think ?
I think you got it all wrong. Consider this:
int *ptr;
int otherInt = 10;
ptr = &otherInt; // Notice this line.
The asterisk is a part of the declaration, not the assignment.

can a c vector store more than one data type? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'd like to write a function that will return 4 members of a class. They are 3 ints and char, and I'd like to store them all in one vector and return it from a function call. Can I do that?
You either need an std::tuple if you want to preserve the types and if the result length is constant, or just cast all members to some common supertype and store them in a container.
You need a class:
struct S
{
int a, b, c;
char letter;
};
int main()
{
S s;
}