![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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;
}
|
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
string.h is used for libc string functions in C or C++
string is used by C++ for string datatypes - change the include |
|
#3
|
|||
|
|||
|
not sure i understand... change the include to not have string.h?
i'm still learning my way here... thanks for your patience |
|
#4
|
||||
|
||||
|
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 So, in your code try : Code:
char cmdline[80] ; |
|
#5
|
|||
|
|||
|
cheers! |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |