2

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;
}
Davinci3f
  • 21
  • 1
  • 1
  • 4

4 Answers4

5

The problem is with char Hero1 = "Batman":

  • When you use a double-quoted string of characters in your code, the compiler replaces it with a pointer to the beginning of the memory space in which the string will reside during runtime.
  • So by 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:

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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
barak manos
  • 29,648
  • 10
  • 62
  • 114
3
char Hero1 = "Batman";

should be

char Hero1[] = "Batman";
ouah
  • 142,963
  • 15
  • 272
  • 331
0

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.

phyrrus9
  • 1,441
  • 11
  • 26
0

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

  • The first version is not correct. C will not null-terminate the array automatically. You need to do `char a[] = {'a', 'b', 'c', 'd', 'f', '\0'};` – costaparas Dec 31 '20 at 05:15
  • Also, please work on fixing the indentation and formatting of your answer. – costaparas Dec 31 '20 at 05:16
  • sir first program shows why that error occurred in your program and the rest of two is the solution to resolve it – akshay_mithari Jan 17 '21 at 17:32
  • I'm not the OP (this question was posted 6 years ago btw), I was commenting on this post since it was flagged as a low-quality answer, advising you to [fix up the formatting in the answer](https://stackoverflow.com/editing-help). The bug I am referring to is for the solution you proposed using the array, which is invalid C, please check again. – costaparas Jan 18 '21 at 01:18
  • sir all solutions are in working condition i also attached screenshots of my codes and output i checked that code on programiz C compiler and also changed format of my answer – akshay_mithari Jan 18 '21 at 05:47
  • Please refer to my original comment, "C will not null-terminate the array automatically" -- you are missing a null terminator `'\0'`. The code can crash in such a case, though it may appear to work out of luck on some systems in some cases, you can read more about it in [this post](https://stackoverflow.com/questions/40821419/when-why-is-0-necessary-to-mark-end-of-an-char-array). You can also verify this is a bug if you compile with address sanitizer extensions. – costaparas Jan 18 '21 at 06:04
  • thank you for your support sir I corrected my code – akshay_mithari Jan 18 '21 at 12:15