将字符串传递给c中的指针
我对 C 相当陌生。我想将函数中的字符串分配给指针,但我不知道为什么它不起作用?
这是初始代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
void test(char* result) {
*result = "HELLO";
}
int main() {
char result[64];
test(result);
printf("%s", *result);
}
这是错误:警告:赋值从指针生成整数而不进行强制转换。由于 * result 应该存储值,而 result 是地址,这不应该可行吗?
I am fairly new in C. I want to assign string in a function to a pointer but I have no idea why it is not working?
This is the initial code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
void test(char* result) {
*result = "HELLO";
}
int main() {
char result[64];
test(result);
printf("%s", *result);
}
This is the error: warning: assignment makes integer from pointer without a cast. Since * result should store value and result is the address, shouldn't this work out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您好,欢迎来到 C。
您的陈述:
与尝试执行以下操作相同:
尝试将单个字符设置为字符串,但您不能这样做。
您需要逐个字符地复制字符串,
幸运的是,您已经在
中包含了一个名为strcpy
的函数,只要这个函数就可以工作因为要复制的字符串少于您在 main() 函数中定义的 63 个字符。
您可能还应该将字符串的长度发送到测试函数并使用 strncpy
,然后用 '\0' 终止字符串,
您的 printf 不需要取消引用字符串指针。所以简单地
printf("%s",result)
;很好。综上所述:
Hello and welcome to C.
Your statement:
is the same as attempting to do the following:
which is attempting to set a single character to a string, and you can't do that.
you will need to copy the string character by character
luckily there is a function for that which you have included already with
<string.h>
calledstrcpy
This will work as long as your string to copy is fewer than 63 characters as you have defined in your main() function.
you should probably also send the length of the string to the test function and use strncpy
and then terminate the string with '\0'
your printf doesn't need to dereference the string pointer. So simply
printf("%s",result)
; is fine.so in summary:
您在 main 中声明了一个数组
,传递给函数,它被转换为指向数组第一个元素的 char * 类型的右值。该函数处理此指针的副本。更改指针的此副本不会影响原始数组。
在函数内,表达式
*result
的类型为char
。所以这个任务没有任何意义。
在此调用中,
再次使用了 char
*result
类型的错误表达式。您需要的是使用标准字符串函数
strcpy
。例如
You declared an array in main
Passed to the function it is converted to rvalue of the type char * that points to the first element of the array. The function deals with a copy of this pointer. Changing this copy of the pointer fors not influence on the original array.
Within the function the expression
*result
has the typechar
. So this assignmentdoes not make a sense.
In this call
there is again used an incorrect expression of the type char
*result
.What you need is to use standard string function
strcpy
.For example
问题:
当您将字符存储在 char 变量中时,它会将字符的 ASCII 码放入内存中。
char c='a';
与char c=97;
相同您可以使用代码验证这一点:
所以这是一种方法:
但它是多余的,因为
中有一个名为strcpy
的函数。Problem:
When you store a character in a char varible,it puts the ASCII of the character in the memory.
char c='a';
is the same aschar c=97;
You can verify this by using the code:
So here is one way:
But it is redundant because there is a function called
strcpy
in<string.h>
.声明变量“result”后,删除它的“*”,并在代码中使用函数“strcpy()”。
Remove the '*' of the variable "result" after you've declared it and use the function "strcpy()" in your code.