gcc does not string as a type

Asked by FRU

Good day All.
I have installed Ubuntu 9.04 and the necessar packages for compilation.
However, when I try to compile a simple program that has string type declaration, a compiler error is pasted:
===========================

trout.c:7: error: ‘string’ undeclared (first use in this function)
===========================

What is the package I need to install to compile easily. I know an option is to declare char * instead. But I want to have to handle simple types without bothering about buffer overflows.
What other thing can I do to compile easily.

Thanks for all the advise you can provide.

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Arnaudus (a-lerouzic) said :
#1

Hi,

First you need to include the corresponding header (#include <string>), and then you have to remind that string is in the std namespace, so either add the namespace std at the beginning of your file; or call std::string.

In any case,

* Always include a reproducible example when you ask a question, people cannot guess what was in your code
* Find a C++ mailing list
* Buy a good C++ book, this is a trivial question and you might not get any answer if you don't look in a book or search the web before.

Revision history for this message
FRU (frungang) said :
#2

Hi Arnaudus.
Please I am compiling a C program and not C++. Even though I included string (#include <string.h.), I still have the problem.THis probelm is peculiar only to standard gnu c compilers. I have the same problem when I installed a gnu c compiler for Windows. I did not have it when I compiled codes using Debian.

What package must I install to make gcc recognize string as a type?

Thanks for your answer anyway. I

Revision history for this message
Arnaudus (a-lerouzic) said :
#3

Hi,

Sorry for the misunderstanding, read carefully your question and you will realize that you never refer to C. From the "string" type I deduced you were trying to compile a C++ code. If you had copied and pasted your code, it would have been much clearer.

Anyway, I am quite confused about your answer. string.h is a standard header ( http://en.wikipedia.org/wiki/String.h ), and as far as I know, there is no string type defined in it. Using char* is not an alternative in C, char* IS the regular C string. Of course, you may find additional libraries that provide non-standard string types that are a bit more user-friendly than the default C strings, but you will not find them by including string.h...

Finally, Debian is a Linux distribution that is very close to Ubuntu. It is likely that you've used gcc as well. No standard version of any C compiler would include a "string" type in "string.h", are you sure you're not confusing several things?

Revision history for this message
FRU (frungang) said :
#4

Hi Arnaudus.
Please find below the source code I am trying to compile.
I agree with you that the standard recent versions of c do not have string as a data type. However, it is still being used in C programming text books. I have quite a few.
My code is:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{

 string chat;
 chat = "It is a brave new world";
printf(%s\n, &chat);
return 0;
}

Revision history for this message
Antonio Litterio (antonio-litterio-gmail) said :
#5

Dear FRU if I remember that in C there isn't the special word "string", you can use some functionalities like getting string length, copying strings but you always use type "char" with array of char "char[10]" if you previously know its length, and with dynamic allocation "malloc" if you don't know it.
If you want use type string you must use C++.

Regards
Antonio

Can you help with this problem?

Provide an answer of your own, or ask FRU for more information if necessary.

To post a message you must log in.