"Carry" variables between functions, appears strange character in place?

Asked

Viewed 163 times

4

In function main when I print a char, it appears a strange code like a hexa:

But when I print the same char in another function it appears normal.

char tempA[100] = "1111";


int main () {

    strcpy(tempA , "11");
    printf("\n\n  )  ====>  tempTestA = %s \n\n", tempA);   // aqui NAO funciona, EXIBINDO APENAS CARACTERES estranhos..

  outraFuncao(tempA);

}

int outraFuncao(tempA)
{

    printf("\n\n  )  ====>  tempTestB = %s \n\n", tempA);   // aqui NAO funciona, EXIBINDO APENAS CARACTERES estranhos..


    strcpy(tempmais , "11");
    printf("\n\n  )  ====>  tempTestB = %s \n\n", tempmais);   // aqui funciona bem..

//restante da funcao...

}

And if I call the job outraFuncao() with some parameter, it also shows in the values, only strange characters.

What could I be doing wrong?

  • 1

    Apparently the title does not match the problem presented. Which problem you want to solve?

  • And if I call the function "otherFuncao()" with some parameter, it also shows in the values, only strange characters. What I might be doing wrong?

  • The ideal is to put a real code you are using. The question is extremely confusing and only got worse with this comment.

  • but that’s the real code...

  • eu chamo a função "outraFuncao()" com algum parâmetro your sentence, the code doesn’t do it. If it’s the real code, the comment doesn’t make sense.

  • sorry bad way, I think I’m not able to ask the question... but I edited the post, I hope to be clearer..

  • In fact, it only got worse, because the code doesn’t match the statement, the body doesn’t match the title that would make sense if the rest of the text matched the last statement. Anyway I answered what the title asks, rearranging the code to show you the right way to do and fixing the printing problem. There may be some other problem, but the code doesn’t show it. Still the code of my answer is the complete solution.

  • I found it curious that the code has several errors to the answers that only correct one of them has more votes than mine that corrects all errors.

  • for eh... I am confused .. . too much...

  • Hello @bigown, (I edited the post) and made the corrections that Oce suggested! but the problem persists... when I call otherFuncao(tempa); and print to the screen .. errors continue.. can give me a touch?

  • You didn’t do everything I said. Did you see that mine is working? Click there on ideone to see? Anyway you can’t keep changing the question. Question asked, question answered. If you have another problem, another question must be asked. But before you do another need to follow what I said, you’re doing something else, it won’t even work. To tell you the truth, only a bad compiler will compile this code. You fixed what the other answers said, mine solves all problems. I proved it.

  • Okay, let me review! I’ve definitely screwed up here! Thanks for the patience! This is learning and with an avalanche of information!! rrsrs !

  • thanks!!! I realized what I was doing wrong!!!! Thanks!! our... how to study tires tires!!!

Show 8 more comments

3 answers

3

Change "%c" to "%s". This is the error. Even in the case that is apparently working, it is by chance...

3


Variables are not carried from one function to another. Only values are copied from one function to another. And some of these values give access to some object available in memory.

You needs to learn about parameters. You already use arguments in ready-made functions that are those values that are passed in the function call. Now it will create its functions to receive these values, are the parameters.

What you did is using a global variable. Although it works in some cases is not the right way to do it. You should only use this form when you completely master the entire operation of an application (which many programmers never get to, especially in C).

In fact this is a simplification. The ideal would be to study the subject more deeply.

There is another problem in printing. She is having a character printed. It looks like she wants to print a string. Then you’d have to use %s.

Working is not the same as being right.

void outraFuncao(char *temp) { //recebe 1 ponteiro p/ o objeto que é uma sequência de chars
    strcpy(temp, "11");
    printf("tempTest = %s\n", temp);
}

int main() {
    char tempA[100] = "1111";
    outraFuncao(tempA); //passa o ponteiro do array
    printf("tempTest = %s\n", tempA); //o objeto foi modificado, já que passou um ponteiro
    strcpy(tempA, "22"); //mudou o valor
    printf("tempTest = %s\n", tempA); //imprimiu a string
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

3

%c is used for single char, as you have char[100] must use %s

Browser other questions tagged

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