Jump to content

  • Log In with Google      Sign In   
  • Create Account

Could someone explain this?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 LAURENT*   Members   -  Reputation: 242

Like
0Likes
Like

Posted 18 March 2014 - 05:48 PM

Is there like any different between these two main functions? I was just exploring the internet looking at code and notice this small difference.

int main (int argc, char* args[])

int main (int argc, char *argv[])



Sponsor:

#2 Nathan2222_old   Members   -  Reputation: -400

Like
-8Likes
Like

Posted 18 March 2014 - 05:52 PM

And this:
int main()

UNREAL ENGINE 4:
Total LOC: ~3M Lines
Total Languages: ~32
smile.png
--
GREAT QUOTES:
I can do ALL things through Christ - Jesus Christ
--
Logic will get you from A-Z, imagination gets you everywhere - Albert Einstein
--
The problems of the world cannot be solved by skeptics or cynics whose horizons are limited by the obvious realities. - John F. Kennedy


#3 LAURENT*   Members   -  Reputation: 242

Like
0Likes
Like

Posted 18 March 2014 - 05:58 PM

Nah I know about declaring the type of function.



#4 Álvaro   Crossbones+   -  Reputation: 13897

Like
8Likes
Like

Posted 18 March 2014 - 05:59 PM

The spacing doesn't make a difference. Some people prefer to put the `*' with the `char' because they think of "pointer to char as the type". I prefer to put the `*' with the parameter name, because it is parallel to variable declarations, where the syntax of the language clearly associates the `*' with the variable.

Here's an example of this last point:
  char* a_pointer, another_pointer; // Actually, `another_pointer' is just a char!!

You can also write
int main(int argc, char * argv[])


#5 ElDuro   Members   -  Reputation: 559

Like
4Likes
Like

Posted 18 March 2014 - 06:08 PM

I like to say char * a_pointer;

You can also declare it as int main(int argc char **argv);

http://stackoverflow.com/questions/3898021/mainint-argc-char-argv



#6 Vortez   Crossbones+   -  Reputation: 2704

Like
4Likes
Like

Posted 18 March 2014 - 06:18 PM

And this:

int main()

 

Are you trolling now? Because, i can't tell if you are or if this is a question.



#7 LAURENT*   Members   -  Reputation: 242

Like
0Likes
Like

Posted 18 March 2014 - 06:19 PM

COOL! That solve one of my problem even if breaks the rules of pointers. You guys didn't notice args or does that not matter as well?



#8 Álvaro   Crossbones+   -  Reputation: 13897

Like
7Likes
Like

Posted 18 March 2014 - 06:26 PM

You guys didn't notice args or does that not matter as well?


Do you mean the name of the parameter? It can be anything you want. `argc' and `argv' are just traditional choices.

#9 LAURENT*   Members   -  Reputation: 242

Like
0Likes
Like

Posted 18 March 2014 - 06:32 PM

I just thought the name would have more important in a main function and it used a lot. Thanks for clearing this up



#10 Hodgman   Moderators   -  Reputation: 31798

Like
12Likes
Like

Posted 18 March 2014 - 06:46 PM

You can call it Sally if it makes you happy. laugh.png



#11 Nathan2222_old   Members   -  Reputation: -400

Like
0Likes
Like

Posted 18 March 2014 - 10:30 PM

And this:

int main()
 
Are you trolling now? Because, i can't tell if you are or if this is a question.
It's a real question. I've seen those things before within the main() and didn't know what they did.

UNREAL ENGINE 4:
Total LOC: ~3M Lines
Total Languages: ~32
smile.png
--
GREAT QUOTES:
I can do ALL things through Christ - Jesus Christ
--
Logic will get you from A-Z, imagination gets you everywhere - Albert Einstein
--
The problems of the world cannot be solved by skeptics or cynics whose horizons are limited by the obvious realities. - John F. Kennedy


#12 Hodgman   Moderators   -  Reputation: 31798

Like
8Likes
Like

Posted 18 March 2014 - 10:38 PM

int main() just means that you don't care about command line arguments. 

You can either use:

int main() -- and get no arguments, 

or

int main(int argc, char * argv[]) -- and get an array of command line arguments as C-strings, where argc is the array size, and argv is the array of pointers.



#13 Kirkkaf13   Members   -  Reputation: 301

Like
1Likes
Like

Posted 19 March 2014 - 07:47 AM

 

And this:

int main()

 

Are you trolling now? Because, i can't tell if you are or if this is a question.

 

This is the guy who thinks he is going to create a more realistic GTA V replicate. The same guy who had half a page of quotes for a signature. The same guy who has been learning C++ for the last 4 months and doesn't know what a command line argument is. The same guy who has been part of the community for for 5 months, posted almost 500 posts and has a reputation of -367.

 

That Guy.

 

I apologize to the original poster whos topic has been hi-jacked.



#14 Kirkkaf13   Members   -  Reputation: 301

Like
0Likes
Like

Posted 19 March 2014 - 09:58 AM

 

int main() just means that you don't care about command line arguments.
You can either use:
int main() -- and get no arguments.

Thanks.
@kirkaff: and your point is?

 

 

My point is self-explainatory, need I say more? Your attention to detail is appalling my username is Kirkkaf13 (two k's one f), that is what is called a bug, I am sure you are familiar with them.

 

The original question has been answered, I think this thread should be closed before Nathan2222 posts and receives more down votes. Again, I would like to apologize for my actions but this is infuriating.

 

For the people reading this thinking I am being unreasonable or some sort of jerk, please read through Nathan2222 previous posts and thread hi-jacks.


Edited by Kirkkaf13, 19 March 2014 - 10:01 AM.


#15 rip-off   Moderators   -  Reputation: 8713

Like
0Likes
Like

Posted 19 March 2014 - 10:09 AM

Everyone back on topic, please. I won't ask nicely again.

 


I just thought the name would have more important in a main function and it used a lot.

They're important, yes. Names are vital to communicate intent to other programmers. Hence, dyou certainly shouldn't take Hodgman's suggestion of Sally too seriously!

 

But funnily enough C++ does not require that the names match between declarations and definitions.

void example(int one, int two);
 
int main()
{
    example(42, 13);
}
 
void example(int a, int b)
{
    std::cout << "A: " << a << ", B:" << b << std::endl;
}

 

In fact, you don't need to provide names when declaring a function, and if you don't wish to use a parameter you don't need to give it a name in a definition:

void example(int, int);
 
int main()
{
    example(42, 13);
}
 
void example(int a, int)
{
    std::cout << "A: " << a << ", but we don\'t care about B..." << std::endl;
}

It is typical for them to be present and to match, again to help provide clarity.

 
Thus, to the code that "calls" main, i.e. the runtime, it doesn't matter what the parameters names are or if they even have names.


#16 Lactose!   GDNet+   -  Reputation: 3807

Like
0Likes
Like

Posted 19 March 2014 - 10:22 AM

Related to function/parameter naming, main has to be called main. You can't rename that.

(Well, for console stuff, at least. Win32 has different naming requirements, etc.)



#17 Godmil   Members   -  Reputation: 744

Like
0Likes
Like

Posted 19 March 2014 - 10:48 AM

For posterity, it's probably worth also pointing out that since the start of an array is kinda the same as a pointer, this is also valid:

int main (int argc, char** argv)


#18 jHaskell   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 19 March 2014 - 01:15 PM

*edited out*

 

Somehow I missed reading over half the posts in this thread.  No new information here.


Edited by jHaskell, 19 March 2014 - 01:17 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS