Thread: makes pointer from integer without a cast

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    makes pointer from integer without a cast

    Hey, I am having a problem with my program, it keeps saying that

    95 [Warning] passing arg 1 of `initmovie' makes pointer from integer without a cast
    95 [Warning] passing arg 2 of `initmovie' makes pointer from integer without a cast

    Below is my program...Any help with be greatly appreciated..thanks in advance.

    The Error is pointed out in red below:

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>

    struct movie {
    char * title; //movie title
    char * director; //director's name
    int year; //release year
    int duration; //expressed in minutes
    struct movie *next; //pointer to next movie
    };

    typedef struct movie MovieRecord;
    typedef struct movie *MovieRecordPtr;



    void NewMovie(void);
    struct movie *initmovie( char *, char *, int, int );
    void insertmovie( struct movie * );
    void printlist( struct movie * );


    //struct movie *strPtr = (struct movie *) NULL;
    //struct movie *endPtr = (struct movie *) NULL;
    MovieRecordPtr strPtr = NULL;
    MovieRecordPtr endPtr = NULL;



    int main(int argc, char *argv[])
    {
    char title;
    char director;
    int year;
    int duration;
    struct movie *ptr;

    NewMovie();
    printlist(strPtr);


    system("PAUSE");
    return 0;
    }


    void NewMovie(void)
    {
    char title;
    char director;
    int year;
    int duration;
    struct movie *ptr;
    int count = 0;
    int amount = 0;

    printf("\nHow many movies do you want to store? ");
    scanf("%d", &amount);

    while(amount != 0)
    {
    count++;

    printf("\nMovie #%d ", count);
    printf("\t Title: \t\t");
    scanf("%s", &title);
    char c =getchar();


    printf("\n");
    printf("\t\t Director: \t\t");
    scanf("%s", &director);
    char d =getchar();


    printf("\n");
    printf("\t\t Duration of minutes: \t");
    scanf("%d", &duration);


    printf("\n");
    printf("\t\t Year: \t\t\t");
    scanf("%d", &year);
    ptr = initmovie( title, director, duration, year );
    insertmovie( ptr );


    }

    }


    struct movie *initmovie( char *title, char *director, int duration, int year )
    {
    struct movie *ptr;
    ptr = (struct movie *) calloc( 1, sizeof(struct movie ) );
    if( ptr == NULL )
    return (struct movie *) NULL;
    else {
    strcpy( ptr->title, title );
    ptr->director = director;
    return ptr;
    }
    }

    */
    void insertmovie( struct movie *new )
    {
    struct movie *temp, *prev;

    if( strPtr == NULL ) {
    strPtr = new;
    endPtr = new;
    strPtr->next = NULL;
    return;
    }

    temp = strPtr;

    while( strcmp( temp->title, new->title) < 0 ) {
    temp = temp->next;
    if( temp == NULL )
    break;
    }


    if( temp == strPtr ) {
    new->next = strPtr;
    strPtr = new;
    }
    else {
    prev = strPtr;
    while( prev->next != temp ) {
    prev = prev->next;
    }
    prev->next = new;
    new->next = temp;
    if( endPtr == prev )
    endPtr = new;
    }
    }


    void printlist( struct movie *ptr )
    {
    while( ptr != NULL )
    {
    printmovie( ptr );
    ptr = ptr->next;
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Just as a proof for "quzah" (work with files in С99), I'll press quote and and code tags around the code, to show him it works:

    Quote Originally Posted by nyquil View Post
    Hey, I am having a problem with my program, it keeps saying that

    95 [Warning] passing arg 1 of `initmovie' makes pointer from integer without a cast
    95 [Warning] passing arg 2 of `initmovie' makes pointer from integer without a cast

    Below is my program...Any help with be greatly appreciated..thanks in advance.

    The Error is pointed out in red below:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>
    
    struct movie {
           char *           title;       //movie title
           char *           director;    //director's name
           int              year;        //release year
           int              duration;    //expressed in minutes
           struct movie    *next;        //pointer to next movie
    };
    
    typedef struct movie    MovieRecord;
    typedef struct movie   *MovieRecordPtr;
    
    
    
    void NewMovie(void);
    struct movie *initmovie( char *, char *, int, int );
    void insertmovie( struct movie * );
    void printlist( struct movie * );
    
    
    //struct movie *strPtr = (struct movie *) NULL;
    //struct movie *endPtr = (struct movie *) NULL;
    MovieRecordPtr strPtr = NULL;
    MovieRecordPtr endPtr = NULL;
    
    
    
    int main(int argc, char *argv[])
    {
        char title;
        char director;
        int year;
        int duration;
        struct movie *ptr;
        
        NewMovie();
        printlist(strPtr);
        
      
      system("PAUSE");	
      return 0;
    }
    
    
    void NewMovie(void)
    {
        char title;
        char director;
        int year;
        int duration;
        struct movie *ptr;
        int count = 0;
        int amount = 0;
        
        printf("\nHow many movies do you want to store? ");
        scanf("%d", &amount);
        
        while(amount != 0)
        {
                     count++;
                     
                     printf("\nMovie #%d ", count);
                     printf("\t Title: \t\t");
                     scanf("%s", &title);
                     char c =getchar();
                     
                     
                     printf("\n");
                     printf("\t\t Director: \t\t");
                     scanf("%s", &director);
                     char d =getchar();
                     
                     
                     printf("\n"); 
                     printf("\t\t Duration of minutes: \t");
                     scanf("%d", &duration);
                     
                     
                     printf("\n");
                     printf("\t\t Year: \t\t\t");
                     scanf("%d", &year);
                     ptr = initmovie( title, director, duration, year );
                     insertmovie( ptr );
                     
                        
        } 
        
    }
    
    
    struct movie *initmovie( char *title, char *director, int duration, int year )
    {
       struct movie *ptr;
       ptr = (struct movie *) calloc( 1, sizeof(struct movie ) );
       if( ptr == NULL )                       
           return (struct movie *) NULL;        
       else {                                 
           strcpy( ptr->title, title );         
           ptr->director = director;           
           return ptr;                         
       }
    }
    
                                                                */
    void insertmovie( struct movie *new )
    {
       struct movie *temp, *prev;              
    
       if( strPtr == NULL ) {                    
           strPtr = new;                         
           endPtr = new;
           strPtr->next = NULL;                
           return;                             
       }
    
       temp = strPtr;                           
                         
       while( strcmp( temp->title, new->title) < 0 ) {
              temp = temp->next;               
              if( temp == NULL )               
                  break;
       }
    
      
       if( temp == strPtr ) {
          new->next = strPtr;            
          strPtr = new;                  
       }
       else {    
          prev = strPtr;   
          while( prev->next != temp ) {
              prev = prev->next;
          }
          prev->next = new;             
          new->next = temp;
          if( endPtr == prev )             
             endPtr = new;                 
       }
    }
    
    
    void printlist( struct movie *ptr )
    {
       while( ptr != NULL )           
       {
          printmovie( ptr );          
          ptr = ptr->next;           
       }
    }

    Note: USE CODE TAGS!
    This is the first and last time I did this.


    About your error: you have an integer, you use it as a pointer. This is normally wrong. I couldn't read your code because of the lack of code tags, so I don't know where the error lies at this point.

    Edit: title and directory are characters (so, essentially, integers); not pointers (to characters)

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Most people won't read this unless you indent it properly and use code tags.

    The problem is that you declared "title" and "director" as char. And scanf into an invalid buffer. Malloc first.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2
    Sorry about the post earlier and not putting in the Code Tags:

    My Error is at : ptr = initmovie( title, director, duration, year );



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>
    
    struct movie {
           char *           title;       //movie title
           char *           director;    //director's name
           int              year;        //release year
           int              duration;    //expressed in minutes
           struct movie    *next;        //pointer to next movie
    };
    
    typedef struct movie    MovieRecord;
    typedef struct movie   *MovieRecordPtr;
    
    
    void NewMovie(void);
    struct movie *initmovie( char *, char *, int, int );
    void insertmovie( struct movie * );
    void printlist( struct movie * );
    void printmovie( struct movie * );
    
    
    //struct movie *strPtr = (struct movie *) NULL;
    //struct movie *endPtr = (struct movie *) NULL;
    MovieRecordPtr strPtr = NULL;
    MovieRecordPtr endPtr = NULL;
    
    
    
    int main(int argc, char *argv[])
    {
        char title;
        char director;
        int year;
        int duration;
        struct movie *ptr;
        
        NewMovie();
        printlist(strPtr);
        
      
      system("PAUSE");	
      return 0;
    }
    
    
    void NewMovie(void)
    {
        char title;
        char director;
        int year;
        int duration;
        struct movie *ptr;
        int count = 0;
        int amount = 0;
        
        printf("\nHow many movies do you want to store? ");
        scanf("%d", &amount);
        
        while(amount != 0)
        {
                     count++;
                     
                     printf("\nMovie #%d ", count);
                     printf("\t Title: \t\t");
                     scanf("%s", &title);
                     char c =getchar();
                     //DisplayEntireList( &startPtr, m_name );
                     
                     printf("\n");
                     printf("\t\t Director: \t\t");
                     scanf("%s", &director);
                     char d =getchar();
                     //DisplayEntireList( &secondPtr, d_name );
                     
                     printf("\n"); 
                     printf("\t\t Duration of minutes: \t");
                     scanf("%d", &duration);
                     //DisplayEntireList( &thirdPtr, m_duration );
                     
                     printf("\n");
                     printf("\t\t Year: \t\t\t");
                     scanf("%d", &year);
                     ptr = initmovie( title, director, duration, year );
                     insertmovie( ptr );
                     //DisplayEntireList( &fourthPtr, m_year );
                        
        } 
        
    }
    
    
    struct movie *initmovie( char *title, char *director, int duration, int year )
    {
       struct movie *ptr;
       ptr = (struct movie *) calloc( 1, sizeof(struct movie ) );
       if( ptr == NULL )                      
           return (struct movie *) NULL;       
       else {                                  
           strcpy( ptr->title, title );          
           ptr->director = director;           
           return ptr;                        
       }
    }
    
    
    void insertmovie( struct movie *new )
    {
       struct movie *temp, *prev;                /* similar to deletenode      */
    
       if( strPtr == NULL ) {                     /* if an empty list,          */
           strPtr = new;                          /* set 'head' to it           */
           endPtr = new;
           strPtr->next = NULL;                 
           return;                             
       }
    
       temp = strPtr;                            
                         
       while( strcmp( temp->title, new->title) < 0 ) {
              temp = temp->next;                
              if( temp == NULL )                
                  break;
       }
    
      
       if( temp == strPtr ) {
          new->next = strPtr;            
          strPtr = new;                   
       }
       else {     
          prev = strPtr;   
          while( prev->next != temp ) {
              prev = prev->next;
          }
          prev->next = new;            
          new->next = temp;
          if( endPtr == prev )            
             endPtr = new;                
       }
    }
    
    
    void printlist( struct movie *ptr )
    {
       while( ptr != NULL )           
       {
          printmovie( ptr );           
          ptr = ptr->next;           
       }
    }
    
    
    void printmovie( struct movie *ptr )
    {
       printf("\nMovie #");
       printf("\t\t%s\n", ptr->title );
       printf("\n");
       printf("\t\t%s, ", ptr->director );
       printf("\t\t%d, ", ptr->duration );
       printf("\t\t%d, ", ptr->year );
       printf("\n");
    }
    Last edited by nyquil; 05-06-2010 at 06:01 AM. Reason: fixed linking error i was having

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    In 'initmovie', you're allocating space for the structure, but not for the structure member named 'title'. The 'title' pointer will be 0 (calloc zeroes the memory) and so you are derefencing a NULL pointer in strcpy. Even if malloc were used instead, 'title' would contain a garbage value. You need to allocate space for 'title'.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    void NewMovie(void)
    {
        char title;
        char director;
        int year;
        int duration;
        struct movie *ptr;
        int count = 0;
        int amount = 0;
        
        printf("\nHow many movies do you want to store? ");
        scanf("%d", &amount);
        
        while(amount != 0)
        {
            count++;
                     
            printf("\nMovie #%d ", count);
            printf("\t Title: \t\t");
            scanf("%s", &title);
            char c =getchar();
            //DisplayEntireList( &startPtr, m_name );
                     
            printf("\n");
            printf("\t\t Director: \t\t");
            scanf("%s", &director);
            char d =getchar();
            //DisplayEntireList( &secondPtr, d_name );
                     
            printf("\n"); 
            printf("\t\t Duration of minutes: \t");
            scanf("%d", &duration);
            //DisplayEntireList( &thirdPtr, m_duration );
                     
            printf("\n");
            printf("\t\t Year: \t\t\t");
            scanf("%d", &year);
            ptr = initmovie( title, director, duration, year );
            insertmovie( ptr );
            //DisplayEntireList( &fourthPtr, m_year );
                        
        }
        
    }
    #1 As Brafil mentioned, you are attempting to read a string into a variable that only has space for a single character, not multiple characters. Also when you pass these single char variables to the intimovie function, one that expects char* and not simple char variables, you're gonna get errors there as well. You probably want to have an array of sufficient size to read into for this purpose.

    #2 ptr is a local variable and has nothing to do with the variable of the same name within main. Adding new nodes to this pointer will only change that list, it will not affect the list outside of this function and therefore when the function ends, it all goes bye-bye (and you also cause a massive memory leak in the process). I'm guessing you're gonna need to pass in a pointer from main or use one of those global pointers... strPtr perhaps?

    #3 You have an infinite loop, amount never changes during the loop execution.




    Code:
    struct movie *initmovie( char *title, char *director, int duration, int year )
    {
        struct movie *ptr;
        ptr = (struct movie *) calloc( 1, sizeof(struct movie ) );
        if( ptr == NULL )                      
            return (struct movie *) NULL;       
        else {                                  
            strcpy( ptr->title, title );          
            ptr->director = director;           
            return ptr;                        
        }
    }
    #4 Bit of a nit but those arguments should be const char*, not char*.

    #5 Can we say instant program crash? You need to allocate memory for these members individually and then copy the strings into them. Merely using strcpy to copy data into an uninitialized member is a recipe for disaster. The later assignment of director works syntactically but logically is another time bomb waiting to happen.

    #6 What were you planning on doing with duration and year?




    Code:
    void insertmovie( struct movie *new )
    {
       struct movie *temp, *prev;                /* similar to deletenode      */
    
       if( strPtr == NULL ) {                     /* if an empty list,          */
           strPtr = new;                          /* set 'head' to it           */
           endPtr = new;
           strPtr->next = NULL;                 
           return;                             
       }
    
       temp = strPtr;                            
                         
       while( strcmp( temp->title, new->title) < 0 ) {
              temp = temp->next;                
              if( temp == NULL )                
                  break;
       }
    
      
       if( temp == strPtr ) {
          new->next = strPtr;            
          strPtr = new;                   
       }
       else {     
          prev = strPtr;   
          while( prev->next != temp ) {
              prev = prev->next;
          }
          prev->next = new;            
          new->next = temp;
          if( endPtr == prev )            
             endPtr = new;                
       }
    }
    #7 A possible scenario is that the passed in new pointer is null (it's one possible return values from calling the previously critiqued initmovie function). If you assign strPtr to this null value and then later attempt to dereference the null pointer (strPtr) to set it's next pointer, then we again have an instant program crash.

    #8 ...and I'm bored now, I'm sure there are other things wrong but I'm too lazy ATM to look any further.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Pointer from integer without cast
    By STDSkillz in forum C Programming
    Replies: 2
    Last Post: 10-22-2007, 09:39 PM
  3. help with error message
    By ddolddolee82 in forum C Programming
    Replies: 10
    Last Post: 05-07-2006, 09:37 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Replies: 3
    Last Post: 01-14-2002, 12:13 PM