c - 是否可以在 C 中将 char** 转换为 char*?

标签 c arrays char

当我制作一个遍历字符串的程序时,我让它使用 char* 类型的变量。但是我发现我需要向它传递一个 char** 类型的变量。

如果我可以将 char** line 转换为 char* line,一切都会完美无缺。有办法做到这一点吗?

编辑:这是我的代码的顶部。这适用于 char* 变量,但我无法让我的最终产品工作,除非我可以传入 char** line 并将其转换为 char*

int exiting(char* line, int argcptr){
   printf("char* line = %s\n", line);
   int words = argcptr;
   char* linecpy = (char*) malloc(100); 
   char* arg1 = " ";
   char* arg2 = " ";
   strcpy(linecpy, line);

   char* bin = strtok(linecpy, " "); //obtain the first word

   if(words > 1){
      linecpy += (strlen(bin) + 1); 
      arg1 = strtok(linecpy, " ");  //the next word will be the first argument, if applicable
      printf("arg1 = %s\n", arg1);
      words--;
   }
   if(words > 1){
      linecpy += (strlen(arg1) + 1); 
      arg2 = strtok(linecpy, " "); //if applicable, grab the third argument
   }


   //***exit [value]*** :  exit the shell with the value. if no value given, exit with value 0.
   if(strcmp(bin, "exit") == 0){

      if(strcmp(arg1, " ") == 0){
         printf("exiting with 0\n");
         exit(0);
      }
      else{
         int exitNum = atoi(arg1);
         printf("exiting with %d\n", exitNum); 
         exit(exitNum);
      }
   }
   ...

最佳答案

当你有这样的功能时:

void doSomething ( char ** linePtr ) {
    // ...
}

然后linePtr是一个指向字符串指针的指针,因此调用者可能是这样调用它的:

char * string = ...;
doSomething(&string);

如果你的函数中需要一个字符串指针,你只需要读取指针指向的值:

void doSomething ( char ** linePtr ) {
    char * line = *linePtr;
    // ...
}

但是请注意 char **也可以是指向字符串指针数组的指针:

char ** strings[] = {
    "String1", "String2", "String3", "String4", NULL
};
doSomething(strings);

在这种情况下,您要么需要知道数组长度:

void doSomething ( char ** linePtr, size_t arrayLength ) {
    for (size_t i = 0; i < arrayLength; i++) {
        char * line = linePtr[i];
        // ...
    }
}

或者数组必须如上所示以NULL结尾,那么你可以通过检查NULL来找到它的结尾:

void doSomething ( char ** linePtr ) {
    for (size_t i = 0; linePtr[i]; i++) {
        char * line = linePtr[i];
        // ...
    }
}

这里还有一些指针魔术:

char c = 5;
char * cptr = &c; // `cptr` is the address of `c`
char ** cptrptr = &cptr; // `cptrptr` is the address of `cptr`

char * anotherCptr = *cptrptr; // `anotherCptr` is the address of `c`
char anotherC = *anotherCptr; // `anotherC` is now 5.

*cptr = 8; // Now `c` is `8`

char c2 = 12;
cptr = &c2;
*cptr = 16; // `c2` is now `16` but `c` has not changed again.
**cptrptr = 2; // `c2` is now 2. Why? As `cptrptr` still points to `cptr`,
// the fact that `cptr` points elsewhere now won't influence `cptrptr`.

char * aString = calloc(sizeof(char), 100); // 100 characters in memory
char ** aStringPtr = &aString;
char * anotherString = *aStringPtr; // `anotherString == aString`
// They both point to the same piece of memory now. When changing
// `anotherString[0]`, `aString[0]` changes as well.

另请注意,指针和数组在 C 中通常可以互换,这是因为 C 中的数组没有元数据,因此内存中只有一个值的数组实际上只是一个值:

char ca = 5;
char cb[1] = { 5 }; // The memory of `ca` and the one `cb` points to look identical!
char * captr = &ca;
char * cbptr = cb; // No `&`, as an array reference is a pointer.
char c = *captr; // Works.
c = captr[0]; // Works, too, albeit `ca` is no array!
c = cbptr[0]; // Works.
c = *cbptr;   // Works, too, albeit `cb` is an array!

其实C中的数组订阅就是语法糖。而不是 x[n]你总是可以写 *(x+n)它会产生相同的效果。注意 +在指针上不会将指针移动 n字节,它将指针移动 n 倍 x 的大小(<addr> + (n * sizeof(x)))。所以一个char *指针可以是指向单个 char 的指针或者它可以是指向 char 值数组 ( char[] ) 的指针,这在 C 语言中是无法分辨的。

关于c - 是否可以在 C 中将 char** 转换为 char*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49950857/

相关文章:

c - 如何修复我的代码以清除链接结构中的节点

c - 如何在c中从char*访问char[]?

c - 按位与 C 中的 HEX 和 CHAR

c - 如何通过管道传输到 ffmpeg RGB 值 10?

c - PaStreamCallbackTimeInfo成员全部为0

C 文件读取不会读取字符串中的数字数组

c# - 如何从 C# 中的类对象数组访问属性?

Javascript 对象数组检查键

java - 动态创建数组,其中所有子项都位于线性布局内

c - 为什么这个数组不报错?