English 中文(简体)
果园的果园或果园。
原标题:Cast char to char array or char array to char?

So let s say I have a char and I want to strcat() it to a char array in a single line of code. For [a non-practical] example:

strcat("ljsdflusdfg",getchar());

或者,我想要扭转这种局面,无论数据类型如何,什么是混编或打字的恰当功能? 或者说,有些yn子一米失踪......

这里就是一个例子。 它只汇编了罚款,但坠毁。

char* input(){
 char* inp="";
 while(1){
  char c=getchar();
  if(c){
   if(c== 
 ||c==EOF){
    break;
   }else{
    strcat(inp,(char*)c);
   }
  }
 }
 return inp;
}
最佳回答

<代码>strcat将其论点视为一种绝缘体。 将“条码”改为“条码”(<>条码/代码>)同样危险,我无法想象任何理由会有所助益(不意味着你重新站出来审判——每个人都在学习时犯了sil错。 因此,我们在此重申。

The reason is that it would interpret the single byte char, plus the extra sizeof(char*) - sizeof(char) (usually 3) bytes surrounding that char, as a pointer, which would point to... anywhere. You have no way of knowing where it points, since 3 of those bytes are beyond your control, and thus no way of knowing whether it points to valid data.

您可以采取第二种做法:

strcat(inp, &c);

This time, you d be doing better, since &c is an expression of type char * and no casts are required. But again, strcat assumes it s argument is a nul-terminated string, and since you have no way of guaranteeing a nul byte after your char data, this won t do.

最好的办法是:

size_t len = strlen(inp); // this may already be calculated somewhere else
...
inp[len++] = c; // add c as the last character, and adjust our len
inp[len] =   ; // add a new nul terminator to our string

最新资料:

实际上,我 lie。 rel=“nofollow”>fgets,似乎正在做更多或更少的工作。 我原谅这一点,但如果这是家务劳动,教授可能不想使用<条码>fgets。 以便你能够了解如何人工操作。 然而,如果这种单质的家务劳动,fgets确实是你重新寻找的东西。 (实际上,第三种办法正好在改写如下:<条码>fgets或<条码>fgets 类似功能。)

I would also add some other commentary on your input function:

  1. char* inp = ""; will point to read-only data. A flaw in the C standard (for backwards compatability) allows string literals to be assigned to char* types instead of const char * types as it (IMHO) should be.

    There are several ways to approach this, but the best is dynamic allocation. Use the malloc function to reserve some data, keep track of how much you ve used in your function, and use the realloc function if you end up needing more room to store it. If you need help with that, I m sure you ll be back here (hopefully not too soon) with another question. :)
  2. getchar() returns an int, because EOF is defined to be outside the normal range of a char. In order to distinguish between any char and EOF, it s best to make c an int. Otherwise a perfectly valid character may signal EOF. Be sure to cast c down to a char when you add it to the string, though.
问题回答
strcat(inp,(char*)c);

这正试图将历史记忆的内容(作为记忆点)混为一谈,直到它找到零。

更好的办法是,在你认为合理的最大规模上制造空洞,以零填充,然后在目前职位上插入c。

如果您希望设计一个单一的<条码>。 在座标上,您可使用<代码>strncat(>,并注明星号为1。 但指出:

  • The destination buffer must have sufficient space; and
  • In C89, you must have an actual char object to take the address of (so you can t directly concatenate the result of getchar().

例如:

char str[5] = "foo";
char c;
int ch;

if ((ch = getchar()) != EOF) {
    c = ch;
    strncat(str, &c, 1);
}

在C99中,你可以使用poundlich/em>,第二个限制不适用:

char str[5] = "foo";
int ch;

if ((ch = getchar()) != EOF) {
    strncat(str, &(char){ ch }, 1);
}

This won t work because the *inp char pointer doesn t have any memory backing it up. When you declared it to be char *inp = "";, you only gave it the address of an empty string. If you had given the following declaration char *inp = "abcdefg", then you could have written 7 chars to it before you overwrote the and got into trouble.

你们需要积极增加背后记忆的大小,因为你们具有更多的特性。 你们还将知道何时释放记忆,以便你不会泄露记忆。

此外,你们的剧团需要 have;在此之前。

首先,你正在把一个点回到分配的阵列。 这是没有定义的bah。 第二,你试图将果园写成未分配的记忆。 第三,<代码>inp的类型是const char* notchar*

你们想要的是:

char* input(char *dest) {
  strcpy(dest,"");
  while(1){
  char c=getchar();
  if(c){
   if(c== 
 ||c==EOF){
    break;
   }else{
    strcat(inp,&c);
   }
  }
 }
 return dest;
}

Your code crashes because strcat expects null-terminated strings for both its parameters.

如果你想 con一个果园,你需要首先从中找到一个被无名的str子。

char c[2] = {0,0};
c[0] = getchar();
strcat(inp, c);

don t use strcat,除非您能够保证,所提供的投入为这种额外性质分配了空间。 更适合你,其中还包括第一个参数(最底)的空间面积参数。

我建议阅读,首先见

一种简单的办法是使用print:

char newString[50+1];
snprintf(newString, sizeof(newString), "%s%c", "ljsdflusdfg", getchar());

这是一种简单的解决办法,但确实要求你大致知道,这种扼杀将多么大。

Notes: I use the +1 to remind myself that there needs to be room for the null termination, and snprintf is better than sprintf as it is given the size of the buffer to prevent buffer overrun.

Cast char to char array?
It s just like saying you want to cast a char into string.
You just can t do that directly.
Instead of char, place it in an array (myChar[]).
Afterwhich, strcat it with your original char array.





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...