Warning: Initialization makes integer from pointer without a cast

This is a discussion on Warning: Initialization makes integer from pointer without a cast within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am somewhat new to C programming (I started last year) and I'm fairly good at it so ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    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 the warning "initialization makes integer from pointer without a cast" 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

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    depart & arrive are arrays, but you are only trying to assign one value to them.
    The compiler assumes that the first {} is for initializing the depart arrays, and thus you are truncating the strings (const char*) to int.
    Also remember that string literals should be const char* because they are read-only in most cases.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Thanks for the help, Elysia. But then I realized that I didn't mean for 'depart' and 'arrive' to be arrays--they're just integer values that are part of my array of structures. I did change my string literal to be const char*, so thanks for that advice!

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    One thing you might consider is to define a template.

    For example:
    Code:
    typedef struct {
    	int depart;
    	int arrive;
            const char *attendant;
    	} FLIGHT_DEF;
    Then declare the actual storage and contents:

    Code:
    FLIGHT_DEF flight[] = {
    	{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 advantage there is that you may now use FLIGHT_DEF as a parameter type to functions (cast), or determine number of elements:

    Code:
    #define FLIGHT_DEF_SIZE		(sizeof(flight)/sizeof(FLIGHT_DEF))
    ... which could come in handy.

    The capitalization and closing brace on separate line are old habits. Makes adding/deleting records easier. Eventually I take all the capitalized typedefs into a separate include file so that they may become available in other modules.
    Last edited by nonoob; 03-09-2009 at 04:25 PM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by nonoob View Post
    The capitalization and closing brace on separate line are old habits.
    And not necessarily bad ones, either. It makes code more readable in my opinion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  3. assignment makes pointer from integer
    By crescen7 in forum C Programming
    Replies: 4
    Last Post: 06-25-2002, 10:08 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 3
    Last Post: 01-14-2002, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21