将字符串传递给c中的指针

发布于 2025-01-11 20:13:58 字数 449 浏览 7 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒绊 2025-01-18 20:13:58

您好,欢迎来到 C。

您的陈述:

*result = "HELLO";

与尝试执行以下操作相同:

result[0] = "HELLO"

尝试将单个字符设置为字符串,但您不能这样做。

您需要逐个字符地复制字符串,

幸运的是,您已经在 中包含了一个名为 strcpy 的函数,

strcpy(result,"HELLO")

只要这个函数就可以工作因为要复制的字符串少于您在 main() 函数中定义的 63 个字符。

char result[64];

您可能还应该将字符串的长度发送到测试函数并使用 strncpy

strncpy(result,"HELLO",length); // safe copy

,然后用 '\0' 终止字符串,

result[length-1] = 0;

您的 printf 不需要取消引用字符串指针。所以简单地 printf("%s",result);很好。

综上所述:

void test(char* result,uint32_t len) {
    strncpy(result,"HELLO",len); // safe copy (however "HELLO" will work for 64 length string fine)
    result[len-1] = 0; // terminate the string
}

#define MY_STRING_LENGTH 64

int main() {
    char result[MY_STRING_LENGTH ];
    test(result,MY_STRING_LENGTH);
    printf("%s",result); // remove *

}

Hello and welcome to C.

Your statement:

*result = "HELLO";

is the same as attempting to do the following:

result[0] = "HELLO"

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> called strcpy

strcpy(result,"HELLO")

This will work as long as your string to copy is fewer than 63 characters as you have defined in your main() function.

char result[64];

you should probably also send the length of the string to the test function and use strncpy

strncpy(result,"HELLO",length); // safe copy

and then terminate the string with '\0'

result[length-1] = 0;

your printf doesn't need to dereference the string pointer. So simply printf("%s",result); is fine.

so in summary:

void test(char* result,uint32_t len) {
    strncpy(result,"HELLO",len); // safe copy (however "HELLO" will work for 64 length string fine)
    result[len-1] = 0; // terminate the string
}

#define MY_STRING_LENGTH 64

int main() {
    char result[MY_STRING_LENGTH ];
    test(result,MY_STRING_LENGTH);
    printf("%s",result); // remove *

}
洛阳烟雨空心柳 2025-01-18 20:13:58

您在 main 中声明了一个数组

char result[64];

,传递给函数,它被转换为指向数组第一个元素的 char * 类型的右值。该函数处理此指针的副本。更改指针的此副本不会影响原始数组。

在函数内,表达式*result 的类型为char。所以这个任务

*result = "HELLO";

没有任何意义。

在此调用中,

printf("%s", *result);

再次使用了 char *result 类型的错误表达式。

您需要的是使用标准字符串函数strcpy

例如

#include <stdio.h>
#include <string.h>

void test(char* result) {
    strcpy( result, "HELLO" );
}

int main( void ) {
    char result[64];
    test(result);
    puts( result );

}

You declared an array in main

char result[64];

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 type char. So this assignment

*result = "HELLO";

does not make a sense.

In this call

printf("%s", *result);

there is again used an incorrect expression of the type char *result.

What you need is to use standard string function strcpy.

For example

#include <stdio.h>
#include <string.h>

void test(char* result) {
    strcpy( result, "HELLO" );
}

int main( void ) {
    char result[64];
    test(result);
    puts( result );

}
忆依然 2025-01-18 20:13:58

问题:

当您将字符存储在 char 变量中时,它会将字符的 ASCII 码放入内存中。

char c='a';char c=97;相同

您可以使用代码验证这一点:

char c='a';
printf("%d",c);

所以这是一种方法:

void test(char* result) {
    *result++ = 'H';
    *result++ = 'E';
    *result++ = 'L';
    *result++ = 'L';
    *result = 'O';
}

int main() {
    char result[64];
    test(result);
    printf("%s", result);
}

但它是多余的,因为 中有一个名为 strcpy 的函数。

#include <stdio.h>
#include <string.h>

void test(char* result) {
    strcpy( resul, "HELLO" );
}

int main() {
    char result[64];
    test(result);
    puts( result );

}

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:

char c='a';
printf("%d",c);

So here is one way:

void test(char* result) {
    *result++ = 'H';
    *result++ = 'E';
    *result++ = 'L';
    *result++ = 'L';
    *result = 'O';
}

int main() {
    char result[64];
    test(result);
    printf("%s", result);
}

But it is redundant because there is a function called strcpy in <string.h>.

#include <stdio.h>
#include <string.h>

void test(char* result) {
    strcpy( resul, "HELLO" );
}

int main() {
    char result[64];
    test(result);
    puts( result );

}
壹場煙雨 2025-01-18 20:13:58

声明变量“result”后,删除它的“*”,并在代码中使用函数“strcpy()”。

Remove the '*' of the variable "result" after you've declared it and use the function "strcpy()" in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文