Sign in to follow this  
derek7

char c[] = "12345678"

Recommended Posts

Boder    938
The compiler creates a place in memory with "123456789\0" not "1234567890" and points c to it.

But I can say that "c" is probably not assigned 12345678, it is given a memory address.

Share this post


Link to post
Share on other sites
derek7    100
Quote:
Original post by Boder
The compiler creates a place in memory with "123456789\0" not "1234567890" and points c to it.

But I can say that "c" is probably not assigned 12345678, it is given a memory address.


all char is number. so '\0\ is exact equal to 0. maybe I wrong.but it often confuse people. I thing \0 is exact a number it is 0.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by derek7
Quote:
Original post by Boder
The compiler creates a place in memory with "123456789\0" not "1234567890" and points c to it.

But I can say that "c" is probably not assigned 12345678, it is given a memory address.


all char is number. so '\0\ is exact equal to 0. maybe I wrong.but it often confuse people. I thing \0 is exact a number it is 0.
Yes, \0 equals ASCII 0, but ASCII 0 does not equal '0'. That's got a code of 48 or something (Don't know the number off the top of my head)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Boder
The compiler creates a place in memory with "123456789\0" not "1234567890" and points c to it.

But I can say that "c" is probably not assigned 12345678, it is given a memory address.
Actually the string 12345678 is assigned to "c", because "c" is an array. If "c" was a char pointer, you'd be correct.

Share this post


Link to post
Share on other sites
derek7    100
Quote:
Original post by Evil Steve
Quote:
Original post by derek7
Quote:
Original post by Boder
The compiler creates a place in memory with "123456789\0" not "1234567890" and points c to it.

But I can say that "c" is probably not assigned 12345678, it is given a memory address.


all char is number. so '\0\ is exact equal to 0. maybe I wrong.but it often confuse people. I thing \0 is exact a number it is 0.
Yes, \0 equals ASCII 0, but ASCII 0 does not equal '0'. That's got a code of 48 or something (Don't know the number off the top of my head)


if('\0' == 0)::MessageBox(0," equal ",0,0);

if('0' == 48)::MessageBox(0," equal ",0,0);

so \0 = number 0

Share this post


Link to post
Share on other sites
Mushu    1396
Quote:
Original post by Anonymous Poster
Quote:
Original post by Boder
The compiler creates a place in memory with "123456789\0" not "1234567890" and points c to it.

But I can say that "c" is probably not assigned 12345678, it is given a memory address.
Actually the string 12345678 is assigned to "c", because "c" is an array. If "c" was a char pointer, you'd be correct.

Actually, an array is a pointer. I was lied to as a child.

Quote:
Original post by derek7
if('\0' == 0)::MessageBox(0," equal ",0,0);

if('0' == 48)::MessageBox(0," equal ",0,0);

so \0 = number 0

Uhh, you've just shown that the ASCII code for -

'\0' == 0
'0' == 48

Therefore '\0' != '0'.

Share this post


Link to post
Share on other sites
JohnBSmall    881
Quote:
Original post by Mushu
Quote:
Original post by Anonymous Poster
Quote:
Original post by Boder
The compiler creates a place in memory with "123456789\0" not "1234567890" and points c to it.

But I can say that "c" is probably not assigned 12345678, it is given a memory address.
Actually the string 12345678 is assigned to "c", because "c" is an array. If "c" was a char pointer, you'd be correct.

Actually, an array is a pointer.

No.

John B

Share this post


Link to post
Share on other sites
smart_idiot    1298
I can only conclude that deep down, all arrays have an unquenchable desire to be pointers, as they will convert themselves to pointers at the slightest provocation. It's kind of sad, really. Hopefully one day they'll be more comfortable with themselves and not try to be what they assume everyone wants them to be. They will not find happiness by pretending to be something they're not.

Share this post


Link to post
Share on other sites
Boder    938
I found a truly humerous error message testing out this array stuff.

edit: Saved my dignity.

Is there such thing as a non-const array?

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by Boder
That's actually a const array to three mutable ints. Try to modify "arr"


That's meaningless. "arr" itself doesn't really exist, only its contents do. That's the difference between an array and a pointer.

