The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 2 04-11-2008 11:32 AM
Extracting a string from one file and searching the same string in other files mohancrr Shell Programming and Scripting 1 09-19-2007 12:17 AM
Replace string B depending on occurence of string A hemangjani Shell Programming and Scripting 1 12-05-2006 02:10 PM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 05:53 AM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 03:11 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-22-2004
Registered User
 

Join Date: May 2004
Posts: 66
Question 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
Code:
#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...
Reply With Quote
Forum Sponsor
  #2  
Old 07-22-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
string.h is used for libc string functions in C or C++

string is used by C++ for string datatypes - change the include
Reply With Quote
  #3  
Old 07-22-2004
Registered User
 

Join Date: May 2004
Posts: 66
not sure i understand... change the include to not have string.h?

i'm still learning my way here... thanks for your patience
Reply With Quote
  #4  
Old 07-22-2004
milhan's Avatar
Registered User
 

Join Date: Oct 2002
Location: /home
Posts: 121
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 :
Code:
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 :
Code:
char cmdline[80] ;
Reply With Quote
  #5  
Old 07-27-2004
Registered User
 

Join Date: May 2004
Posts: 66
thanks for all your input. i really learn a lot from you guys.
cheers!
Reply With Quote
  #6  
Old 07-27-2004
Mastermind
Guest
 

Posts: n/a
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.
Reply With Quote
  #7  
Old 07-27-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0