Conversation started Sep 13, 2012 at 3:19.
Sep 13, 2012 03:19
i cant change the value stored in char arrays without strcpy?
Let me rephrase it. I cant change the value stored in char arrays with assignment operator?
@MohamedAhmedNabil Shittiest example I can come up with. ideone.com/PR2Qx
I don't like C-style strings.
@Rapptz these are char pointers, i was talking about char arrays
@MohamedAhmedNabil ideone.com/bJWRn
@Rapptz but not directly a="aaaa";
Why would you do that?
That causes buffer problems.
Sep 13, 2012 03:26
I believe you can modify argv according to the C standard. Not sure about C++.
@Insilico I moved past argv. argv points to a string literal which is a const. but i can change what argv is pointing to
@MohamedAhmedNabil Of course. You can always modify the values passed to you via the parameters.
(Unless it was declared const, which is pointless)
The parameters passed to you in a function call are local to you.
@Insilico argv[] is a bunch of pointers. Each one points to a string literal in the memory. What is pointed to is a const but the pointer itself can be changed to point to other stuff
@MohamedAhmedNabil Yes. I just said you can modify the values passed to you via the parameters. What was passed to you are pointers to string literals. You can modify the pointers.
And according to the C99 standard you can modify the pointed strings as well. I don't know about C++, though.
@Insilico in C++ string literals are consts, pretty certain of that
Sep 13, 2012 03:34
@MohamedAhmedNabil Yes they are, but I don't see anywhere in the standard that says argv is an array of char pointers to string literals.
@Insilico Logic says so
@MohamedAhmedNabil Logic says what?
@Insilico The arguments are chars and are stored in the memory, same as string literals
@MohamedAhmedNabil Yes, but the fact that the strings are in memory implies nothing about their constness.
@Insilico should anything stored in the memory not as a variable be considered a const?
Sep 13, 2012 03:37
@MohamedAhmedNabil No.
constness is a property of C++ data types, not of what's in your computer's memory. The processor knows nothing about const data.
@Insilico Check this out :
int main(int argc, char* argv[]){
	*argv[0]='a';

	cout<<argv[0];
	cin.get();
	return 0;
}
it worked :D
You replaced the first letter with an a
I'm not entirely sure what you wanted to do.
@Rapptz if it was a const it wouldnt change right?
Who said it was a const?
const isn't a property that exists in C so why would it exists for the C++ standard of argv
@Rapptz I did, arrogantly
Sep 13, 2012 03:40
Also @Insilico I'm pretty sure from what I've read that the C and C++ argv are the same.
@Rapptz Actually, it does.
@Rapptz I thought that since string literals are const then this would be too
@EtiennedeMartel TIL
Although const doesn't have the same semantics in C and in C++, it does exist in both.
@MohamedAhmedNabil argv != string literal.
Sep 13, 2012 03:41
@MohamedAhmedNabil You misunderstand what a litteral is.
@Insilico but argv is a pointer to a char array right?
it can also be written as char** argv
@MohamedAhmedNabil No, it is an array of char pointers.
@Insilico i mean each element in it is a pointer to a char array
So what does const do in C?
Sep 13, 2012 03:42
@MohamedAhmedNabil Yes.
@Rapptz Makes a variable unmodifiable.
@Rapptz It's used mostly on constants and pointers.
@Insilico This means that argv elements point to char arrays that arent string literals?
@MohamedAhmedNabil What makes you think that char pointers have to point to string literals?
@MohamedAhmedNabil A string litteral is a string that is written in the source code, between quotes ("").
Sep 13, 2012 03:44
char buffer[1000]; char* p = buffer;
@Insilico i dont know
@Insilico No need for that &.
@EtiennedeMartel so there can be a char array in the memory that isnt a string literal?
@EtiennedeMartel Thank you.
@MohamedAhmedNabil Yeah, you can allocate one yourself if you want.
Sep 13, 2012 03:45
@MohamedAhmedNabil A string literal is just a sequence of characters that appears in source code.
@MohamedAhmedNabil ideone.com/bJWRn
Your RAM doesn't know the difference between a string allocated via a string literal or one allocated through some other means.
Crappy(?) example again
@Insilico Well, litterals might get in read only memory, so it depends on the OS.
a literal was written into the source code and is compiled was compiled as a constant.
Sep 13, 2012 03:46
@EtiennedeMartel Sure, but that sort of protection is enforced through the OS's virtual memory mechanism.
@Insilico Indeed.
The compiler (but not the runtime) will not let you alter it.
Another example: char foo = 'a'; char* p = &foo;
The char pointer p doesn't even point to an array of characters.
Once in ram, its like any other char* value -- a block of memory that means something to you and I.
I can't say I've been on porn tube sites to experience the amazing banner loading times, but I can't imagine there's anything special involved. Just use highly compressed video. — minitech 1 hour ago
Sep 13, 2012 03:48
Any ads with videos/sounds = automatic adblock
@Mysticial Hey, it's that minitech guy.
@Insilico this will cause problems
@MohamedAhmedNabil How so?
@MohamedAhmedNabil Like?
Sep 13, 2012 03:50
@Insilico if i do this cout <<p;
Correct.
a single char is not a null terminated string.
const char * literal = "abc";
Yes, if you cout << p; using the p in my previous example, things will not work.
const char * wasALiteral = literal;
That's why we tell people to use std::string, because C-style strings suck.
@JonathanSeng yep, i will need to dreffrence
Sep 13, 2012 03:51
In the compiled code, both literal and wasALiteral will point to an array in the loaded object code.
const char * newString = new char[32]; newString[0]= '\0';
This will be allocated at run time and is NOT a literal.
strcpy(newString, literal);
This COPIES the values in literal to the non-literal, dynamically allocated newString.
No, Mohammed. The problem with p is that it was just a char.
@JonathanSeng p is a pointer to a single char, not the char itself. You might want to change that. :-)
A c-style null terminated string exists in ram within an array of chars such that immediately after the last intentionally used character is the numeric value of 0.
p was a pointer to foo
However, on the stack we had:
...
single char foo with value a
@JonathanSeng correct me if im wrong, but isnt sending a char address with the stream operator, sends it untill it finds a null?
pointer p with value address of a.
If you use p as a string, it will take character 'a' and then look at p as if it were part of the string.
Correct mohamed.
The problem was that foo was a single char.
Not a character array with a null character byte.
Now, in practice, that example would likely work.
Since the compiler likely padded the stack frame as:
char foo, char unused, char unused, char unused, char pointer p....
and the unused would have been initialized as null.
But, that is very much and assumption and things would likely go very bad.
 
Conversation ended Sep 13, 2012 at 3:58.