-2
I made a code in C that when running presents some errors and possibly not a logic error, since when I run it presents some errors, however when I ask a friend to run the code works normally.
The code takes the first letter of each vector, creates and places in another vector doing the same with the following letters, but this answer is appearing.
that’s the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * JuntarVetores(char * v1, char *v2, char* v3);
void main()
{
char A[80],B[80],C[80];
char *D;
printf("Vetor A: ");
scanf("%s",A);
fflush(stdin);
printf("Vetor B: ");
scanf("%s",B);
fflush(stdin);
printf("Vetor C: ");
scanf("%s",C);
D=JuntarVetores(A,B,C);
printf("%s",D);
}
char * JuntarVetores(char * v1, char *v2, char* v3)
{
int i,x1=0,x2=0,x3=0;
char *v4=(char*)malloc((strlen(v1)+strlen(v2)+strlen(v3)) * sizeof(char));
for(i=0;i<strlen(v1)||i<strlen(v2)||i<strlen(v3);i++)
{
if(v1[i]!='\0' && x1==0)
{
*(v4+strlen(v4))=*(v1+i);
// printf("1");
}
else
x1=1;
if(v2[i]!='\0' && x2==0)
{
*(v4+strlen(v4))=*(v2+i);
//printf("2");
}
else
x2=1;
if(v3[i]!='\0' && x3==0)
{
*(v4+strlen(v4))=*(v3+i);
//printf("3");
}
else
x3=1;
}
return(v4);
}
It’s never the compiler’s problem. Or do you think after decades that millions of people using no one has seen a problem in such simple code and you who are starting now have found this problem? Looking at that I saw several problems in the code, but I will leave now and with so many problems for me the question is too wide to answer.
– Maniero
No way I think I’m somebody fuck, I’m just with a problem I can’t solve, I may have wrong the way to write the title, but I don’t understand why I’m such a snob
– KSM
Note that you allocate memory to
v4
but does not put anything in this memory. I believe I want initiallyv4
is a string of zero length, in this case dov4[0] = '\0'
. Its code is quite confusing but I believe that one counter would suffice for v4, which would be incremented until the concatenation of all other arrays and another counter to be used to traverse each of the arrays to be concatenated. No need to use the functionstrlen
, just remember that a string in C ends with the character'\0'
.– anonimo
thank you, I’ll try
– KSM
@Kaiosato you want something more snobbish than to say that the compiler is wrong and you’re right? I tried to show you that attitude doesn’t work, you have every right to ignore it and keep it that way. Some people take this information and learn.
– Maniero
i n understand computation/programming, qnd said q was showing different answers in the same code, my intuition says q has something wrong ( and in case I thought q would be the compiler, since the code was the same, as I said n understand fuck none), in the case of the title you could have said directly to have changed, and as I have already answered, I will take the benefit of the doubt and assume that I may have misinterpreted you.
– KSM