printf 不适用于特定上下文,为什么?
printf doesn't work on specific context, why?
我需要测试一些东西并编写了这段代码(如下所示)。我不明白为什么第一个印刷品有效而第二个印刷品无效。这个程序的输出只是
this prints
但应该是
this prints
this doesn't print i: 1
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(char *str) {
char *z[1];
strcpy(*z, "z");
int a;
a = strcmp(str, *z);
return a;
}
int main() {
int i;
char *name[1];
printf("this prints\n");
strcpy(*name, "y");
i = cmp(*name);
printf("this doesn't print i:%d", i);
return 0;
}
char *z[1]; // this is an array of pointer
strcpy(*z, "z"); // you have to allocate at least 2 bytes for *z
// and
char *name[1];
strcpy(*name, "y"); // you have to allocate at least 2 bytes for *name also
您没有为数组 z
和 name
中的指针分配。
您的 cmp
函数看起来很奇怪。如果你想将字符串与 "z"
进行比较,你可以这样做:
int cmp(char *str){
return strcmp(str, "z");
}
您不需要使用char *name[1]
,只需char *name = malloc(SIZE+1);
或char name[SIZE+1]
(SIZE
是您要比较的字符串的长度)就足够了。
char *z[1];
和 char *name[]
name
和 z
都不是 char
的数组。它们都是指向 char
的一个指针的数组。
两个指针 name[1]
和 z[1]
都指向没有有效的内存,因此取消引用它并尝试使用 strcpy(*name, "y");
和将字符串存储到未定义的内存中strcpy(*z, "z");
并调用 undefined behavior。 - 你需要的是 char
的数组,用于 name
和 z
.
要存储字符串,您需要一个元素来存储字符串终止空字符。
如果您只想让字符串包含一个字符,请使用 char z[2]
和 name[2]
。
顺便说一句,您的代码处理起来有点复杂。如果只想存储和比较单个字符,则不要使用字符串。可以简化为:
#include <stdio.h>
int main (void) {
char name = 'y';
printf("In name is the character: '%c'\n", name);
printf("Is the character 'z' in 'name'? %d", name == 'z');
return 0;
}
输出:
In name is the character: 'y'
Is the character 'z' in 'name'? 0
其中 0
表示 false
,1
表示 true
。
旁注:
您在任何时候都不需要 #include <stdlib.h>
。
我需要测试一些东西并编写了这段代码(如下所示)。我不明白为什么第一个印刷品有效而第二个印刷品无效。这个程序的输出只是
this prints
但应该是
this prints this doesn't print i: 1
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(char *str) {
char *z[1];
strcpy(*z, "z");
int a;
a = strcmp(str, *z);
return a;
}
int main() {
int i;
char *name[1];
printf("this prints\n");
strcpy(*name, "y");
i = cmp(*name);
printf("this doesn't print i:%d", i);
return 0;
}
char *z[1]; // this is an array of pointer
strcpy(*z, "z"); // you have to allocate at least 2 bytes for *z
// and
char *name[1];
strcpy(*name, "y"); // you have to allocate at least 2 bytes for *name also
您没有为数组 z
和 name
中的指针分配。
您的 cmp
函数看起来很奇怪。如果你想将字符串与 "z"
进行比较,你可以这样做:
int cmp(char *str){
return strcmp(str, "z");
}
您不需要使用char *name[1]
,只需char *name = malloc(SIZE+1);
或char name[SIZE+1]
(SIZE
是您要比较的字符串的长度)就足够了。
char *z[1];
和 char *name[]
name
和z
都不是char
的数组。它们都是指向char
的一个指针的数组。两个指针
name[1]
和z[1]
都指向没有有效的内存,因此取消引用它并尝试使用strcpy(*name, "y");
和将字符串存储到未定义的内存中strcpy(*z, "z");
并调用 undefined behavior。 - 你需要的是char
的数组,用于name
和z
.要存储字符串,您需要一个元素来存储字符串终止空字符。
如果您只想让字符串包含一个字符,请使用 char z[2]
和 name[2]
。
顺便说一句,您的代码处理起来有点复杂。如果只想存储和比较单个字符,则不要使用字符串。可以简化为:
#include <stdio.h>
int main (void) {
char name = 'y';
printf("In name is the character: '%c'\n", name);
printf("Is the character 'z' in 'name'? %d", name == 'z');
return 0;
}
输出:
In name is the character: 'y'
Is the character 'z' in 'name'? 0
其中 0
表示 false
,1
表示 true
。
旁注:
您在任何时候都不需要 #include <stdlib.h>
。