将数组返回到C中的函数的问题

发布于 2025-01-23 18:37:31 字数 593 浏览 0 评论 0原文

我的问题是我无法将处理的字符串返回到该功能。该函数的作业是它必须接受字符串,更改字母并将处理的字符串返回到该功能。


char *dna_strand(const char *dna)
{
    int a;
   for (a = 1; *(dna+a) != '\0' ; a++) {
}
 char array[a];
    for (int i = 0; i < a; ++i) {
        if ('A' == *(dna + i)) {
            array[i] = 'T';
        } else if ('T' == *(dna + i)){ 
            array[i] = 'A';
        } else{
            array[i] = *(dna + i);
        }
    }
      return array;
}

错误 : [1]: https://i.sstatic.net/uwvch.png

My problem is that I can't return the processed string to the function. The function's job is that it must accept a string, change its letters, and return the processed string to the function.


char *dna_strand(const char *dna)
{
    int a;
   for (a = 1; *(dna+a) != '\0' ; a++) {
}
 char array[a];
    for (int i = 0; i < a; ++i) {
        if ('A' == *(dna + i)) {
            array[i] = 'T';
        } else if ('T' == *(dna + i)){ 
            array[i] = 'A';
        } else{
            array[i] = *(dna + i);
        }
    }
      return array;
}

Error :
[1]: https://i.sstatic.net/uwvCh.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

墟烟 2025-01-30 18:37:31

本地变量char数组[a];不在呼叫者的范围内,因此您不能使用它来返回值。而是用malloc()或此处分配内存 strdup():

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

char *dna_strand(const char *dna) {
    char *array = strdup(dna);
    if (!array) return array;
    for (int i = 0; array[i]; i++) {
        if (dna[i] == 'A') array[i] = 'T';
        else if (dna[i] == 'T') array[i] = 'A';
    }
    return array;
}

int main() {
    char *s = dna_strand("ATTCG");
    printf("%s\n", s);
    free(s);
    return 0;
}

如果您想幻想使用strpbrk()查找我们需要映射的下一个字母,而不是一次踩一封信:

char *dna_strand(const char *dna) {
    char *array = strdup(dna);
    for (char *p = array; p; p = strpbrk(p, "AT")) {
        if (*p == 'A') *p++ = 'T';
        else *p++ = 'A';
    }
    return array;
}

The local variable char array[a]; is out of scope for caller so you cannot use that to return a value. Instead allocate memory on the heap with malloc() or as herestrdup():

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

char *dna_strand(const char *dna) {
    char *array = strdup(dna);
    if (!array) return array;
    for (int i = 0; array[i]; i++) {
        if (dna[i] == 'A') array[i] = 'T';
        else if (dna[i] == 'T') array[i] = 'A';
    }
    return array;
}

int main() {
    char *s = dna_strand("ATTCG");
    printf("%s\n", s);
    free(s);
    return 0;
}

If you want to be fancy use strpbrk() to find the next letter we need to map instead of stepping one letter at a time:

char *dna_strand(const char *dna) {
    char *array = strdup(dna);
    for (char *p = array; p; p = strpbrk(p, "AT")) {
        if (*p == 'A') *p++ = 'T';
        else *p++ = 'A';
    }
    return array;
}
温柔戏命师 2025-01-30 18:37:31

您不得将指针返回具有自动存储持续时间的本地数组,因为退出功能后,数组将不会活着,并且返回的指针将无效。

您需要动态分配数组。

请注意此循环

for (a = 1; *(dna+a) != '\0' ; a++) {

当用户将一个空字符串传递给函数时,

可以调用不确定的行为。您至少应该写作

int a = 0;
while ( *(dna+a) != '\0' ) ++a;
++a;

int a = 0;
do ; while ( *( dna + a++ ) != '\0' );

之后写一个字符数组的分配

char *array = malloc( a );

You may not return a pointer to a local array with automatic storage duration because after exiting the function the array will not be alive and the returned pointer will be invalid.

You need to allocate an array dynamically.

Pay attention to that this loop

for (a = 1; *(dna+a) != '\0' ; a++) {

can invoke undefined behavior when the user will pass to the function an empty string.

You should write at least like

int a = 0;
while ( *(dna+a) != '\0' ) ++a;
++a;

or

int a = 0;
do ; while ( *( dna + a++ ) != '\0' );

After that you can allocate dynamically a character array

char *array = malloc( a );
旧伤还要旧人安 2025-01-30 18:37:31

您无法将指针返回到本地变量的地址,因为它将在函数范围中删除时会被破坏。

由于,a不是一个常数值,因此您不能初始化array作为static
因此,唯一的选项是使用 heap-Alocation

a可以像这样初始化:

char *array = calloc(a + 1, sizeof(char));

注意:calloc()是在stdlib.h标题文件中定义的。

You cannot return a pointer to a local variable's address, because it will be destroyed when it will goes out from the function's scope.

Since, a is not a constant value, you cannot initialize array as static.
Hence, the only option is left is using heap-allocation.

a can be initialize like this:

char *array = calloc(a + 1, sizeof(char));

Note: calloc() is defined in stdlib.h header file.

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