2

I am trying to get substrings from a string (a=ATCG) and then store it in an array but I am getting a warning: assignment makes integer from pointer without a cast [enabled by default] dna[i]=dna1;

I want the output array to print: ATCG TCG CG G

I am new to C, how do I fix this?

Here's my program

int main()
{
    char a[]="ATCG", dna[20], *dna1  ;

    dna1 = (char *)malloc(2000);
    int i, len_of_seg_2;
    for(i = 0; i < strlen(a); i++)
    {
        len_of_seg_2=strlen(a)-i+1;  
        strncpy(dna1, a+i, len_of_seg_2); 
        dna1[len_of_seg_2-1]='\0';

        dna[i]=dna1;
        printf("sequence:%s\n",dna1);
    }
    for(i=0;i<5;i++){
        printf("\nseq:%s\n",dna[i]);
    }
    return 0;   
}
3
  • 1
    dna1 is char *, dna[i] is char. The error is exactly what it says. You are assigning a pointer to an integer value. Maybe you meant dna[i] = *dna1? Also, remember to free(dna1) for good practice. Commented Apr 29, 2016 at 2:39
  • What are you trying to do in the statement dna[i]=dna1;? Are you trying to copy a single character, or a string. If you are trying to copy a string, then why is the destination (dna[i]) a character? Commented Apr 29, 2016 at 2:44
  • I am trying to copy the entire string. In the statement dna [i]=dna1; I am trying to store dna1 as element number i in the dna array. Thanks Commented Apr 29, 2016 at 14:23

2 Answers 2

2

You were missing a '*' infront of dna[20].

int main()
{
    char a[]="ATCG";
    char *dna[20];  // <-- this was char dna[20]
    char *dna1;

    dna1 = (char *)malloc(2000);
    int i, len_of_seg_2;
    for(i = 0; i < strlen(a); i++)
    {
        len_of_seg_2=strlen(a)-i+1;  
        strncpy(dna1, a+i, len_of_seg_2); 
        dna1[len_of_seg_2-1]='\0';

        dna[i]=dna1;  // <-- this was the crash
        printf("sequence:%s\n",dna1);
    }
    for(i=0;i<5;i++){
        printf("\nseq:%s\n",dna[i]);
    }
    return 0;   
}

Live demo: http://ideone.com/ZhO3SZ

Sign up to request clarification or add additional context in comments.

1 Comment

It prints from the first for loop. However I am trying to print it out from the second for loop so that I can access the stored subsequences elsewhere in the code. From the second for loop it only prints the last subsequence, G. Thanks for your comment.
0

the type of dna1 is char *, and the type of dna[i] is char. The error is exactly what it says. You are assigning a pointer (dna1) to an integer value (dna[i]). You can't do that - actually, you can and you did, which is why you saw this warning - without explictly telling the compiler that you want to do it (dna[i] = (char)dna1; in this case). Maybe you meant dna[i] = *dna1?. I don't know what you want to do.

Also, remember to free(dna1) for good practice.

Comments

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.