declaring an array of pointers to string

June 22, 2010 at 13:05:46
Specs: Windows 7
print out the string's of an array of pointers to string

See More: declaring an array of pointers to string

Report •

#1
June 22, 2010 at 14:09:01
#include<iostrema>
#include<string>

using namespace std;

void printArray( string sList[], int size){


for (int i = 0; i < size; i++)
cout << sList[i] << endl;

}

int main() {

string array1[] = { "tom", "bob"};
printArray(array1,2};

system("pause");

return 0;
} setting up a string is no problem

setting up an array of pointers to string is the problem I am having.


Is this correct: char * arrayofstrings pointers[10];


Report •

#2
June 22, 2010 at 14:17:43
No, the space between "arrayofstrings" and "pointers" will cause the C++ complier to choke.

Report •

#3
June 22, 2010 at 16:11:58
when declaring an array of pointers to a string:


void pritnStrList(string * sList[], int size) {


then in
int main(){


I get an error:

string * sarray[] = {"paul', 'rich"};


Report •

Related Solutions

#4
June 22, 2010 at 16:16:20
string * sarray[] = {"paul", "rich"};

cannot convert const char to std:string.


Report •

#5
June 22, 2010 at 16:29:13
This is true; you're trying to assign the address of two const cstrings to the pointers of string objects. This is not allowed.

Report •

#6
June 22, 2010 at 17:07:05
In reponse #1 I didn't have an * with the string, now I have an
asterist, and now I have a cstring vs a string array. I don't understand this


Report •

#7
June 22, 2010 at 17:33:40
char message [] = {'' ", " "," \0"};

How do you initialize an array pointers to string???


Report •

#8
June 22, 2010 at 18:05:08
I think you're confused; you should probably sit down with your professor and ask him what he wants. He's bound to have a better idea of what's expected of you than me blindly guessing your class lessons.

Report •

#9
June 22, 2010 at 18:05:13
??

string sarray[] = {"paul", "rich"};

Edit:

Razor2.3 is right as usual, if you can't settle on a data type how can you possibly get anything done?


Report •

#10
June 22, 2010 at 18:26:01
string sarray[] = {"paul", "rich"};

is an array of strings.

Now, when I put the asterisk in between string and sarray, it turns into a cstring. From what you are telling me.


Report •

#11
June 22, 2010 at 18:28:14
look at response #1

Report •

#12
June 22, 2010 at 19:19:54
Initialise the array numerically?

string sarray[10][10];


Report •

#13
June 22, 2010 at 20:15:29
Now, when I put the asterisk in between string and sarray, it turns into a cstring. From what you are telling me.
No; no one said that at all. That would create an array of pointers to string objects.

Report •

#14
June 23, 2010 at 08:59:39
looking at response #1 , how would I change that program from an array of strings, to an array of pointers to strings.

Report •

#15
June 23, 2010 at 09:16:37
string * names[] pointers to string objects

array of pointers to string



Report •

#16
June 23, 2010 at 09:56:17
Changing from an array of strings, to an array of pointers to
strings: that's slightly complicated. But why would you want
to do that anyway? Are you sure it's not an array of cstrings
you want (where a cstring is a pointer to char)?

If you are not entirely certain what the difference is between a
string (i.e. a class from the C++ Standard Library) and a
classic C string (i.e. a char *) then you really should read up
on both until you are perfectly clear before going any further. It
will save you a lot of confusion and time in the long run.


Report •

#17
June 23, 2010 at 13:32:02
It is array of pointer to strings.

string *ptrs[];

I use a for loop:

for(int i = 0; i < size: i++){


Report •

#18
June 23, 2010 at 15:08:19
to print out the strings.

#include<iostream>
#include<string>
using namespace std:

void PrintPtrList(string * sList[], int size){

for (int i = 0; i < size; i++){

cout << *(sList[i]) << endl;

}

int main() {

string * ptrsarray [] = {"pete", "rose"};
PrintPtrList(ptrsarray, 2);

system("pause");
return 0:
}


Report •

#19
June 23, 2010 at 15:10:13
unbalanced braces in function

Report •

#20
June 23, 2010 at 18:49:34
Does #18 even compile for you (with the added in brace).


string * ptrsarray [] = {"pete", "rose"};


I think I finally understand this, you are trying to make an array of string pointers, but you are giving it strings at initialisation, not pointers.

I think the compiler is saying "these aren't pointers, they are strings, maybe it's a character array(i.e cstring), but wait if it's a character pointer(array) it can't be a string pointer."

You must remember that a pointer isn't a data structure, it only references a place that a data structure exists in memory. You have no string objects, only pointers that can be set to reference them.

You could only create strings on the heap and set the pointers to reference them, but without setting the pointers to reference *something* they are essentially useless, that is of course unless you count crashes as useful.


If you must have a multi-dimensional static sized array of strings, I would suggest to do it numerically, at least that way you have the pointers and actual strings you can use.

string test[5][5];
test[0][0] =  "hello!";
test[0][1] = "goodbye!";
......


If you want to use dynamic allocation it takes a bit more care.


Report •

#21
June 23, 2010 at 21:41:34
the problem involves : array of pointers to string!!!!!!!!!!!!!!!!!!!


a function with two parameters : an array of pointers to string,and the size of array, return value is void.


print out an array of pointers to string...!!!!!


that's all


Report •

#22
June 23, 2010 at 22:21:46
..... And my point was that the pointers actually have to point *somewhere*, i.e to an *actual* string. Otherwise they are pointers to nowhere(or worse - memory not assigned to the program).

You are going to need to either:

A: Create some strings and your pointer array. Set the pointers in the pointer array to the location of the string.

B: Create you pointer array and use "new"(don't forget to delete when finished) to allocate a string for each item and set the pointers to their locations.

Either way you are going to need to know how many items the array is going to be, B requires some cleanup on exit, but allows you to figure out the number needed at runtime.


I have a working example of B, but I refuse to do other peoples homework.

I just made this too easy:

    string teststr1 = "yesy";
    string teststr2 = "noe";
    string * ptrtest[2];
    ptrtest[0] = &teststr1;
    ptrtest[1] = &teststr2;


Report •

#23
June 24, 2010 at 14:13:57
#include<iostream>
#include<string>

using namespace std;

void printPtrList( ( string * ptrArray[], int size){

for ( int i = 0; i < size; i ++)
cout << *ptrArray[i] << endl;

}


int main() {

string * ptrArray[4];

string a = "bob";
string b = "clyde';
string c = "steve";
string d = "pete";

ptrArray[0] = &a;
ptrArray[1] = &b;
ptrArray[2] = &c;
ptrArray[3] = &d;


printPtrList(ptrArray,4);

system("pause"
return 0;

}


Report •

#24
June 24, 2010 at 17:05:09
That actually works fine except you have 3 syntactical errors; they are basically typos.

Report •

Ask Question