Solved[B]Bug in my program[/B]

Posts 1–8 of 8 · Page 1 of 1
[B]Bug in my program[/B]
Here is my code, it crushes every time when i run it

// Example program #1 from Chapter 6 of
// Absolute Beginner's Guide to C, 3rd Edition
// File Chapter6ex1.c


// This program pairs three kids with their favorite superhero


#include <stdio.h>
#include <string.h>

main()
{

char Kid1[12];
// Kid1 can hold an 11-character name
// Kid2 will be 7 characters (Maddie plus null 0)
char Kid2[] = "Maddie";
// Kid3 is also 7 characters, but specifically defined
char Kid3[7] = "Andrew";
// Hero1 will be 7 characters (adding null 0!)
char Hero1 = "Batman";
// Hero2 will have extra room just in case
char Hero2[34] = "Spiderman";
char Hero3[25];

Kid1[0] = 'K'; //Kid1 is being defined character-by-character
Kid1[1] = 'a'; //Not efficient, but it does work
Kid1[2] = 't';
Kid1[3] = 'i';
Kid1[4] = 'e';
Kid1[5] = '\0'; // Never forget the null 0 so C knows when the
// string ends

strcpy(Hero3, "The Incredible Hulk");

printf("%s\'s favorite hero is %s.\n", Kid1, Hero1);
printf("%s\'s favorite hero is %s.\n", Kid2, Hero2);
printf("%s\'s favorite hero is %s.\n", Kid3, Hero3);

return 0;
}



I found the bug it was char Hero1 = "Batman"; it must be char Hero1[7] = "Batman";

It was mistake in the book!!!
Quote Originally Posted by familyguy2 View Post
I found the bug it was char Hero1 = "Batman"; it must be char Hero1[7] = "Batman";

It was mistake in the book!!!
Really nice you found the bug by yourself!

Yea multiple characters are always char arrays or strings.
Quote Originally Posted by RoPMadM View Post


Really nice you found the bug by yourself!

Yea multiple characters are always char arrays or strings.


Thanks for advice!

- - - Updated - - -

Quote Originally Posted by garupede View Post
Ei man, in the exam I've just done the teacher suggested us to use a #define instead of putting numbers into "[]";
I don't know if everyone has had this suggestion or even if it is useful for something. Moreover I noticed that you use a domension of 7 chars more than once, so you could use, for instance:
Code:
#define DIM 7
I hope that this can help you!
Yeah it is useful, but for now i am yust writing simple codes.
Quote Originally Posted by familyguy2 View Post
Thanks for advice!

- - - Updated - - -



Yeah it is useful, but for now i am yust writing simple codes.
Define is very simple, that code just makes it so every time you type DIM, it will be replaced with the integer 7.
Ei man, in the exam I've just done the teacher suggested us to use a #define instead of putting numbers into "[]";
I don't know if everyone has had this suggestion or even if it is useful for something. Moreover I noticed that you use a domension of 7 chars more than once, so you could use, for instance:
Code:
#define DIM 7
I hope that this can help you!
I know, but i used it only once, i think.
Posts 1–8 of 8 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us