|
#1
| ||||
| ||||
| C/C++ pointers and strings Can someone please use human language to tell me how to use pointers to make a string. Maybe even write a short little line of code that shows me how to recieve input for a string. All the books I'm reading seem pretty cryptic to me when it comes to pointers being used for strings. Thanks in advance
__________________ Nathan Coffield Apple iBook; AMD 3400 3GB of RAM, 250GB SATA HD, 19" Wide Screen Monitor, Windows XP Home, VMWare running Slackware 11 for development purposes, Apache 2.2, PHP 5; Home Page |
|
#2
| |||
| |||
| There are 3 things to remember when using pointers with a string. The main thing is that your pointer needs to have a block of memory assigned to it - otherwise, your string will be stored at a random memory location and could easily damage some other data. The second thing is that this block of memory needs to be big enough to hold the string PLUS an extra byte for a NULL terminator (you might need a couple of extra bytes if you're using Unicode or some other multi-byte character string). So - what happens if you don't know how big the string will be? Just allocate a big enough block to hold the maximum size you think it will probably be and hope for the best....! The final thing to remember is that every block of allocated memory must be released once you've finished with it. In C, memory is allocated using 'malloc()' and released using 'free()'. In C++ it is allocated using the 'new' operator and released using 'delete[]'. Note the square brackets. Here's an example in C assuming a simple character string...... Code: #define MAX_STRING_SIZE 1024;
char* pStringBuffer = NULL;
pStringBuffer = (char *)malloc(MAX_STRING_SIZE);
/* 'malloc()' returns a pointer to void, so cast it as a pointer to char */
if (pStringBuffer)
{
strcpy (pStringBuffer, "Hello World");
/* Now do something with the string */
/* and finally... */
free (pStringBuffer);
} Code: #define MAX_STRING_SIZE 1024;
char* pStringBuffer = NULL;
pStringBuffer = new char[MAX_STRING_SIZE]; // Note the square brackets
if (pStringBuffer)
{
strcpy (pStringBuffer, "Hello World");
// Now do something with the string
// and finally...
delete[] pStringBuffer; // Note the square brackets again
} Incidentally, for most purposes it's easier to hold strings on the program stack. This is especially true if they're small and only being used temporarily - e.g. Code: char stringBuffer[] = "Hello World"; Hope that helps. |
|
#3
| ||||
| ||||
| OK now is there anyway to get a compiled program to run in the background but still recieve input from the keyboard? Like is there anyway that I can pull data from the ps2 port (on a pc) without anyone noticing?
__________________ Nathan Coffield Apple iBook; AMD 3400 3GB of RAM, 250GB SATA HD, 19" Wide Screen Monitor, Windows XP Home, VMWare running Slackware 11 for development purposes, Apache 2.2, PHP 5; Home Page |
|
#4
| ||||
| ||||
| Buy a keyboard snooping dongle and put it between the keyboard and the box. In software the answer is generally no but depending on the Os and level of "not noticing" required you could do something...
__________________ Wenn ist das Nunstruck git und Slotermeyer? Ja!... Beiherhund das Oder die Flipperwaldt gersput! |
|
#5
| ||||
| ||||
| There's a thing called an OS level hook that will do that on Windows(win32) but I haven't ever used it. It's not an easy thing to do, unless you've done a lot of win32 programming, but it's possible. There are many key logging programs for Windoze. BTW, I say windoze because you say you want to log from a PS2 port, so I assume you want to run a program on the windoze computer.
__________________ MacBook Pro 2.16GHz Core2Duo 3GB RAM, G4 1.4GHz OSX Tiger 1.25GB RAM, Dual 2GHz G5 OSX Tiger 2GB RAM (freakin shweet) Athlon 64 Windoze XP for school work (programming) 1GB RAM dferns@macosx.com |
|
#6
| ||||
| ||||
| That is true but I figured that that approach violated the "not noticing" part of the message. Before those APIs are availale to a program it has to regester itself with the windowing system and then it will show up in the list of running applications. It may be a background task but it will be there. (A similar thing is possible under OSX) To avoid that situation you would need to most likely go down to the level of a device driver and I am not sure if they can do things like request arbitrary event notifications. I think not, so you basically have to put your code into the driver for the keyboard which is really hard. One other problem with the OS hooks modle is that you are working at the level of UI events and are relying on the OS to deliver them to you. Now if some cases even if you have regestered to recieve all events the OS will not deliver them to you. In the case of X Windows a given window can request that it have a sevure connection with the server in which case those events are not published to just anyone who wants to listen. (As an example of this an xterm will let you request a secure keyboard connection.) I assume such a system would fail to snoop on the very keystrokes you were wanting to get. Or was this for honest purposes ;-) -Eric
__________________ Wenn ist das Nunstruck git und Slotermeyer? Ja!... Beiherhund das Oder die Flipperwaldt gersput! |
|
#7
| ||||
| ||||
| Wow typo city today. Sorry.
__________________ Wenn ist das Nunstruck git und Slotermeyer? Ja!... Beiherhund das Oder die Flipperwaldt gersput! |
|
#8
| |||
| |||
| *edited* Last edited by wiz; October 10th, 2003 at 11:34 AM. |
![]() |
| Thread Tools | |
|
|