Reputation: 21
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;
}
Upvotes: 2
Views: 927
Reputation: 24249
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
Upvotes: 2
Reputation: 526
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.
Upvotes: 0