Hi,
I'm not sure if this something that is completely over my head... but i'm trying to use #include string.h in my program to declare a variable "string cmdline;" but i keep on getting the message of cmndline not declared when i have declared it already. I know that there are differences in compiler version for OS and all...but this is driving me nuts not knowing what is missing and could be the problem.
The whole program is written in C++ and trying to compile it on Linux.
here's a snippet
#include <stdio.h>
#include <string.h> // C strings
#include <unistd.h> // for getopt
#include <alloc.h> // for free
#include <stdlib.h> //for setenv
//others declaration
int main(int argc, char **argv, char **envp)
{
unsigned long sleep_sec, sleep_micro, sleep_nano;
int ch;
pid_t proc_pid;
int pr_no = PR_INIT_VAL;
char mon_log[40];
char *pr_name = NULL, **cmdargs = NULL;
string cmdline;
char *pidfile = NULL;
//while loop
cmdline = optarg;
}
As far as I see, he is doing C, not C++, as the code suggests.
In C and C++ strings are treated differently. In C, there is no string data type, instead strings are treated as arrray of characters. So if you want to declare string in C you do :
char str[80] ; // declare character array(string) of length 80
gets(str) ; // reads a string that you type in command line
// or ;
char myString[] = "This is a C string" ; // myString is a char pointer to the above character string
In order to do that you include => # include <string.h>
On PDP's about 25 years ago, char * was a real workhorse - because we could cram more into memory. Just allocate a buffer and put whatever you want into it, a kind of poor man's struct.
As I remember this was also a way to get around some of the problems void * solved later on. I dunno. That may be what you're seeing.
I dropped out of Unix in 1980, came back in 2000. Which is probably why some of my answers appear odd.