0

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

5
  • 1
    Please post error message as text instead of image. Format the code so it's not misleading. Use strlen() + 1 instead of the first loop. Commented Apr 25, 2022 at 9:20
  • 1
    VLA is an C99 extension and shall be avoided. Better use alloca instead of array[i] or in your case, malloc then return the allocated string, but then you must free it. Commented Apr 25, 2022 at 9:23
  • @IlianZapryanov alloca would not work as op wants to return the value. Commented Apr 25, 2022 at 9:24
  • you are promising a string but an array is not null terminated Commented Apr 25, 2022 at 9:32
  • The function doesn't return an array. It returns a pointer to an array. Then the array is destroyed. Then the function which calls this function tries to use the array. Commented Apr 25, 2022 at 10:14

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Scope is not the applicable concept here. Objects may be used in code outside the scope of their identifiers. The appropriate concepts are storage duration and lifetime; objects may not be used after their lifetimes end.
1

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 );

Comments

0

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.

6 Comments

sizeof(char) is defined as 1
Wording is not accurate: You cannot return a local variable -> You cannot return a pointer to a local variable
@AllanWind: sizeof (char) is indeed 1 by definition although it is not defined to be 1. (The fact that sizeof (char) evaluates to 1 is a logical conclusion of the definitions of sizeof and char, but it is not the explicit definition of sizeof (char).) However, it is not good advice to suggest it not be used, as your comment perhaps implies. If char is explicitly called out where the code relies in it, this helps both to communicate to readers what the code is doing and to make it easier to update for future changes…
… For example, if somebody wishes to migrate character-handling code to wide-character-handling code, the fact the original other explicitly used char in sizeof and other places makes it easier for the migrator to find them and replace them with wchar_t.
@EricPostpischil if you make such a change, you need to port your code. As op used the type the would have to change sizeof(char) to sizeof(new_type). To your point, they should use the variable name instead (*array).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.