Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c字符串不是';t正确添加到字符串并输出□;使用printf时_C_String_C Strings - Fatal编程技术网

c字符串不是';t正确添加到字符串并输出□;使用printf时

c字符串不是';t正确添加到字符串并输出□;使用printf时,c,string,c-strings,C,String,C Strings,我一直在编写一个脚本,其中字符串应该添加到字符串中。 但是我的代码中的printf函数第一次打印□第二次□□和第三次□□□。它应该打印A,Ap,App。 下面是我的代码的简要概述: #include<stdio.h> #include<stdlib.h> int i = 0; char * name[]; char * tok[]; int hello = 0; void append(char* s, char c) { int len = strl

我一直在编写一个脚本,其中字符串应该添加到字符串中。 但是我的代码中的printf函数第一次打印
第二次
□□和第三次
□□□。它应该打印
A
Ap
App
。 下面是我的代码的简要概述:

#include<stdio.h>
#include<stdlib.h>

int i = 0;
char * name[];
char * tok[];
int hello = 0;

void append(char* s, char c) {
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
}

int input(char data[]){
    for(i=0; i<strlen(data); ++i){
        append(tok, data[i]);
        if(hello == 0){
            append(name, tok);
            strcpy(tok, "");
            printf(name);
            printf("\n");
        }
   }
return 0;
}

int main(){
    input("App");
    return 0;
}
#包括
#包括
int i=0;
字符*名称[];
char*tok[];
int hello=0;
无效附加(字符*s,字符c){
int len=strlen(s);
s[len]=c;
s[len+1]='\0';
}
int输入(字符数据[]){
对于(i=0;i
#包括
#包括
#包括
int i=0;
字符名[99];
char-tok[99];
int hello=0;
无效附加(字符*s,字符c){
int len=strlen(s);
s[len]=c;
s[len+1]='\0';
}
int输入(字符*数据){

对于(i=0;i程序具有未定义的行为

这些暂定定义

char * name[];
char * tok[];
其实都相当于

char * name[1] = { NULL };
char * tok[1] = { NULL };
比如说这句话

int len = strlen(s);
调用未定义的行为

或函数的第一个参数

void append(char* s, char c) {
以及提供的参数

append(tok, data[i]);

有不同的类型。参数的类型是
char**
,而参数的类型是
char*

对于我的问题,我有一个简单的答案:

if(hello == 0){
            char t = *tok;
            append(name, t);
            strcpy(tok, "");
            printf(name);
            printf("\n");
        }
因此,
char*
变量
tok
被转换为名为
t
char
,可以通过append函数添加到变量
char*name
。 下面是工作代码的简要概述:

#include<stdio.h>
#include<stdlib.h>

int i = 0;
char * name[];
char * tok[];
int hello = 0;

void append(char* s, char c) {
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
}

int input(char data[]){
    for(i=0; i<strlen(data); ++i){
        append(tok, data[i]);
        if(hello == 0){
            char t = *tok;
            append(name, t);
            strcpy(tok, "");
            printf(name);
            printf("\n");
        }
   }
return 0;
}

int main(){
    input("App");
    return 0;
}
#包括
#包括
int i=0;
字符*名称[];
char*tok[];
int hello=0;
无效附加(字符*s,字符c){
int len=strlen(s);
s[len]=c;
s[len+1]='\0';
}
int输入(字符数据[]){

对于(i=0;它没有与tok或name关联的存储。它们只是指向字符数组的指针。请尝试使tok和name具有实际存储,如:char-tok[16],name[16]。此外,“hello”的函数是什么?@Level42在实际脚本tok中是一个变量。仅在上述情况下(if语句)tok是一个。名称也是可变的,因为输入(“这可能是一个很长的文本”)。所以我不能给他们一个存储空间。所谓“真实脚本”是指你的真实代码吗?如果你不愿意发布确切的代码(一个,不一定是你的确切代码)这就产生了问题,很难帮助你。这里没有人是读心术的人,也没有人会回头看你。如果你不知道在运行之前需要多少内存,那么你需要使用或类似的东西动态分配。如果
name
tok
只是单个字符串,它们的类型应该是
>char*
相反,每个字母后都应重置.tok。此外,结果应“保存”在其他变量中,如上面的示例所示。您所述的目标是“它应打印A、Ap、App”上面的代码可以做到这一点。它应该打印在我上面脚本中的变量名上,ThxDownVote,因为这不是它的工作方式。Vlad已经为您的问题发布了正确的答案。因此,您应该接受他的答案,而不是采纳他的建议并将其用于您接受的答案。@ProXicT更改了它
#include<stdio.h>
#include<stdlib.h>

int i = 0;
char * name[];
char * tok[];
int hello = 0;

void append(char* s, char c) {
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
}

int input(char data[]){
    for(i=0; i<strlen(data); ++i){
        append(tok, data[i]);
        if(hello == 0){
            char t = *tok;
            append(name, t);
            strcpy(tok, "");
            printf(name);
            printf("\n");
        }
   }
return 0;
}

int main(){
    input("App");
    return 0;
}