ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hey guys, so i am writing a program in c for ubuntu, but the problem is, i have never really done dev. for linux before. my c++ experience was pretty much limited to dev-c++ on windows. a couple things came up upon compilation of my program, included among them:
-the type 'string'. for some reason, when i try to declare a variable as type string, it denies me. like this:
string variable;
it gives an error message. is there another word for it in unix?
-the command-line user input prompt function: i used
cin >> variable;
just because that seemed reasonable, but it seems that
cout << "hello world!";
is different in unix, too, so what is the function for getting user input? (i want to have them enter a string in the terminal. this is just for testing purposes, so it doesnt have to be fancy)
And if you are indeed programming in C (not C++), there is no such thing as "string"---the closest thing you get is an array of chars the size of the "string" you put there. See the various C manuals for more information about this..if you're doing it in C, you probably have your reasons (as opposed to using C++ which has more fancy features like those strings for example), but then again you should get to know the language before trying to do something with it. C is a very little language, and a lot that you've accustomed to in C++ isn't there.
Another thing is that you should never use
Code:
using namespace std;
in a C++ application. It's simply bad practise and works only in very very small apps (i.e. "hello world") where you are aware of the whole code at all times; in any bigger program it's a potential trap.
hmm... thats too bad, no such thing as strings in c? the reason that i am programming this in c is the original code that it is based off of was written in c, i am really just tailoring it to fit my needs. so is there a way to just convert a c program into c++? i feel like that probably would not go that well, but then again i am not so clear on the differences. (other than that c++ clearly has more to it)
so what about gathering user input? cin >> does not work, in c at least.
really? so there is no way to get user input in c?
How does saying there are no streams translate to no way to get user input? Of course you can get user input in C, just not as conveniently as in C++.
There are various forms of "gets" function for reading a line of input and/or you can use ordinary file reading functions to read from stdin. Then you can parse the text that you read.
There are also scanf functions in C that both read input and parse it. Those are seriously flawed in several ways and difficult to learn and a major source of beginner errors in C programming. Personally, I would stay away from the whole family of scanf functions and use more basic file I/O operations on stdin, followed by parsing.
But since you know some C++ and know how to use streams, it would be easiest for you to mix C and C++. Write any new functions that get user input in C++.
so when you say 'mix,' what exactly are you thinking?
"Mix" would mean having more than one source file, with C code in a .c file and C++ code in a .cpp file, and using the extern "C" feature of C++ to declare both C functions to be called from C++ code and C++ functions to be called from C code.
Quote:
just converting the whole file to a .cpp extension, adding in some c++ code and compiling it?
That's an alternate approach. Change the extension to .cpp and try compiling the existing C code as C++. Fix whatever details go wrong (hopefully not too many) then you can put new C++ code anywhere in the resulting file.
i tried just switching the file extension name from .c to .cpp and three errors came up
That's pretty good. Hopefully (and probably) all the errors are compile time, not run time, so you are almost there.
Quote:
invalid conversion from ‘void*’ to ‘buffer*’
That is one of the rules of C++ that is different from C. In C++ you need an explicit cast to convert from void* to another pointer type. If the original C code was correct, then the error message even tells you the type you need to cast to.
// This is C++ ... but you tried compiling it as a C program
std::string mystring;
cin >> mystring;
cout << "hello world!";
Code:
/* This is C-ish .. but you've put it in a .cpp file */
static void
init_read (unsigned int buffer_size)
{
buffers = calloc (1, sizeof (*buffers));
if (!buffers) {
fprintf (stderr, "Out of memory\n");
exit (EXIT_FAILURE);
}
buffers[0].length = buffer_size;
buffers[0].start = malloc (buffer_size);
if (!buffers[0].start) {
fprintf (stderr, "Out of memory\n");
exit (EXIT_FAILURE);
}
}
thanks for all the info guys, i really appreciate it. i am still running in to a few issues though, and since you have been so helpful so far, i thought i might bring it up:
so i decided that i would program this in c because of the other things it will need to interface with, and i just feel more comfortable with it. one thing i am not so clear on is strings of characters. i am modifying someone else's code, mind you, so this is not all code that i just randomly wrote. so take a look at this:
i define 'dev_name' like so:
Code:
static char * dev_name = NULL; //will hold the filepath to a device file
then later on, in the 'main', i assign it like this:
Code:
dev_name = "/dev/video0";
now for some reason, C++ does not allow me to make a string of characters like that. the error it gives me is
"warning: deprecated conversion from string constant to 'char*'"
how do i get around this? it is somewhat important to the program that this remains as a string of characters, as opposed to a std::string- it gets called by stat() and fprintf() and the like a few times, and they are not too friendly from what i can tell.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.