pointer characters

Pointer character arrays;
when i declare arrays with empty braces and initialize it with string and try function "strcpy" to copy first string to second it will run and give no problem

question is that when i try to use strcpy function with pointer declared character initialized with string than the program crashes it does not run although it compile but it does not run.
1
2
3
4
5
6
7
8
9
   char *wordone-"honeybee";
char *wordtwo="simplebee";


strcpy(wordone,wordtwo);
cout<<wordone;


Last edited on
In your example code, wordone isn't an array. It's just a pointer. It's pointing to a string literal - this is stored in a bit of memory that's not available for you to try and overwrite.

In line 5, you're trying to overwrite that bit of memory with the string "simplebee". This is illegal.

Do you understand what a pointer is? Do you understand what an array is? Do you understand the difference between the two?

EDIT: And while it may compile, your compiler should be warning you that you shouldn't be doing that.
Last edited on
honeybee line has a weird subtraction. that probably was = ?


if you want to overwrite them, do this.

char w1[] = "honeybee";
char w2[] = "simplebee";

or
char safer[100] = "beez";


but printing of both declared arrays were same in both when cout a pointer declared characters and array declared .

when we print like
cout<<wordone; // honeybee

char wordone[]="honeybee";

cout<<wordone; // honeybee

but you're also right that when i use pointer declared characters compiler is giving the waring
Last edited on
but printing of both declared arrays were same in both when cout a pointer declared characters and array declared .

Yes. Why would you expect it not to?

Did you read what I wrote? Did you understand it?

Why am I bothering to ask you questions, when you didn't bother to answer the ones I asked in my previous post?
compiler is giving the waring

What warning is that?

Does it say:
This here char* wordtwo = "simplebee"; takes address of memory that THOU SHALT NOT MODIFY, but you dare to save the address in non-const pointer!

I hope it does.

These two have quite different initializations:
1
2
const char* foo  = "hello";
char bar[] = "Dolly";

The first one creates a pointer and stores the address of "hello" into it. The "hello" exists elsewhere in memory.
The second allocates 6 bytes of memory for an array and then copies characters of "Dolly" into that array.

Printing is a different situation:
1
2
std::cout << foo;
std::cout << bar;

Operator << is a function. There are many overloads of that function.

Guess which is used when you "print pointer"? The (std::ostream&, const char*)
http://www.cplusplus.com/reference/ostream/ostream/operator-free/

Now guess, which is used to "print array"?
I can tell that it is not a version that would take an array as argument, because no function takes a plain array as argument.

The correct answer is: (std::ostream&, const char*)
Yes, the array('s address) is converted into const char* pointer and that pointer goes to the function. (There are actually two conversions: (1) from char array into char* and (2) from char* into const char*.)

In other words, the same "print a C-string" operator is used for char pointers and char arrays.
char * blah = "foo";
^^^ You can't see it, but "foo" is going to be a constant piece of data. That is just what the compiler does with it.

you code is similar to this:

const int x = 3; //do you expect to be able to modify this?
x = 5; //compiler throws error. With pointers, it may only warn, but it is a critical failure just the same.

my code is more like this:

int x = 3; //the way I declared the strings is not constant.
x = 5; // ok

this is just a nuance of the language. I always hated the char* ? = "" format because it is an exception and different from almost everything else in the language. Thankfully we have strings now and don't have to deal with these anymore unless editing old code or writing C.

Last edited on
Topic archived. No new replies allowed.