I am getting many many warnings of this kind in my code. It comes every time I typecast something in my code, which I am doing to variable addresses in order to use integer arithmetic on them. How do I fix these warnings? Thank you!


I would appreciate any constructive criticism of my code, but please don't come here to simply tell me I am doing things in a stupid way, as that has been my experience when asking for help on here, and it is unhelpful. I have only just recently started programming and have very limited experience. Thank you for being kind : )

#include <stdio.h>
#include <string.h>

void printmenu()
{
     printf("\n\n(1) Search file for an ascii value 0-255.\n\
(2) Search file for a string (up to 30 chars).\n\
(3) Display data at a given address.\n\
(4) Quit.\n\n");
}




int main(int argc, char *argv[])
{
    // variable declarations
    FILE *fptr;
    int i, j, k, length=0, command=0, difference, placement, spot=0, index;
    unsigned int start_address, l, address, difference2, difference3, string_length;
    char junkholder, char_search[1], string[30];
    const char inputerror[] = "Please input argument in the form: \
(executable) (filename)\n";

    // Error checking on input    
    if (argc != 2)
    {
         printf("Error in input. %s", inputerror);
    }
    if ((fptr=fopen(argv[1],"r")) == NULL)
    {
         printf("Error. File cannot be opened.\n\n");
         //return 1;
    }
    
    
    // Here, I measure the length of the file
    while(junkholder!=EOF)
    {
         junkholder=getc(fptr);
         if(junkholder!=EOF) length++;
    }
    
    
    // run through the file and save contents
    char file[length]; // must declare it here, bc didn't know length
    rewind(fptr); // reset file pointer to beginning
    
    
    // saves file in memory and resets file pointer
    for(i=0;i<length;i++)
    {
         file[i]=getc(fptr);
    }
    rewind(fptr);
    file[length]='\0';
    
    
while(command != 4) // keep program running
{   
    printmenu();
    printf("Enter command #: ");
    scanf("%d",&command); fflush(stdin);
    // Error checking on input of command
    if(command<1 || command>4)
    {
         printf("Error. Not an option.\n\n");
         continue;
    }
    
    
    switch(command)
    {
    case 1:
         printf("Enter the character to be searched for: ");
         scanf("%s",char_search); fflush(stdin);
         if(strlen(char_search)!=1)
         {
              printf("Error. Please only enter one character.\n");
              break;
         }
         
         for(k=0;k<length;k++) // search whole file
         {
         if(file[k]==char_search[0]) // if this spot is what was entered
         {    //start_address holds the closest modulo 16 # to the target #
              start_address=(unsigned int)&file[k]-((unsigned int)&file[k]%16);
              difference=(unsigned int)&file[k]-(unsigned int)start_address;

              
              // this prints off the top line
              printf("\n\t\t   ");
              for(i=0;i<16;i++)
              {
                   printf("%d   ",(start_address-start_address/10*10));
                   start_address++;
              }
              start_address=(unsigned int)start_address-16;
              
              
              // this prints off the second line
              printf("\n[%u]\t[  ", start_address);
              for(i=(int)difference;i>0;i--)
              {
                   if(file[k-i]<33 || file[k-i]==127 || &file[k-i]<&file[0]) printf("    ");
                   else printf("%c   ",file[k-i]);
              }
              for(i=0;i<(16-difference);i++)
              {
                   if(file[k+i]<33 || file[k+i]==127 || &file[k+i]>=&file[length]) printf("    ");
                   else printf("%c   ",file[k+i]);
              }
              printf("]\n");
              
              
              // this prints off the third line
              printf("\t\t   ");
              for(spot=(k-difference), i=0;i<16;i++)
              {
                   if(&file[spot]<&file[0] || &file[spot]>=&file[length]) printf("00  ");
                   else printf("%2X  ",file[spot]);
                   spot++;
              }

              
              
              // this prints off the last line
              printf("\n\t\t   "); // new line
              /*for(spot=(k-difference), i=0; i<16; i++)
              {
                   if(&file[spot]<&file[0] || &file[spot]>=&file[length]) printf(".   ");
                   else if(file[spot]==file[k]) printf("^   ");
                   else printf("    ");
                   spot++;
              }*/
              for(i=difference;i>0;i--)
              {     
                    if(&file[k-i]<&file[0])
                        printf(".   ");
                    else printf("    ");
              }
              printf("^   ");
              for(i=0;i<16-difference;i++)
              {
                    if(&file[k+i]>=&file[length-1])
                        printf(".   ");
                    else printf("    ");
              }
              printf("\n\n");
         }
         }
         break;
    case 2:
         printf("Enter a string to search for (max 30 characters: ");
         scanf("%s",string);
         
         
         // error checking for input
         if(strlen(string)>30)
         {
              printf("\nError. String too long.\n\n");
              break;
         }
         
         
         // set the NULL character
         string_length=strlen(string);
         string[string_length]='\0';
         

         // error checking if string isn't in file
         if (strstr(file, string)==0)
         {
              printf("\nError. String does not occur in file.\n\n");
              break;
         }
         
         
         // find index of first occurence
         index=strstr(file,string)-(int)&file[0];
         
         printf("\nindex=%d\n\n",index);
         
         
         // calculate closest modulo 16 address
         start_address=(unsigned int)&file[index]-(unsigned int)&file[index]%16;
         printf("\nstart address = %u\n\n", start_address);    

         break;
    case 3:
         // input the address
         printf("\nEnter an address value between %u and %u\n\n", (unsigned int)\
         &file[0], (unsigned int)&file[length-1]);
         scanf("%u",&address);
         
         
         // Error checking for input
         if(address<(int)&file[0] || address>(int)&file[length])
         {
              printf("\nError. Not in the given range.\n\n");
              break;
         }
         
         
         // find the index of the address
         for(i=0;i<length;i++) if((int)&file[i]==address) spot=i;
         printf("\n\n%d %c\n\n", spot, file[spot]);

         
         // calculate closest modulo 16 address
         start_address = address - (address%16);
         difference3 = address - (int)start_address;
         
         
         // print off first line of output
         printf("\n\t\t   ");
         for(l=start_address;l<(start_address+16);l++)
               printf("%d   ",l-l/10*10);
         printf("\n");
         
         
         // printf off the second line
         printf("[%u]\t[  ", start_address);
         for(j=difference3;j>0;j--)
         {
              if(file[spot-j]<33 || file[spot-j]==127 || &file[spot-j]<&file[0]) printf("    ");
              else printf("%c   ", file[spot-j]);
         }
         for(j=0;j<(16-difference3);j++)
         {
              if(file[spot+j]<33 || file[spot+j]==127 || &file[spot+j]>=&file[length]) printf("    ");
              else printf("%c   ",file[spot+j]);
         }
         printf("]\n");
         
         
         // printf off the third line
         printf("\t\t   ");
         for(j=(spot-difference),i=0;i<16;i++)
         {
              if(&file[j]<&file[0] || &file[j]>&file[length]) printf("00  ");
              else printf("%2X  ",file[j]);
              j++;
         }
              
         
         // printf off the last line
         printf("\n\t\t   ");
         for(i=difference3;i>1;i--)
         {     
              if(&file[spot-i]<&file[0])
                   printf(".   ");
              else printf("    ");
         }
         printf("^   ");
         for(i=0;i<16-difference3;i++)
         {
              if(&file[spot+i]>=&file[length])
                   printf(".   ");
              else printf("    ");
         }
         printf("\n\n");
         
         
         
         
         break;
    }         
         
}
   
return 0;
}

