5 ms·
--- a/resume.c +++ b/resume.c @@ -118,7 +118,7 @@ void print_job(job_t * job) { } } -int main(int argc, char * argv) { +in
by newobj 12y ago
--- a/resume.c
+++ b/resume.c
@@ -118,7 +118,7 @@ void print_job(job_t * job) {
}
}
-int main(int argc, char * argv) {
+int main(int argc, char ** argv) {
int i = 0;
while (jobs[i]) {
@@ -127,4 +127,4 @@ int main(int argc, char * argv) {
}
return 0;
-}
\ No newline at end of file
+}
- lukeasrodgers 12y agoFor those who don't write C, could you explain what is significant about this change? I'm guessing the earlier version was some kind of subtle bug?
- tcas 12y agochar * argv is a pointer to a string (or a single character). char ** argv is a pointer to an array of pointers to strings. The latter is used since it allows for multiple arguments in (the number being `int argc`). This picture shows the second example in terms of pointers: http://www.londonquilters.org.uk/ctext/pic511.gif http://www.londonquilters.org.uk/ctext/pic511.gif
- shultays 12y agoWouldn't second simply segfault when you try to access it?
- tcas 12y agoNope, neither will segfault on the initial access. char ** argv is a null terminated array in C according to the C standard. (EDIT: see clarification below) C doesn't care about types at all and you can abuse this by casting pointers to different (sometimes incompatible types). The second argument to main (argv) it set up by the initialization code and has the same space as a pointer regardless if it's a string, array of strings, struct, integer etc... The existing structure will simply be interpreted as that new type like so: String1 String2 String3 String4 char ** a2: |0x41414141 |0x42424242 |0x43434343 |0x00000000 | +-----------+-----------+-----------+-----------+ char * a1 : |41|41|41|41|42|42|42|42|43|43|43|43|00|00|00|00| printf("%s\n", a1); > AAAABBBBCCCC That being said, some compilers (clang) will emit an error on the improper type, like so: test.c:3:5: error: second parameter of 'main' (argument array) must be of type 'char **' int main(int argc, char * argv) ^ 1 error generated.
- pqomdv 12y agoWe are talking about C and not a specific compiler, so there are a lot of mistakes in your comment. char * * is a pointer to a pointer to a char and nothing else, it might be a NULL terminated array or not at all. C cares about types. Casting a pointer to an incompatible type or reinterpreting through an incompatible pointer is not defined in C( undefined behavior ). Pointers of different types are allowed to have different sizes. sizeof( char* ) == sizeof( char* * ) is not guaranteed in C. Also any program whose main is not int main( void ) or int main( int , char* * ) will result in undefined behavior. All of this is in the latest standard. Both, segfaulting or running completely normally can be a result of undefined behavior. That is why we really like to avoid it in C. Thus your advice is really not good for a modern C.
- tcas 12y agoIn the case of argv though on main it is defined to be null terminated on argv[argc] according to the standard. Assuming the pointers are the same size though what I said will work but is bad practice. > sizeof( char* ) == sizeof( char* * ) True, I forgot that different architectures/compilers can produce different pointer sizes. Thanks!
- pqomdv 12y agochar* * argv is NULL terminated, my mistake I though you were referring to that type in general.
- deleted 12y ago[deleted]
- shultays 12y agoYou input arguments into your program using argc and argv. argc is number of arguments and argv is the list of arguments. There is no string in c so you use char pointers instead. But in previous code he used char, which is usually defines a single string, but main actually inputs an array of string so it should have been char* (pointer to pointer of char, or pointer to 'string') Basically he did something like this in Java public static void main(String args) instead of public static void main(String args[])