error with code

This is a discussion on error with code within the C Programming forums, part of the General Programming Boards category; Im getting an error when compiling that says "assignments makes integer from pointer without a cast". For all three assignment ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    31

    Question error with code

    Im getting an error when compiling that says "assignments makes integer from pointer without a cast". For all three assignment statements.

    Code:
    if (res == 0){
             day = current_day;
             mon = current_month;
             year = current_year;
    current_day
    Current_month and
    current_year are all in another file, that i have #included.

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    if (res == 0)
    {
        day = *current_day;
        mon = *current_month;
        year = *current_year;
        ....
    }
    Without the '*' you're assigning the addresses at which the pointers current_day, current_month and current_year are pointing to the integer variables day, mon and year. With '*' you're assigning the data that is stored at the address that the pointer is pointing at.
    Last edited by Sargnagel; 10-22-2002 at 03:19 AM.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    even with the pointers *. i still get the same error. Ill put all of the code so you can see it all.

    Code:
    #include <stdio.h> 
    #include <time.h> 
    #include <sys/time.h>
    #include "date.h" 
    #include "date.c"
    
    /* true for leap years */ 
    
    
    /************************MY PROGRAM**********************/ 
       static char myday[7][15]= 
       {{"Sunday"}, 
        {"Monday"}, 
        {"Tuesday"}, 
        {"Wednesday"}, 
        {"Thursday"}, 
        {"Friday"}, 
        {"Saturday"} 
       }; 
       int first_day_of_month ( int year, int month ); 
    
               
       char* getday(int d, int m, int y){ 
               
          int current_day; 
          int day = first_day_of_month( y, m ); 
          current_day = (day + d - 1) % 7; 
          return myday[current_day]; 
       } 
    
    
               
       int do_day ( char *cmd ) { 
               
          int day;    
          int month;
          int year; 
          int res; 
          char mon;
          res = sscanf( cmd, "%*c %d %d %d", &day, &month, &year ); 
          if (res == 0){
             day = *current_day;
             mon = *current_month;
             year = *current_year;
             printf("%d %s %d is a %s \n", day, mon, year, getday(day, month, year));
          }
       
          if ( res >= 0 ) {
             switch(month){ 
                case 1:
                   printf("%d January %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 2:
                   printf("%d February %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 3:
                   printf("%d March %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 4:
                   printf("%d April %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 5:
                   printf("%d May %d is a %s \n", day, year, getday(day,month,year)); week_day(day, month, year); 
                   break; 
                case 6:
                   printf("%d June %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 7:
                   printf("%d July %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 8:
                   printf("%d August %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 9:
                   printf("%d September %d is a %s \n", day, year, getday(day,month,year)); week_day(day, month, year); 
                   break; 
                case 10:
                   printf("%d October %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 11:
                   printf("%d November %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 12:
                   printf("%d December %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                default :
                   printf("Invalid month\n"); 
                   return 0; 
             } 
          
          } 
          else{ 
             printf( "Bad day command parameters\n" ); 
          } 
          return 1; 
       } 
    
    
    
               
       int do_month ( char *cmd ) { 
               
       
          int month, year; 
          int res; 
          res = sscanf( cmd, "%*c %d %d", &month, &year ); 
          if ( res == 2 ) { 
             switch(month){ 
                case 1:
                   printf("January\n"); 
                   break; 
                case 2:
                   printf("February\n"); 
                   break; 
                case 3:
                   printf("March\n"); 
                   break; 
                case 4:
                   printf("April\n"); 
                   break;
                case 5:
                   printf("May\n"); 
                   break; 
                case 6:
                   printf("June\n"); 
                   break; 
                case 7:
                   printf("July\n"); 
                   break; 
                case 8:
                   printf("August\n"); 
                   break; 
                case 9:
                   printf("September\n"); 
                   break; 
                case 10:
                   printf("October\n"); 
                   break; 
                case 11:
                   printf("November "); 
                   break; 
                case 12:
                   printf("December\n"); 
                   break; 
                default :
                   printf("Invalid month\n"); 
                   return 0; 
             } 
          } 
          else { 
             printf( "Bad month command parameters\n" ); 
          } 
          return 1; 
       } 
    
    
    
               
       void do_year ( char *cmd ) { 
               
          int year; 
          int res; 
          res = sscanf( cmd, "%*c %d", &year ); 
          if ( res == 1 ) { 
             printf("%d \n", year); 
          } 
          else { 
             printf( "Bad year command parameters\n" ); 
          } 
       } 
    
    
    
               
       int first_day_of_month ( int year, int month ) { 
               
       
          struct tm t = { 0 }; 
          struct tm *tp; 
          time_t now; 
          t.tm_mday = 1; 
          t.tm_mon = month-1; 
          t.tm_year = year-1900; 
          now = mktime(&t); 
          tp = localtime(&now); 
          return tp->tm_wday; 
       } 
    
    
    
               
       int main ( ) { 
               
       
          char buff[BUFSIZ]; 
       
          while ( fgets( buff, BUFSIZ, stdin ) != NULL ) { 
             switch ( buff[0] ) { 
                case 'd':
                   do_day ( buff ); 
                   return 0; 
                   break; 
                case 'm':
                   do_month ( buff ); 
                   return 0; 
                   break; 
                case 'y':
                   do_year ( buff ); 
                   return 0; 
                   break; 
                default:
                   printf( "Unknown command: %c\n", buff[0] ); 
                   return 0; 
                   break; 
             } 
          } 
          return 0; 
       }
    date.c
    Code:
    #include <sys/time.h>
    #include <time.h>
    #include "date.h"
    
    /* true for leap years */
    
               
       int leap_year (int year) {
               
          return year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
       }
    
    /* the number of days in month of year */
    
               
       int days_in_month (int month, int year) {
               
          switch (month) {
             case 9: case 4: case 6: case 11:
                return 30;
             case 2:
                return leap_year(year) ? 29 : 28;
             default:
                return 31;
          }
       }
    
    /* the weekday (0 = Sunday) of a given date
       accurate for dates in the Gregorian calendar
       which began on 14 September 1752 */
    
               
       int week_day (int day, int month, int year) {
               
          int d = 14;    /* The modern calendar began on 14 ... */
          int m = 9;     /* ... September ... */
          int y = 1752;  /* ... 1752 ... */
          int n = 4;     /* ... which was a Thursday */
       
          while (year >  y || month > m || day > d) {
             n++;
             day--;
             if (day < 1) { month--; day = days_in_month(month, year); }
             if (month <  1) { year--; month = 12; }
          }
          return n % 7;
       }
    
       struct timeval tv;
    
               
       int current_day () {
               
          gettimeofday(&tv, 0);
          return localtime(&tv.tv_sec)->tm_mday;
       }
    
               
       int current_month () {
               
          gettimeofday(&tv, 0);
          return localtime(&tv.tv_sec)->tm_mon + 1;
       }
    
               
       int current_year () {
               
          gettimeofday(&tv, 0);
          return localtime(&tv.tv_sec)->tm_year + 1900;
       }

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    Now im getting a totally wrong date being outputted. current_year is saying 1970! something is still not working right but i cant figure out what it is. Also when i try to print out the month name i get an exception: status_access_violation.

    Code:
               
       int do_day ( char *cmd ) { 
               
          int day;    
          int month;
          int year; 
          int res; 
          char mon;
          res = sscanf( cmd, "%*c %d %d %d", &day, &month, &year ); 
          if (res == 0){
             day = current_day();
             mon = current_month();
             year = current_year();
          	printf("%d %d \n", day, year); 
          	return 0;
          }

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    Thanx salem, it fixed the problem. I assumed everything to be correct in that date.c file because the teacher set it up, but apparently not . Um my next task is:
    The program reads and executes multiple commands, each consisting of a single line. It terminates at end of file on input or when it reads a blank line.

    Do you think that it means that the program reads a line of input, produces some output, and then waits for another line of input, and if its a blank line the program terminates?

    I cant really understand what this is asking. I dont want any code for this, just an opinion of what you think the task is asking.
    Cheers guys.

  6. #6
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    If there is one thing I have learned, it is to NEVER trust your tutors! They tell me all sorts of garbage, and hardly any of the programs they have written for me work!!

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    what im trying to do now is make it so that the person can enter either a number for the month, or the name of the month itself.

    Like:d 23 November 2002

    Am i on the right track here?

    Code:
    #include <stdio.h> 
    #include <time.h> 
    #include <sys/time.h>
    #include "date.h" 
    #include "date2.c"
    
    /* true for leap years */ 
    
    
    /************************MY PROGRAM**********************/ 
       static char myday[7][15]= 
       {{"Sunday"}, 
        {"Monday"}, 
        {"Tuesday"}, 
        {"Wednesday"}, 
        {"Thursday"}, 
        {"Friday"}, 
        {"Saturday"} 
       }; 
       int first_day_of_month ( int year, int month ); 
    
               
       char* getday(int d, int m, int y){ 
               
          int current_days; 
          int day = first_day_of_month( y, m ); 
          current_days = (day + d - 1) % 7; 
          return myday[current_days]; 
       } 
    
               
       int convert (char* converted, int failure){
               
          int name;
          if (!converted || !*converted){
             return failure;
          }
          else if (name = atoi(converted)){
             return name;
          }
          return failure;
       }
    
    
    
               
       int do_day ( char *cmd ) { 
               
          int day;    
          int month;
          int year; 
          int res; 
          char mon;
          char arg0[res];
          char arg1[res];
          char arg2[res];
          strcpy(arg0, "");
          strcpy(arg1, "");
          strcpy(arg2, "");
          res = sscanf( cmd, "%*c %s %s %s", arg0, arg1, arg2 ); 
          if (res == 0){
             day = current_dayy();
             mon = current_monthh();
             year = current_yearr();
             switch(mon){
                case 1:
                   printf("%d January %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 2:
                   printf("%d February %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 3:
                   printf("%d March %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 4:
                   printf("%d April %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 5:
                   printf("%d May %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 6:
                   printf("%d June %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 7:
                   printf("%d July %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 8:
                   printf("%d August %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 9:
                   printf("%d September %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 10:
                   printf("%d October %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 11:
                   printf("%d November %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 12:
                   printf("%d December %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                default:
                   printf("Invalid Month");
             }
             /*printf("%d %d \n", day, year); */
             return 0;
          }
       
          if (res == 1){
             mon = current_monthh();
             year = current_yearr();
             switch(mon){
                case 1:
                   printf("%d January %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 2:
                   printf("%d February %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 3:
                   printf("%d March %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 4:
                   printf("%d April %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 5:
                   printf("%d May %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 6:
                   printf("%d June %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 7:
                   printf("%d July %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 8:
                   printf("%d August %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 9:
                   printf("%d September %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 10:
                   printf("%d October %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 11:
                   printf("%d November %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                case 12:
                   printf("%d December %d is a %s \n", day, year, getday(day, mon, year));
                   week_day(day, mon, year);
                   break;
                default:
                   printf("Invalid Month");
             }
             /*printf("%d %d \n", day, year); */
             return 0;
          }
       
          if (res == 2){
             year = current_yearr();
             switch(month){
                case 1:
                   printf("%d January %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 2:
                   printf("%d February %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 3:
                   printf("%d March %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 4:
                   printf("%d April %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 5:
                   printf("%d May %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 6:
                   printf("%d June %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 7:
                   printf("%d July %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 8:
                   printf("%d August %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 9:
                   printf("%d September %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 10:
                   printf("%d October %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 11:
                   printf("%d November %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                case 12:
                   printf("%d December %d is a %s \n", day, year, getday(day, month, year));
                   week_day(day, month, year);
                   break;
                default:
                   printf("Invalid Month");
             }
             /*printf("%d %d \n", day, year); */
             return 0;
          }
       
          if ( res == 3 ) {
             switch(month){ 
                case 1:
                   printf("%d January %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 2:
                   printf("%d February %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 3:
                   printf("%d March %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 4:
                   printf("%d April %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 5:
                   printf("%d May %d is a %s \n", day, year, getday(day,month,year)); week_day(day, month, year); 
                   break; 
                case 6:
                   printf("%d June %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 7:
                   printf("%d July %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 8:
                   printf("%d August %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 9:
                   printf("%d September %d is a %s \n", day, year, getday(day,month,year)); week_day(day, month, year); 
                   break; 
                case 10:
                   printf("%d October %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 11:
                   printf("%d November %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 12:
                   printf("%d December %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                default :
                   printf("Invalid month\n"); 
                   return 0; 
             } 
          
          } 
          
          else{ 
             printf( "Bad day command parameters\n" ); 
             return 1;
          } 
       
       } 
    
    
    
               
       int do_month ( char *cmd ) { 
               
       
          int month, year; 
          int res; 
          res = sscanf( cmd, "%*c %d %d", &month, &year ); 
          if ( res == 2 ) { 
             switch(month){ 
                case 1:
                   printf("January\n"); 
                   break; 
                case 2:
                   printf("February\n"); 
                   break; 
                case 3:
                   printf("March\n"); 
                   break; 
                case 4:
                   printf("April\n"); 
                   break;
                case 5:
                   printf("May\n"); 
                   break; 
                case 6:
                   printf("June\n"); 
                   break; 
                case 7:
                   printf("July\n"); 
                   break; 
                case 8:
                   printf("August\n"); 
                   break; 
                case 9:
                   printf("September\n"); 
                   break; 
                case 10:
                   printf("October\n"); 
                   break; 
                case 11:
                   printf("November "); 
                   break; 
                case 12:
                   printf("December\n"); 
                   break; 
                default :
                   printf("Invalid month\n"); 
                   return 0; 
             } 
          } 
          else { 
             printf( "Bad month command parameters\n" ); 
          } 
          return 1; 
       } 
    
    
    
               
       void do_year ( char *cmd ) { 
               
          int year; 
          int res; 
          res = sscanf( cmd, "%*c %d", &year ); 
          if ( res == 1 ) { 
             printf("%d \n", year); 
          } 
          else { 
             printf( "Bad year command parameters\n" ); 
          } 
       } 
    
    
    
               
       int first_day_of_month ( int year, int month ) { 
               
       
          struct tm t = { 0 }; 
          struct tm *tp; 
          time_t now; 
          t.tm_mday = 1; 
          t.tm_mon = month-1; 
          t.tm_year = year-1900; 
          now = mktime(&t); 
          tp = localtime(&now); 
          return tp->tm_wday; 
       } 
    
    
    
               
       int main ( ) { 
               
       
          char buff[BUFSIZ]; 
       
       
          while ( fgets( buff, BUFSIZ, stdin )) {
             if (strlen(buff) <= 1) {
                break;
             }
             switch ( buff[0] ) { 
                case 'd':
                   do_day ( buff );  
                   break; 
                case 'm':
                   do_month ( buff ); 
                   return 0; 
                   break; 
                case 'y':
                   do_year ( buff ); 
                   return 0; 
                   break; 
                default:
                   printf( "Unknown command: %c\n", buff[0] ); 
                   return 0; 
                   break; 
             } 
          } 
          return 0; 
       }

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Duffy, please don't post your whole source, try post just the section you're having trouble with. It makes answering your questions a lot easier.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    ok im sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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