Reputation: 41
i just started to learn how to program and i encountered this error that goes like this: "initialization makes integer from pointer without a cast [enabled by default]" What is the problem?
// 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;
}
Upvotes: 4
Views: 23910
Reputation: 1
Solution:
This error you get because String data type is not in C programming you can print string by using array or char pointer like
1.Array:
#include<stdio.h>
int main(){
char a[]={'a','b','c','d','f','\0'};
printf("%s",a);
return 0;
}
Click here to check the output of solution array
2.char pointer:
#include<stdio.h>
int main(){
char* a="abcd";
printf("%s",a);
return 0;
}
Click here to check the output of solution char pointer
Upvotes: 0
Reputation: 30136
The problem is with char Hero1 = "Batman"
:
char Hero1 = "Batman"
, you are actually attempting to assign a memory address (which typically consists of 32 or 64 bits of data, depending on your system) into a character variable (which typically stores 8 bits of data).In order to fix the problem, you need to change it to either one of the following options:
char Hero1[] = "Batman"
char* Hero1 = "Batman"
FYI, in both cases above, the string "Batman"
will reside in a read-only memory section during runtime.
However, there is a notable difference between these two cases:
char Hero1[]
, the "Batman"
string will be copied into the stack every time the function is called. The Hero1
array will start at that address, and you will be able to change the contents of that array at a later point within the function.char* Hero1
, the "Batman"
string will not be copied into the stack every time the function is called. The Hero1
variable will be pointing to the original address of the string, hence you will not will be able to change the contents of that string at any point within the function.When the executable image is generated from your code, the string is placed in the code-section, which is one of several memory-sections within the program. The compiler then replaces the so-called "string assignment" with a so-called "integer assignment".
For example, char* x = "abc"
is changed into char* x = (char*)0x82004000
before being compiled into object code, where 0x82004000
is the (constant) address of the string in the program's memory space.
When you do sizeof("abc")
, the executable image will not even contain the "abc"
string, since there is no "runtime" operation performed on this string.
There is no object code generated for sizeof
- the compiler computes this value during compilation, and immediately replaces it with a constant.
You can look into the (intermediate) map file that is usually generated, and see that the input string of that sizeof
operation does not appear anywhere.
Upvotes: 9
Reputation: 1467
A couple times you have some issues:
#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"; //needs to be a pointer
// Hero2 will have extra room just in case
char *Hero2 = "Spiderman"; //needs to be a pointer
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;
}
You should define all of your vars at the top of the function, its a good C practice.
Other than that, I flagged the issues (and corrected them) with comments.
Upvotes: 0