没有演员的指针?

时间:2013-06-20 09:01:07

标签: c

我刚刚开始使用C,所以这个问题可能很愚蠢。     有关为什么我一直收到此编辑警告的想法?

问题:编写一个函数escape(s,t),将newline和tab等字符转换为 可见的转义序列,如\n\t,因为它将字符串t复制到s

3-2.c:37:11: warning: assignment makes integer from pointer without a cast [enabled by default]
3-2.c:38:9: warning: assignment makes integer from pointer without a cast [enabled by default]
3-2.c:42:11: warning: assignment makes integer from pointer without a cast [enabled by default]
3-2.c:43:9: warning: assignment makes integer from pointer without a cast [enabled by default]

这是代码:

int get_line (char input[], int max_size);
void escape(char s[], char t[]);

main () {
    int length, l, i;
char line[MAX], t[MAX];

while ((length = get_line (line, MAX))  > 0)
    escape (line, t);       
    printf ("%s", t);


} 


int get_line (char input[], int max_size) {
    int i, c;
for (i = 0; i < max_size-1 && (c = getchar()) != EOF && c != '\n'; ++i)
    input[i] = c;

if (c == '\n') {
    input[i] = c;
    ++i;
}
input[i] = '\0';
return i;
}

void escape(char s[], char t[]) {
int i;
for (i= 0; s[i] != '\0'; ++i) {
    switch(s[i]) {

    case '\t' :
                    //This is where i get the warning.
        t[i++] = "\\";
        t[i] = "t";
        break;
    case '\n' :
        t[i++] = "\\";
        t[i] = "n";

    default :
        t[i] = s[i];
        break;

    }
}
}

3 个答案:

答案 0 :(得分:1)

t [i]给你char元素,     t [i] =“t”,t [i ++] =“\”将字符串的地址赋给char元素

你需要用单引号''分配。

t [i] ='t';或者t [i] ='\';

答案 1 :(得分:0)

t是char数组意味着t [i]会给你一个char元素但是在行

t[i++] = "\\";t[i] = "t";

你在这些元素中推送字符串。字符串被称为字符数组而不是单个字符。用" "写的东西被称为字符串。通过上面提到的赋值,你传递字符串的地址(指针)

答案 2 :(得分:0)

"\\""t"字面值是一个字符串文字,用于计算其在只读内存中的地址。

您可能想要的是'\\''t',它发出的确切字符代码(ASCII中为0x5C / 92)