Conversation error from int to String

Asked

Viewed 97 times

0

This code I’m using to study C, is not converting (at least it’s what I think) a INT temp10 to CHAR... appearing an error:

line 5: 5182 Segmentation failure (recorded core image)

What can it be?

int main() {
    // aqui esta tudo OK!
    char tempA[100] = "11";
    printf("COUVE = %s\n", tempA);

    strcpy(tempA, "22");
    printf("CEREJA = %s\n", tempA);

    strcpy(tempA, "kk");
    outraFuncao(tempA);

    //  mas se transformo um int em string:
    temp6 = 1000;
    temp7 =  100;
    temp8 =   10;
    temp9 =    1;
    temp10= temp6+temp7+temp8+temp9;
    char temp10=temp10+'0';

    printf("\n\n\n");
    //  da falha de segmentacao na hora do print
    printf("CEREJA2 = %s\n", temp10);

    //  aqui vai ser importante para meu programa... chamar um funcao com esse valor convertido de int para char
    //  mas o mesmo erro defalha desegmentacao acontece..
    outraFuncao(temp10);
} 


void outraFuncao(char *temp) {
    //  strcpy(temp, "33");
    printf("AMORA = %s\n", temp);
}
  • This code doesn’t make any sense, so it’s hard to start helping. There’s nothing converting anything. I don’t even know if you need it. There’s a lot that doesn’t need to be there. Actually the whole code doesn’t do anything very useful. It doesn’t even serve as an exercise. Besides you seem to ignore what’s said, so it gets complicated.

  • The question is, why convert something if you can already use what you want directly? I didn’t even try to answer because it will only solve your problem without teaching you anything useful. What’s the point of giving you the solution to this, if what you’re doing is all wrong? I don’t know what the goal is, but this code doesn’t help to learn anything. I don’t know if you got to do this somewhere. I know you need to do it differently or ask questions that will really help you.

  • I suggest doing as Joseph X said, take a book and start learning structurally. Maybe even start with easier language. Normally I usually indicate starting with C even though almost everyone is against it. But there are cases and cases. C is a language full of detail. With meaningless "exercises" you will not learn. You need something that will guide you, that will present each concept, one at a time, in the right order, things that have relevance.Then doing the exercises suggested in the book will have doubts and it will be + easy to ask here.I’m afraid this code is + harming learning

  • Another possibility is to look for a good course, I do not know if you have in your city. When you’re lost, one-on-one guided help is important.I’m trying to show you that this path adopted is not going to help you, you need to opt for another form of learning. Be careful with easy and cheap options (such as tutors and videos on the internet), they hurt more than help.

  • thanks for the attention, but I do not wish to disturb you any more, I already approved an answer. Let’s leave it for there.

1 answer

1



this here NAY is transformed into a int in a string, is converting a int in ONE character... just doesn’t make sense

 temp10= temp6+temp7+temp8+temp9;
 char temp10=temp10+'0';

here gives segmentation failure because temp10 NAY is a string, so you cannot use the "%s" format, it has to be the "%d"

 printf("\n\n\n");
 //  da falha de segmentacao na hora do print
 printf("CEREJA2 = %s\n", temp10);

a way to turn an int into a string is with the function snprintf

char buf[20];
snprintf(buf, 20, "CEREJA=%d\n", temp10);

but in your example you don’t even need it, you can just do it like this

printf("CEREJA=%d\n", temp10);

  • 1

    I suggest you study following some book for beginners...it seems to me that this here "C for Beginners" of Deitel is well known http://www.livrariacultura.com.br/p/c-comorprogramar-22679852 ps. if you are going to buy the book, get ready to die with a black money :) other option would be to take some face-to-face course, but I don’t know where to indicate

Browser other questions tagged

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