连接 C 字符串
Concatenating C strings
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main(void){
char pos[4][4];
char loc[4];
int i;
for(i=0;i<4;i++){
pos[i][0]=4;
pos[i][1]=4;
pos[i][2]=4;
pos[i][3]=4;
} loc[i]=" ";
strcat(loc[i],pos[i]);
}
我正在尝试将 pos[i] 的所有内容保存在一个串联的
细绳。我不知道该怎么做。如果有任何提示,我将不胜感激。
改编代码:
#include <stdio.h>
#include <string.h>
int main(void) {
char pos[4][4];
char *loc;
int i;
for (i = 0; i < 4; i++) {
pos[i][0] = 'a' + i;
pos[i][1] = 'a' + i;
pos[i][2] = 'a' + i;
pos[i][3] = 'a' + i;
};
loc = (char *) pos;
for (i = 0; i != sizeof(pos); i++) {
fputc(loc[i], stdout);
}
fputc('\n', stdout);
/* output: aaaabbbbccccdddd */
return 0;
}
pos
已经是您要查找的连接。
pos
是连续字符的内存块的地址
并且可以这样声明和初始化:
char pos[4][4] = {
{ 'a', 'a', 'a', 'a' }
, { 'b', 'b', 'b', 'b' }
, { 'c', 'c', 'c', 'c' }
, { 'd', 'd', 'd', 'd' }
};
或这种方式(编译器可能会发出警告):
char pos[4][4] = {
'a', 'a', 'a', 'a'
, 'b', 'b', 'b', 'b'
, 'c', 'c', 'c', 'c'
, 'd', 'd', 'd', 'd'
};
这两个维度只是pos
的一个视图,它可以被展平:(char *) pos
.
如果您需要平面副本:
char cpy[sizeof(pos)];
memcpy(cpy, pos, sizeof(pos));
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main(void){
char pos[4][4];
char loc[4];
int i;
for(i=0;i<4;i++){
pos[i][0]=4;
pos[i][1]=4;
pos[i][2]=4;
pos[i][3]=4;
} loc[i]=" ";
strcat(loc[i],pos[i]);
}
我正在尝试将 pos[i] 的所有内容保存在一个串联的 细绳。我不知道该怎么做。如果有任何提示,我将不胜感激。
改编代码:
#include <stdio.h>
#include <string.h>
int main(void) {
char pos[4][4];
char *loc;
int i;
for (i = 0; i < 4; i++) {
pos[i][0] = 'a' + i;
pos[i][1] = 'a' + i;
pos[i][2] = 'a' + i;
pos[i][3] = 'a' + i;
};
loc = (char *) pos;
for (i = 0; i != sizeof(pos); i++) {
fputc(loc[i], stdout);
}
fputc('\n', stdout);
/* output: aaaabbbbccccdddd */
return 0;
}
pos
已经是您要查找的连接。
pos
是连续字符的内存块的地址
并且可以这样声明和初始化:
char pos[4][4] = {
{ 'a', 'a', 'a', 'a' }
, { 'b', 'b', 'b', 'b' }
, { 'c', 'c', 'c', 'c' }
, { 'd', 'd', 'd', 'd' }
};
或这种方式(编译器可能会发出警告):
char pos[4][4] = {
'a', 'a', 'a', 'a'
, 'b', 'b', 'b', 'b'
, 'c', 'c', 'c', 'c'
, 'd', 'd', 'd', 'd'
};
这两个维度只是pos
的一个视图,它可以被展平:(char *) pos
.
如果您需要平面副本:
char cpy[sizeof(pos)];
memcpy(cpy, pos, sizeof(pos));