简体   繁体   English

在 C 中将 char 转换为 char*

[英]cast a char into a char* in C

I have a char array s[11]="0123456789";我有一个字符数组s[11]="0123456789"; and I want to be able to take each digit s[i] (using a for loop) and cast it somehow to a char* (I need a char* specifically because I need to use strncat on some other string)并且我希望能够获取每个数字s[i] (使用 for 循环)并以某种方式将其转换为char* (我需要一个 char*,特别是因为我需要在其他字符串上使用 strncat)

I've been trying to do this for the past 4 hours and I couldn't get anything done.在过去的 4 个小时里,我一直在尝试这样做,但我什么也做不了。

Unless you're willing to temporarily modify s , or copy the character somewhere else... You're going to have a hard time.除非您愿意暂时修改s ,或将角色复制到其他地方……否则您会遇到困难。

Modify s temporarily.暂时修改s

int main() {
  char s[11] = "0123456789";
  for (int i = 0; i < strlen(s); i++) {
    // Modify s in place.
    const char tmp = s[i+1];
    s[i+1] = '\0';
    char *substr = &s[i];
    // Do something with substr.
    printf("%s\n", substr);
    // Fix s
    s[i+1] = tmp;
  }
}

Or copy s[i] to a new string.或者将s[i]复制到一个新字符串。

int main() {
  char s[11] = "0123456789";
  for (int i = 0; i < strlen(s); i++) {
    // Create a temporary string
    char substr[2];
    substr[0] = s[i];
    substr[1] = '\0';
    // Do something with substr.
    printf("%s\n", substr);
  }
}

Or maybe just don't use strncat?或者也许只是不使用strncat?

Assuming you're actually using strncat or similar... Those functions are pretty trivial, especially if you are appending only a single character.假设您实际上正在使用strncat或类似的...这些功能非常简单,尤其是当您仅附加一个字符时。 You might just create your own version.您可能只是创建自己的版本。

void append_character(char *buffer, int buffer_size, char new_character) {
  int length = strlen(buffer);
  if (length + 2 < buffer_size) {
    buffer[length] = new_character;
    buffer[length+1]  = '\0';
  } else {
    // No space for an additional character, drop it like strncat would.
  }
}

Or you could do it as a simple wrapper around strncat:或者你可以将它作为一个简单的 strncat 包装器来完成:

void append_character(char *buffer, int buffer_size, char new_character) {
  char new_string[2] = { new_character, '\0' };
  strncat(buffer, buffer_size, new_string);
}

What you are requesting does not make any sense.你的要求没有任何意义。 A char is a small number.一个字符是一个小数字。 A pointer is the address of some memory.指针是某个内存的地址。 Converting a char to a pointer won't give you a valid pointer, but a pointer that will crash as soon as it is used.将字符转换为指针不会给你一个有效的指针,而是一个一旦使用就会崩溃的指针。

For strncat, you need an array of characters containing a C string.对于 strncat,您需要一个包含 C 字符串的字符数组。 You could create a string by writing char array[2];您可以通过编写 char array[2] 来创建一个字符串; (now you have an array with space for two characters), then array[0] = whateverchar; (现在你有一个包含两个字符空间的数组),然后 array[0] = whatchar; array[1] = 0;数组[1] = 0; and now you have a C string with space for exactly one char and one trailing zero byte.现在你有一个 C 字符串,其中有一个字符和一个尾随零字节的空间。

Yet another idea:还有一个想法:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char src[11]="0123456789";
    char dest[50]="abc";

    char* p = src;
    int i = 0;
    int len = strlen(src);
    
    for (i = 0; i < len; i++)
    {
        strncat(dest,p,1);
        p++;
    }
    
    printf("src:  %s\n", src);
    printf("dest: %s\n", dest);

    return 0;
}

Compiled with gcc under Ubuntu:在Ubuntu下用gcc编译:

$ gcc hello_str.c -o hello_str

Output:输出:

$ ./hello_str 
src:  0123456789
dest: abc0123456789

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM