Conversation started Sep 13, 2012 at 2:51.
Sep 13, 2012 02:51
here char* argv[] this is an array of char pointerS?
@MohamedAhmedNabil Yes.
@Insilico My book said they are read only but i easily edited them
int main(int argc, char* argv[]){
	cout<< argv[0]<<endl;

	argv[0]="Hello";
	cout<<argv[0]<<endl;
	cin.get();
	return 0;
}
maybe the book means what they are pointing to?
@MohamedAhmedNabil What book?
argv[0] is always the same of your program.. well supposed to be.
@Insilico C++ without fear
Sep 13, 2012 03:08
Just watched a ShayCarl video for the first time in like 2 years. Holy shit.
Anyone make any sense of this comment?
@Mysticial rand() returns a values that is in the user input range, doesn't create the mix between a faked entropy and a predictible behaviour, if you input 0 and 100 you get something between 0-100 and this is not a precise and predictible behaviour to me. — hthy46vbs 1 min ago
"rand() returns a values that is in the user input range"
Well.. it's incorrect.
I'm pretty sure that's not how rand() works.
I guess hthy46vbs wants a pseudo-random number generator?
Why can't he use the <random> header? It probably does what he wants to do much better than he could possibly implement it.
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.
char foo = 'a'; char* p = &foo;
char * bar = "a"; char * q = &bar;
Notice the difference: In the given example, we had a single char named foo. In my example, I used the string literal "a" (which allocates as two chars: 'a' and '\0' or number zero).
That was stored as bar.
 
Conversation ended Sep 13, 2012 at 4:00.