I can’t find the problem in the code

Asked

Viewed 73 times

-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.

inserir a descrição da imagem aqui

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);
}
  • 1

    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.

  • 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

  • Note that you allocate memory to v4 but does not put anything in this memory. I believe I want initially v4 is a string of zero length, in this case do v4[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 function strlen, just remember that a string in C ends with the character '\0'.

  • thank you, I’ll try

  • 2

    @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.

  • 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.

Show 1 more comment

1 answer

2

It is a feature of C, that a bug in the program will manifest itself quite differently on different computers, or even running several times on the same computer.

In the specific case of your program, it seems that you are not leaving room for the final 0 of the string, nor are you assigning 0 to the last byte. Every string in C has to be finished at 0. The strings scanf() is filling for you, it terminates, so the program works to a certain extent.

The total space of the final string, allocated with malloc() has to be the sum of the 3, plus 1, to accommodate the 0 at the end. And you need to assign this zero explicitly, or else use calloc() instead of malloc() because calloc() Zwas the allocated memory, while malloc() did not.

  • thanks for the explanation

  • exactly, by the epx answer, you understood, that with calloc() always the allocated memory is clean, with malloc() no, so some situations it finds the memory empty and does not present the error, when the space contains a value, the error appears.....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.