473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Strings inside arrays

Hi,
First, Sorry if the title was bad, I really didn't know how to describe
this.
So,
What I need to do is something like this:
char somevariable[256]; // The tricky part. I don't know how to define
it.
somevariable[1] = "Hello";
somevariable[2] = "Goodbye";
And to print them using:
printf(somevariable[1]);
and
printf(somevariable[2]);

Hope I was describing this enough.
Thanks, LinuxDuud.

Jan 16 '07 #1
8 1630

"LinuxDuud" <ng****@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
Hi,
First, Sorry if the title was bad, I really didn't know how to describe
this.
So,
What I need to do is something like this:
char somevariable[256]; // The tricky part. I don't know how to define
it.
somevariable[1] = "Hello";
somevariable[2] = "Goodbye";
And to print them using:
printf(somevariable[1]);
and
printf(somevariable[2]);

Hope I was describing this enough.
Thanks, LinuxDuud.
#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::string;
using std::vector;

int main()
{
vector<stringtext;

text.push_back("Hello");
text.push_back("The C++ standard library has many easy to use,");
text.push_back("robust and versatile types and algorithms. Use them.");
text.push_back("Goodbye");

for(vector<string>::size_type i = 0; i < text.size(); ++i)
cout << text[i] << '\n';

return 0;
}

-Mike
Jan 16 '07 #2
On 16 Jan 2007 09:56:29 -0800, "LinuxDuud" <ng****@gmail.comwrote in
comp.lang.c++:
Hi,
First, Sorry if the title was bad, I really didn't know how to describe
this.
So,
What I need to do is something like this:
char somevariable[256]; // The tricky part. I don't know how to define
it.
somevariable[1] = "Hello";
somevariable[2] = "Goodbye";
And to print them using:
printf(somevariable[1]);
and
printf(somevariable[2]);

Hope I was describing this enough.
Thanks, LinuxDuud.
Somebody else already showed you a way to use C++ vectors and streams
to do what you want, completely ignoring the fact that you might have
a good reason to use C style strings and the printf() function. And
you should prefer vectors unless there is some reason that you cannot.

But to answer your original question:

const char *somevariable [256] =
{
"Hello",
"Goodbye",
/* up to 254 more string literals */
};

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 16 '07 #3

Mike Wahler wrote:
"LinuxDuud" <ng****@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
Hi,
First, Sorry if the title was bad, I really didn't know how to describe
this.
So,
What I need to do is something like this:
char somevariable[256]; // The tricky part. I don't know how to define
it.
somevariable[1] = "Hello";
somevariable[2] = "Goodbye";
And to print them using:
printf(somevariable[1]);
and
printf(somevariable[2]);

Hope I was describing this enough.
Thanks, LinuxDuud.

#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::string;
using std::vector;

int main()
{
vector<stringtext;

text.push_back("Hello");
text.push_back("The C++ standard library has many easy to use,");
text.push_back("robust and versatile types and algorithms. Use them.");
text.push_back("Goodbye");

for(vector<string>::size_type i = 0; i < text.size(); ++i)
cout << text[i] << '\n';
With such a message I would think your loop might look more like this:

std::copy(text.begin(), text.end(),
std::ostream_iterator<std::string>(cout, "\n"));
>
return 0;
}

-Mike
Jan 16 '07 #4

"Noah Roberts" <ro**********@gmail.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
>
Mike Wahler wrote:
>>
for(vector<string>::size_type i = 0; i < text.size(); ++i)
cout << text[i] << '\n';

With such a message I would think your loop might look more like this:

std::copy(text.begin(), text.end(),
std::ostream_iterator<std::string>(cout, "\n"));
:-)
Actually, I indeed almost did exactly that.

But I thought better of it, since OP seems to obviously
be a C++ beginner. I Didn't want to overload (pun intended)
him with too much info right away.

-Mike
Jan 16 '07 #5

Jack Klein wrote:
Somebody else already showed you a way to use C++ vectors and streams
to do what you want, completely ignoring the fact that you might have
a good reason to use C style strings and the printf() function. And
you should prefer vectors unless there is some reason that you cannot.
I think so, that printf() can be used in some cases if you well know
the function, but you can get wrong output, if you will mix
"std::stdout" and "std::cout" streams. To avoid backside effects use
"printf" and "<<" into separate streams, for example, do printf into
stderror or file and << into cout.

Jan 17 '07 #6
On Tue, 16 Jan 2007 21:19:35 -0800, Grizlyk wrote:
Jack Klein wrote:
>Somebody else already showed you a way to use C++ vectors and streams
to do what you want, completely ignoring the fact that you might have
a good reason to use C style strings and the printf() function. And
you should prefer vectors unless there is some reason that you cannot.

I think so, that printf() can be used in some cases if you well know
the function, but you can get wrong output, if you will mix
"std::stdout" and "std::cout" streams. To avoid backside effects
....please adjust your diet ;) Seriously though, not quite sure what you
intended there.
use "printf" and "<<" into separate streams, for example, do printf into
stderror or file and << into cout.
Is that necessary? I think a call to std::ios_base::sync_with_stdio(true)
should synchronise the standard C I/O facilities (stdout, etc.) with their
standard C++ equivalents (std::cout, etc.).

(Indeed it used to be [still is?] recommended that if you are using one or
the other of C or C++ I/O facilities then it might be a good idea to call
sync_with_stdio(false), since synching I/O modes might entail a substantial
speed/size overhead).

--
Lionel B
Jan 17 '07 #7
Lionel B wrote:
Is that necessary? I think a call to std::ios_base::sync_with_stdio(true)
should synchronise the standard C I/O facilities (stdout, etc.) with their
standard C++ equivalents (std::cout, etc.).
They are sync'd by default; you would only call that function if you
wanted to unsync them.

Jan 17 '07 #8
On Wed, 17 Jan 2007 14:12:29 -0800, Old Wolf wrote:
Lionel B wrote:
>Is that necessary? I think a call to std::ios_base::sync_with_stdio(true)
should synchronise the standard C I/O facilities (stdout, etc.) with their
standard C++ equivalents (std::cout, etc.).
Yep, sorry, that's the wrong way round...
They are sync'd by default; you would only call that function if you
wanted to unsync them.
For that reason it was often recommended that you explicitly *un*-sync them
with std::ios_base::sync_with_stdio(false) for better I/O performance if
you were not intending to mix C and C++ style I/O in the same program.

--
Lionel B
Jan 18 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
9
by: Rafi Kfir | last post by:
Hi, This may look as a smiple task to most of you, but to me (a beginner with C), it drives me crazy. All I want is that one function passes a two dimensional array of strings to another...
5
by: Robert | last post by:
Hi, Is there some way of using an array of strings? Like in basic? I know you have to create an array of chars so i think it has to be an 2d array or something... Really stuck here... Thanks...
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
5
by: shyam | last post by:
Hi All I have to write a function which basically takes in a string and returns an unknown number( at compile time) of strings i hav the following syntax in mind char *tokenize(char *) ...
9
by: Bilgehan.Balban | last post by:
Hi, For a declaration such as: char * mystring = "ABCDabcd123"; Is it a linker issue where such strings are stored in C, or is it defined as part of the language definition? Is there any...
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
19
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.