Where are C/C++ main function's parameters?

前端 未结 10 1834
清酒与你
清酒与你 2020-11-29 02:31

In C/C++, the main function receives parameters which are of type char*.

int main(int argc, char* argv[]){
  return 0;
}

10条回答
  •  粉色の甜心
    2020-11-29 02:53

    Usually it is unknown where they are.

    #include 
    #include 
    
    int main(int argc, char *argv[]) {
      char **foo;
      char *bar[] = {"foo", "bar"};
    
      (void)argv; /* avoid unused argv warning */
    
      foo = malloc(sizeof *foo);
      foo[0] = malloc(42);
      strcpy(foo[0], "forty two");
    
      /* where is foo located? stack? heap? somewhere else? */
      if (argc != 42) main(42, foo); else return 0;
    
      /* where is bar located? stack? heap? somewhere else? */
      if (argc != 43) main(43, bar); else return 0;
      /* except for the fact that bar elements
      ** point to unmodifiable strings
      ** this call to main is perfectably reasonable */
    
      return 0;
      /* please ignore memory leaks, thank you */
    }
    

提交回复
热议问题