字符串文字和命令lind参数的地址

时间:2016-04-03 05:33:44

标签: c

嘿伙计们,我正在尝试打印出字符串文字的地址以及命令行参数的开头和结尾。

int main(int argc, char *argv[]) {
    printf("Address of argc: %p\n", (void*)&argc); //Is this how u find the address of argc?
    //How to find out the start and end of command line arguments?
    printf("Start of argv: %p\n", (void*)argv); //Like this? I am not sure...

    char* strLiteral = "Hello world";
    //how to find the address of "Hello world"? (Address of string literal)
}

我做了我的研究,我听到了答案,比如不接受字符串文字的地址......这是真的吗?那是什么意思?字符串文字没有地址?请告诉我如何获取命令行参数的开始和结束地址。 感谢您抽出宝贵时间。

1 个答案:

答案 0 :(得分:-1)

您可以获取字符串文字的地址。看看下面的代码,特别是从main ...返回之前的行。

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

int main(const int argc, char *argv[]) {
  assert(argc > 1); 
  printf("argc    == %d   &argc    == %p\n",  argc, &argc);
  printf("argv[0] == %s   &argv[0] == %p\n",  argv[0], &argv[0]);
  printf("argv[1] == %s   &argv[1] == %p\n",  argv[1], &argv[1]);
  const char *test     = "You can take the address of this";
  printf("test == %s      &test == %p\n",  test, &test);
  printf("*address of string literal == %p\n",  &"address of string literal");
  return 0;
}