Warnings:

b.c: In function âmainâ:
b.c:87: warning: cast from pointer to integer of different size
b.c:87: warning: cast from pointer to integer of different size
b.c:88: warning: cast from pointer to integer of different size
b.c:180: warning: cast from pointer to integer of different size
b.c:180: warning: assignment makes integer from pointer without a cast
b.c:186: warning: cast from pointer to integer of different size
b.c:186: warning: cast from pointer to integer of different size
b.c:192: warning: cast from pointer to integer of different size
b.c:192: warning: cast from pointer to integer of different size
b.c:198: warning: cast from pointer to integer of different size
b.c:198: warning: cast from pointer to integer of different size
b.c:206: warning: cast from pointer to integer of different size
b.c:20: warning: unused variable âdifference2â
b.c:19: warning: unused variable âplacementâ

try printing out the sizeof(void*)
and the sizeof(unsigned int);

Are the values the same?

Edited 6 Years Ago by gerard4143: n/a

On a unix server (ssh), the void pointer is 8 bytes while the unsigned int is 4.

On my IDE (dev-c++), they are both 4.

So am I truncating 4 of the bytes when I typecast it?
How to I avoid doing so? Should I change their type to begin with? What to?

On a unix server (ssh), the void pointer is 8 bytes while the unsigned int is 4.

On my IDE (dev-c++), they are both 4.

So am I truncating 4 of the bytes when I typecast it?
How to I avoid doing so? Should I change their type to begin with? What to?

Try unsigned long

sizeof(void*);
sizeof(unsigned long);

Edited 6 Years Ago by gerard4143: n/a

This article has been dead for over six months. Start a new discussion instead.