B
blackrosezy
#include
char *code;
void main()
{
char buf[8] = "book";
strcpy(code, buf);
}
char *code;
void main()
{
char buf[8] = "book";
strcpy(code, buf);
}
#include
char *code;
void main()
{
char buf[8] = "book";
strcpy(code, buf);
}
#include
is said:char *code;
void main()
{
char buf[8] = "book";
strcpy(code, buf);
Marcus Kwok said:#include
It seems your newsreader ate your include statement. I will assume it
is said:char *code;
You never initialize nor allocate any memory for code.
void main()
int main()
main() ALWAYS returns an int in conforming code.
{
char buf[8] = "book";
strcpy(code, buf);
You never initialized code, so it is pointing to garbage.
Here is a working example, though prefer std::string to raw char*'s.
#include <cstring>
char* code;
int main()
{
char buf[] = "book";
code = new char[std::strlen(buf) + 1];
std::strcpy(code, buf);
delete[] code;
}
Marcus Kwok said:#include <cstring>
char* code;
int main()
{
char buf[] = "book";
code = new char[std::strlen(buf) + 1];
std::strcpy(code, buf);
delete[] code;
}
Jeffrey Baker said:Another way that is simpler is:
#include<iostream>
using std::cout;
using std::endl;
#include <cstring>
int main()
{
char* code;
char buf[] = "book";
code = buf;
cout << code << endl;
return 0;
}
Marcus Kwok said:Marcus Kwok said:#include <cstring>
char* code;
int main()
{
char buf[] = "book";
code = new char[std::strlen(buf) + 1];
std::strcpy(code, buf);
delete[] code;
}
Jeffrey Baker said:Another way that is simpler is:
#include<iostream>
using std::cout;
using std::endl;
#include <cstring>
int main()
{
char* code;
char buf[] = "book";
code = buf;
cout << code << endl;
return 0;
}
Yes, but then if you change an element of code, it also changes the
corresponding element of buf. In my version, code and buf are distinct
entities (that happen to have the same content), so if you change one,
the other is unaffected.
char* code;
char buf[] = "book";
code = new char[std::strlen(buf) + 1];
std::strcpy(code, buf);
Jeffrey Baker said:char* code;
char buf[] = "book";
code = buf;
Jeffrey Baker said:I agree the new operator is good to use. I see a similarity between both
ways. In the "new" you delete the pointer so it no longer exists. In the
other eg. code = buf; , code no longer points to buf like "new" no longer
points to code. In my example code stays pointed to the object till code is
reassigned. That is like reasigning code with new after delete[] code; is
used. code is reassigned.
In both cases there needs to be a number of pointers in my example and with
new it seems to be the same to keep the data otherwise it is destroyed.
Marcus Kwok said:char* code;
char buf[] = "book";
code = new char[std::strlen(buf) + 1];
std::strcpy(code, buf);
char* code;
char buf[] = "book";
code = buf;
Jeffrey Baker said:I agree the new operator is good to use. I see a similarity between both
ways. In the "new" you delete the pointer so it no longer exists. In the
other eg. code = buf; , code no longer points to buf like "new" no longer
points to code. In my example code stays pointed to the object till code
is
reassigned. That is like reasigning code with new after delete[] code; is
used. code is reassigned.
In both cases there needs to be a number of pointers in my example and
with
new it seems to be the same to keep the data otherwise it is destroyed.
I'm not quite sure what you're getting at here, but here is a program
that demonstrates what I am talking about:
#include <iostream>
#include <cstring>
int main()
{
std::cout << "My way:\n";
char* code;
char buf[] = "book";
code = new char[std::strlen(buf) + 1];
std::strcpy(code, buf);
std::cout << "code: " << code << ", buf: " << buf << '\n';
code[0] = 'n';
std::cout << "code: " << code << ", buf: " << buf << '\n';
delete[] code;
std::cout << "\nYour way:\n";
code = buf;
std::cout << "code: " << code << ", buf: " << buf << '\n';
code[0] = 'n';
std::cout << "code: " << code << ", buf: " << buf << '\n';
}
Output:
My way:
code: book, buf: book
code: nook, buf: book
Your way:
code: book, buf: book
code: nook, buf: nook
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.