Warning: Initialization Makes Integer From Pointer Without A Cast

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nexusdarkblue
    New Member
    • Mar 2009
    • 6

    #1

    Warning: Initialization Makes Integer From Pointer Without A Cast

    Hello everyone,

    I am somewhat new to C programming (I started last year) and I'm fairly good at it so far...that is, until I've recently started working with structures.

    In my current program, I'm simply trying to initialize a simple array of structures that will be the test template for other similar programs I plan on using in the future. The program is as follows:

    =============== =============== =============== ============
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    
            int time, closest;
    
            struct flight
            {
                    int depart[FLIGHTS];
                    int arrive[FLIGHTS];
                    char *attendant;
            };
    
            struct flight schedule[] =
            {{800, 1016, "Jason Mackenzie"},
              {943, 1152, "Valerie Woods"},
              {1119, 1331, "Antonio Vasquez"},
              {1247, 1500, "Natalie McIver"},                   
              {1400, 1608, "Scott Curtis"},
              {1545, 1755, "Yvonne Vogelar"},
              {1900, 2120, "Mitch Matthews"},
              {2145, 2358, "Marcie Maddox"}};
    
            printf("%d\n", schedule[0].arrive[0]);
    
            return 0;
    }
    =============== =============== =============== ============

    For each line where I'm trying to initialize each index of the schedule array, the
    compiler gives me a warning and I can't figure out why.

    To test to see if my code was correct or not, I used a simple printf( ) statement, and all I got for the output was zero, so I'm obviously doing something wrong here. Can anyone help? I'd really appreciate it--thanks!

    RICH
    Last edited by pbmods; Mar 8 '09, 10:02 PM. Reason: Added CODE tags.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Originally posted by nexusdarkblue
    Code:
            struct flight
            {
                    int depart[FLIGHTS];
                    int arrive[FLIGHTS];
                    char *attendant;
            };
    
            struct flight schedule[] =
            {{800, 1016, "Jason Mackenzie"},
              {943, 1152, "Valerie Woods"},
              {1119, 1331, "Antonio Vasquez"},
              {1247, 1500, "Natalie McIver"},                   
              {1400, 1608, "Scott Curtis"},
              {1545, 1755, "Yvonne Vogelar"},
              {1900, 2120, "Mitch Matthews"},
              {2145, 2358, "Marcie Maddox"}};
    The 'departs' and 'arrive' structure fields are arrays. You need to properly delimit array initializers:
    Code:
            struct flight schedule[] = {
                { {800}, {1016}, "Jason Mackenzie"},
                { {943}, {1152}, "Valerie Woods"},
                { {1119}, {1331}, "Antonio Vasquez"},
                { {1247}, {1500}, "Natalie McIver"},                   
                { {1400}, {1608}, "Scott Curtis"},
                { {1545}, {1755}, "Yvonne Vogelar"},
                { {1900}, {2120}, "Mitch Matthews"},
                { {2145}, {2358}, "Marcie Maddox"}
            };
    Suppose you want one of these initializers to set more than the first element in the 'departs' array:
    Code:
                { {943, 1159, 1530}, {1152}, "Valerie Woods"},
    One last observation, the 'attendant' field should be defined as 'const char * const' unless you intend to change the 'attendant' strings on the fly.

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Originally posted by nexusdarkblue
      Hello everyone,

      I am somewhat new to C programming (I started last year) and I'm fairly good at it so far...that is, until I've recently started working with structures.

      In my current program, I'm simply trying to initialize a simple array of structures that will be the test template for other similar programs I plan on using in the future. The program is as follows:

      Code:
      =========================================================
      
      #include <stdio.h>
      
      int main(int argc, char *argv[])
      {
      
              int time, closest;
      
              struct flight
              {
                      int depart[FLIGHTS];
                      int arrive[FLIGHTS];
                      char *attendant;
              };
      
              struct flight schedule[] =
              {{800, 1016, "Jason Mackenzie"},
                {943, 1152, "Valerie Woods"},
                {1119, 1331, "Antonio Vasquez"},
                {1247, 1500, "Natalie McIver"},                   
                {1400, 1608, "Scott Curtis"},
                {1545, 1755, "Yvonne Vogelar"},
                {1900, 2120, "Mitch Matthews"},
                {2145, 2358, "Marcie Maddox"}};
      
              printf("%d\n", schedule[0].arrive[0]);
      
              return 0;
      }
      =============== =============== =============== ============

      For each line where I'm trying to initialize each index of the schedule array, the
      compiler gives me a warning and I can't figure out why.

      To test to see if my code was correct or not, I used a simple printf( ) statement, and all I got for the output was zero, so I'm obviously doing something wrong here. Can anyone help? I'd really appreciate it--thanks!

      RICH
      You are trying to initialize a array with a single integer, interesting thing is that you get only a warning, this is a compile time error on all the compilers i know.

      You need to add an extra pair of curly brackets around the array intialization something like this:

      Code:
          struct flight schedule[] =
      	{
      	  {{800}, {1016}, "Jason Mackenzie"},
      	  {{943}, {1152}, "Valerie Woods"},
      	  {{1119}, {1331}, "Antonio Vasquez"},
      	  {{1247}, {1500}, "Natalie McIver"},
      	  {{1400}, {1608}, "Scott Curtis"},
      	  {{1545}, {1755}, "Yvonne Vogelar"},
      	  {{1900}, {2120}, "Mitch Matthews"},
      	  {{2145}, {2358}, "Marcie Maddox"}
      	};
      EDIT:Guess I'm a bit too late,didn't see don's post coming ^^

      Comment

      • nexusdarkblue
        New Member
        • Mar 2009
        • 6

        #4
        Ahhh, the curly braces! Thanks to you both donbock and Savage--my code works like a charm now! (Can't believe I forgot those curly braces--it's so simple! :-)

        RICH
        Last edited by pbmods; Mar 8 '09, 10:01 PM. Reason: Removed useless formatting.

        Comment

        Working...