Why can't I use string.h?

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;

        
}

Thanks for any thoughts...

string.h is used for libc string functions in C or C++

string is used by C++ for string datatypes - change the include

not sure i understand... change the include to not have string.h?

i'm still learning my way here... thanks for your patience

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>

So, in your code try :

char cmdline[80] ;

:slight_smile: thanks for all your input. i really learn a lot from you guys.
cheers!

I am sure <string> is part of the C++ STL and is
not in C at all.

In C a string is const char* that is why <string> has
the c_str() function.

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.