An array is not so much "const" as it is a literal, like 3.1415, 42 or "hello world" (which has for type const char[12], BTW).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
How could this conversation be going on for so long.

An array is a set of bytes defined at compile time, (unless you're using the new operator in C++, then they can also have space defined at runtime). A pointer is simply a place to store the address of a location in memory. You can dereference a pointer and access the memory it points to and even change what it's pointing to so you can go through memory as if it were an array. Pointers have no specific storage space defined to it like an array, so a pointer is able to point to random locations in memory.

Share this post


Link to post
Share on other sites
Boder    938
Notice when you type

char foo[] = "bar";

The strange thing is that you can modify it without fault, I thought that wasn't allowed.

[Edited by - Boder on April 25, 2006 12:40:58 AM]

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by Boder
That's actually a const array to three mutable ints.


No, it's just a mutable array, for a const literal array is synonymous with an array of constant elements. There is no seperation with arrays, only pointers.

Observe:

class example {
int array[3];
int * pointer;
public:
example() { pointer = & array; }

void foo( void ) {
pointer[0] = 0; //legal, reading mutable-pointer, modifying mutable-elements
array[0] = 0; //legal, modifying mutable-array
}
void bar( void ) const {
//(note, function is const)
pointer[0] = 0; //legal, reading const-pointer, modifying mutable-elements
array[0] = 0; //ILLEGAL - modifying const-array
}
};




The fact that arrays are not assignable is a property shared by references and many classes as well:

class example : boost::noncopyable {
int i;
public:
void modify( int i ) { this->i = i; }
};

int main () {
example foo;
example bar;
foo.modify( 42 ); //legal, foo is mutable
bar.modify( 42 ); //legal, bar is mutable
bar = foo; //illegal, although both foo and bar are mutable (non-const)
}



This does not make them const, merely unreassignable.

Share this post


Link to post
Share on other sites
Boder    938
Nice post, MaulingMonkey! Did you mean to make the pointer in the first example const?


A new question.

Is it legal to read and write to an executable file while it is running by opening it as a file?

Where is a const string in memory like so, and how come it can't be modified?

char * str = "hello world";

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Boder
Nice post, MaulingMonkey! Did you mean to make the pointer in the first example const?


He might have, or might not have. Either way, the result is the same. Using the operator & on an array is pretty much equivalent to &array[0].

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Oops, nevermind, read the question wrong the first time around.

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by Boder
Nice post, MaulingMonkey! Did you mean to make the pointer in the first example const?


Nope, although I could have made it so if I had initializer-list-initialized the pointer instead of merely assigning it.

[qipte]A new question.

Is it legal to read and write to an executable file while it is running by opening it as a file?[/quote]

OS filelocks would prevent opening for write access on any file being read (which most executables will be as they are executing, by the OS). Any method that circumvents this would either:

1) Circumvent the read lock, causing OS-defined results (probably not pretty)
2) Close the file read, causing any changes to the EXE to have no effect on the running program (used by some autopatcher utilities to self patch)

Quote:
Where is a const string in memory like so, and how come it can't be modified?

char * str = "hello world";


It starts out in an EXE's .data section, part of the executable file. While the specifics of loading the file into memory and starting execution are the domain of the OS, the basic gist of it is that it's loaded as read-only memory, since you shouldn't be writing to it (if you use "hello world" again throughout the program, your compiler may reuse the address - meaning modifying it in one place modifys it everywhere you use that exact string. It's also just as likely not to reuse it, meaning you have a big can of "undefined", which is allways nasty stuff to avoid).

An example running from GDB:

(gdb) run
Starting program: /Users/panda/evil
Reading symbols for shared libraries . done

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00002ff4
0x00002d40 in main ()


I'm not used to debugging on my iBook, so I can't say exactly what everything means - but, it boils down to, "You accessed memory, which was okay (otherwise there'd be a segfault), but then you tried to write to it, which wasn't without the write flag enabled, so I'm telling you that you had no right (BAD_ACCESS) to do that.

Since the compiler gives you an easy way to have a single mutable array for you to play with if you want:

char str[] = "hello world";
str[4] = '0'; //lol, hell0 = teh 1337speak


You can get such an effect without inconveniencing the compiler as to figuring out where you want that string allocated :-).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this