Explicit cast to convert

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dinklebaga
    New Member
    • Feb 2009
    • 14

    Explicit cast to convert

    Hi All,
    I am writing a program at the moment that will allow me to manually read sectors on a floppy disk and have come into a bit of bother. The program is designed to run in a DOS32/16 environment so I am using the digital mars c compiler compiling c and some inline assembly language with the parameters 'dmc filename -msd', but i am having issues with the compiler giving me the error: explicit cast to convert parameter 1 from int* to int when trying to give a memory location to a pointer. I believe the problem may lie with the compiler restricting these operations because they may be considered dangerous but they are needed to directly reference the memory. Can anybody offer me any advice?

    The section of code giving me bother is:

    int *memsectptr
    memsectptr = (int *)0x1000;
    for(i=0; i<(sectorsize) ; i++)
    {
    fputc(*(memsect ptr+i), outfileprev); /*get memory values before use*/
    }

    Thanks,
    Dinklebaga
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    What is the type of variable "i"?
    Specifically which line is indicated in your error message?
    Are you getting a compiler error or a compiler warning?

    *(memsectptr+i) uses pointer arithmetic style. This looks ok assuming "i" is an int.

    It doesn't solve your problem, but depending on the rest of your program you might prefer this declaration:
    Code:
    int * const memsectptr = (int*) 0x1000;
    The difference here is that the value of the pointer is initialized to 0x1000 and the compiler will not allow you to change it.

    Comment

    • dinklebaga
      New Member
      • Feb 2009
      • 14

      #3
      Hi Donbock,
      Firstly thankyou for replying. Yes i is set as an integer just simply to hold the value of the sectorsize (512, 2048, etc.) but what i am getting is compiler errors which stop me from compiling the program. The section of code that i have posted is actually only a small section of the entire program but it is this section which is causing me grief. Like you have observed I do want to have the value of the pointer set to 0x1000 but I then want to access the data that is stored at the pointer (the 512 bytes that start at the pointer). Usually i would use malloc but because the program is partly in asm I need to reference the memory location directly.

      Thanks,
      Dinklebaga

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Does the error message refer to a specific line number? If so, which line is it?

        I assume that the error is either for the line where you assign 0x1000 to memsectptr or to the line where you call fputc.

        If the error is for the call to fputc you might try declaring a new int* variable, assigning memsectpr+i to that new variable, and then passing that variable to fputc.

        Comment

        • dinklebaga
          New Member
          • Feb 2009
          • 14

          #5
          The error is occurring on the fputc line. I have created a new pointer (int * tempptr) and then assigning the value of that to the fputc statement like so:

          fputc(tempptr, outfile);

          but it still kicks out an error and won't compile. The only time it will let me compile is when it is written like this:

          fputc(*tempptr, outfile);

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dinklebaga
            The error is occurring on the fputc line. I have created a new pointer (int * tempptr) and then assigning the value of that to the fputc statement like so:

            fputc(tempptr, outfile);

            but it still kicks out an error and won't compile. The only time it will let me compile is when it is written like this:

            fputc(*tempptr, outfile);
            No surprise: the fputc() function takes an int as its first parameter, not a pointer.

            kind regards,

            Jos

            Comment

            • dinklebaga
              New Member
              • Feb 2009
              • 14

              #7
              Hi Jos,
              Thanks for your quick response but unfortunately this still isn't solving my issue. I input a new int tempval and replaced tempptr with this but once again i am getting compiler errors being kicked out but on the line previous to the fputc statement:

              for(i=0; i<(sectorsize * sectorct); i++)
              {
              tempval = memsectptr+i;
              fputc(tempval, outfile);
              }
              complaining of the explicit cast to convert int * to int

              Thanks,
              Dinklebaga

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dinklebaga
                Hi Jos,
                Thanks for your quick response but unfortunately this still isn't solving my issue. I input a new int tempval and replaced tempptr with this but once again i am getting compiler errors being kicked out but on the line previous to the fputc statement:

                for(i=0; i<(sectorsize * sectorct); i++)
                {
                tempval = memsectptr+i;
                fputc(tempval, outfile);
                }
                complaining of the explicit cast to convert int * to int

                Thanks,
                Dinklebaga
                You're doing it again: memsectptr+i is a pointer value; fputc() takes an int as its first parameter. Maybe you want memsectptr[ i ]

                kind regards,

                Jos

                Comment

                • dinklebaga
                  New Member
                  • Feb 2009
                  • 14

                  #9
                  Thanks Jos, that seems to have cured one of the problems with the code but I am still not receiving the correct output from the program. In the ASM code I am explicitly reading the sector to 0x1000 from the floppy disk but i am not getting different results from when i run the fputc function before and after the asm is run (into different files to compare) which leads me to assume that the C code is not referencing the memory correctly

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    C doesn't make 'mistakes' like that; more likely nothing is read from the floppy disk and no memory contents is changed.

                    kind regards,

                    Jos

                    Comment

                    • dinklebaga
                      New Member
                      • Feb 2009
                      • 14

                      #11
                      I'm not sure where the code is wrong then because it is the boot sector that is being read by the asm code and i have downloaded a hex editor to view what code is actually in the sector and sure enough it is there?
                      Would it be handy to post the program as a whole?
                      Dinklebaga

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by dinklebaga
                        I'm not sure where the code is wrong then because it is the boot sector that is being read by the asm code and i have downloaded a hex editor to view what code is actually in the sector and sure enough it is there?
                        Would it be handy to post the program as a whole?
                        Dinklebaga
                        Sure, feel free to post the relevant parts because it's no use to keep on guessing.

                        kind regards,

                        Jos

                        Comment

                        • dinklebaga
                          New Member
                          • Feb 2009
                          • 14

                          #13
                          int status;
                          char * const memsectptr = (char *) 0x1000;
                          int *tempptr;
                          int sectorsize = 512;
                          int sectorct = 1;
                          int i;
                          FILE *outfile;
                          FILE *outfileprev;
                          outfileprev = fopen("hexdumpp rev.txt", "wb");
                          outfile = fopen("hexdump. txt", "wb");
                          memset(memsectp tr, '\0', sectorsize);

                          for(i=0; i<(sectorsize) ; i++)
                          {
                          fputc(memsectpt r[i], outfileprev); /*get memory values before use*/
                          }

                          asm
                          {
                          mov bx,0x1000;
                          mov es,bx;
                          mov bx,0;
                          mov ah,02;
                          mov al,01;
                          mov ch,01;
                          mov cl,01;
                          mov dh,01;
                          mov dl,00;
                          int 0x13;
                          }

                          for(i=0; i<(sectorsize * sectorct); i++)
                          {
                          fputc(memsectpt r[i], outfile);
                          }
                          fclose(outfilep rev);
                          fclose(outfile) ;
                          printf("\nreach ing\n");
                          printf("reachin g\n");
                          printf("reachin g\n");
                          return(0);

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            This is just a wild guess but try this:

                            Code:
                                char * const memsectptr = (char *) 0x10000;
                            Your assembly language snippet wants a segment of 0x1000; in those old 16 wide registers those segment registers were shifted to the left by four bits to form the actual address. Just a wild guess; your final address becomes 0x10000. I find that absolute address fiddling extremely tricky.

                            kind regards,

                            Jos

                            Comment

                            • dinklebaga
                              New Member
                              • Feb 2009
                              • 14

                              #15
                              Jos,
                              After adding in the new value (0x10000) I was still obtaining the same results so to see what was happening with the pointer i added in some printf statements to read out the value of th pointer:

                              printf("\nmemse ctptr = %c\n", memsectptr);
                              printf("memsect ptr = %x\n", memsectptr);
                              printf("memsect ptr = %d\n", memsectptr);

                              before on the hex value i was getting 1000 and on the decimal value 4096 but when i changed it to the new value it came out for both at zero?

                              Comment

                              Working...