如何连接一个字符数组情况下,[0]和[1]在C一个字符指针? [英] How to concatenate a char array case [0] and [1] to a char pointer in C?
问题描述
我想连接两个字符7和9,形成这个字符串79。
首先,我初始化的变量。 (限制:我只用CHAR类型,我不能再拍CHARAC一样的变量。)
的char * CHARAC =(字符*)malloc的(的sizeof(char)的* 200);
字符*合并=(字符*)malloc的(的sizeof(char)的* 200);
然后我给的值CHARAC [0]和CHARAC [1]。
CHARAC [0] =7;
CHARAC [1] ='9';
的printf(CHARAC [0]%C \\ N,CHARAC [0]);
的printf(CHARAC [1]:%C \\ N,CHARAC [1]);
我想连接CHARAC [0]和CHARAC [1]
的strcpy(组合,(字符*)CHARAC [0]);
strcat的(组合,(字符*)CHARAC [1]);
的printf(组合:%S \\ n,合并);
免费(组合);
不过,code以上不起作用。我得到了错误信息:警告:转换为不同大小的整指针
阅读所有的意见和建议后,我得到的输出结果,我想。
下面是最后的code:
的char * CHARAC =(字符*)malloc的(的sizeof(char)的* 200);
字符*合并=(字符*)malloc的(的sizeof(char)的* 200); CHARAC [0] =7;
CHARAC [1] ='9';
CHARAC [2] ='\\ 0';
的printf(CHARAC [0]%C \\ N,CHARAC [0]);
的printf(CHARAC [1]:%C \\ N,CHARAC [1]);
的printf(CHARAC [2]:%C \\ N,CHARAC [2]); 的strcpy(相结合,与放大器; CHARAC [0]);
的strcat(相结合,与放大器; CHARAC [1]);
的strcpy(相结合,与放大器; CHARAC [0]);
的strcat(相结合,与放大器; CHARAC [2]);
的printf(组合:%S \\ n,合并);
免费(组合);
他们已经串联原样。这是因为它们是对彼此相邻的存储器。您已声明他们在同一阵列中,并在相邻的索引。
+ --------------- +
| 7| 9|
+ --------------- +
[0] [1]
这是你的存储阵列。七( CHARAC [0]
)是旁边九( CHARAC [1]
)。
正如评论所说,只需空
终止阵列来完成的连接。
CHARAC [2] ='\\ 0';
该阵列现在的printf()
友好的,因为它是空终止。
I want to concatenate two characters '7' and '9' to form this string "79".
First, I initialized the variables. (Restriction: I have to use char types only and I must not make another charac alike variable.)
char *charac = (char *) malloc(sizeof(char) * 200);
char *combined = (char *) malloc(sizeof(char) * 200);
Then I gave the values for charac[0] and charac[1].
charac[0] = '7';
charac[1] = '9';
printf("charac[0] : %c\n", charac[0] );
printf("charac[1] : %c\n", charac[1] );
I want to concatenate charac[0] and charac[1]
strcpy(combined, (char*)charac[0]);
strcat(combined, (char*)charac[1]);
printf("combined : %s\n", combined);
free(combined);
But the code above doesn't work. I got the error message: "warning: cast to pointer from integer of different size".
After reading all of your comments and suggestions, I obtain the output result I want.
Here is the final code:
char *charac = (char *) malloc(sizeof(char) * 200);
char *combined = (char *) malloc(sizeof(char) * 200);
charac[0] = '7';
charac[1] = '9';
charac[2] = '\0';
printf("charac[0] : %c\n", charac[0] );
printf("charac[1] : %c\n", charac[1] );
printf("charac[2] : %c\n", charac[2] );
strcpy(combined, & charac[0]);
strcat(combined, & charac[1]);
strcpy(combined, & charac[0]);
strcat(combined, & charac[2]);
printf("combined : %s\n", combined);
free(combined);
They are already concatenated as is. This is because they are right next to each other in memory. You have declared them in the same array, and at adjacent indexes.
+---------------+
| '7' | '9' |
+---------------+
[0] [1]
This is your array in memory. The seven (charac[0]
) is right next to the nine (charac[1]
).
As suggested in the comments, simply null
terminate the array to complete the concatenation.
charac[2] = '\0';
The array is now printf()
friendly as it is null terminated.
这篇关于如何连接一个字符数组情况下,[0]和[1]在C一个字符指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!