• Welcome to Valhalla Legends Archive.
 

Type mis-match

Started by Eli_1, April 01, 2004, 01:39 PM

Previous topic - Next topic

Eli_1

I'm makin a quick proggy to move the mouse cursor to where ever you specify, and on compile I got 2 errors:

Quote
ERROR: 27: Cannot convert 'int' to 'const char *' in function main()
ERROR: 27: Expected 'int', got 'const char *' in function main()
I have no idea why I'm getting this error :(

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <winuser.h>

int main() {
   char inputBuffer[256], *p;
   long xPos = 0, yPos = 0;
   
   printf("Input the x and y position seperated by a comma and a space (-1, -1) to quit.\nExample: \"10, 203\"\n\n");

   while ( 1 ) {
      printf("Cursor positions: ");
      fgets(inputBuffer, 256, stdin);
      inputBuffer[ strlen(inputBuffer) - 1 ] = 0; // Remove '\n'

      if (strlen(inputBuffer) > 1) {
         p = strchr(inputBuffer, ', ');
         if (p == NULL) {
            printf("Invalid string format.\n");
            continue;
         } else {
            *p = 0;
            xPos = atoi(inputBuffer);
            p += 2;
            strcpy(inputBuffer, *p);
            yPos = atoi(inputBuffer); // <--- ERROR HERE :-(
            if (xPos == -1 && yPos == -1)
               break;

            SetCursorPos(xPos, yPos);
         }
      }
   }
   printf("Quiting.\n");
   return 0;
}

iago

hm, I thought you were using atoi wrong, but it looks ok:

QuoteNAME
      atoi,  atol, atoll, atoq - convert a string to an integer.

SYNOPSIS
      #include <stdlib.h>

      int atoi(const char *nptr);
      long atol(const char *nptr);
      long long atoll(const char *nptr);
      long long atoq(const char *nptr);

DESCRIPTION
      The atoi() function converts the initial  portion  of  the string  pointed  to  by nptr to int.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Eli_1

#2
Yea, that's what I checked when it first happened too, but note that it got past the first atoi() usage which is almost identical.

[Edit] Maybe I should be using atol() because my xPos and yPos are declared as longs? I'll try that and get back.

Answer: Nope... :(

Telos

I suggest using the SCAN Formatted function to get your input...

Zakath

The error is not with atoi(). It lies in your usage of the dereferencing operator *. On the previous line you attempted to copy from an int to a string. The syntax SHOULD be:


strcpy( inputBuffer, p );
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Eli_1

#5
That code is there because atoi(*p) didn't work either.

I thought *p ment the value of the variable, and p was the address in memory for it?  :-\

Add-on:
I looked at the prototype and it wants the pointer to it and not the actuall data, doesn't it?

Zakath

No, you misunderstood me. The error is not on the line with atoi, it's on the previous line where you misuse strcpy.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

vile

You shouldn't use strcpy(). It's vulnerable to buffer overflows.

Skywing

Quote from: vile on April 08, 2004, 02:00 PM
You shouldn't use strcpy(). It's vulnerable to buffer overflows.
strcpy is fine as long as you use it properly...

vile

#9
As long as you have some other way of checking the boundaries of the buffer, yes (a for loop checking the size of the buffer you're passing to most likely). But that is a lot more typing than using strncpy().