dynamic string sscanf reading problem

ook people; newly hatched nube joins the action :)
here's my problem:

string ras = "straus";
int i;
i=ras.length();

WORKS

string * kasi;
kasi = new string[20];
*kasi = "hippo";
int i;
i=kasi.length();


DOESNT WORKS why?
BUT
i=kasi->length();

works. why???
--------------------------------------------------------------------
string otar= "monkey";
cout<<otar;
sscanf(otar,"%s",&ch);


works, reads entire string, and sscanf puts it into
char array.

string * kimber;
kimber = new string [20];

kimber[0] = "t";
kimber[1] = "r";
kimber[2] = "i";
kimber[3] = "t";
kimber[4] = "a";
cout<<*kimber;

doesnt reads out entire string it reads only
first element. how can i tell c++ to read
all elements as it does in previous example?
(i dont want to use for() or something;)
im planning to do this,



sscanf(kimber->c_str(),"%s",&ch);



obviously it would read only
first element which is "t".
so how can i tell sscanf to
search through all elements?
Last edited on
I'll try to explain this to you as far as my limited skills allow.

string *kasi;

this defines kasi as a pointer to a string object. Therefore kasi represents the adress where an object is stored in memory.

If you want the object itself you'd use *kasi.

Therefore if you want to call a function of the object pointed to by kasi you'd use:

(*kasi).function() C++ provides another way of doing exactly the same thing, consider it an shortcut kasi->function().

kasi.function() won't work because you're asking a memory adress to call some function. Memory adresses are just what we call them, adresses. You wouldn't knock on a street adress of a house, you'd knock on the house in that street adress. Understand? Hope so.

As for the second problem:

An array is nothing more than a contiguous space in memory to store something.

when you do something like this;

1
2
string *kimber;
kimber= new string[20];


you're assigning the adress of the first element in the array to kimber.

cout expects, strings wise, that you pass the adress of the first element.

cout will then read element by element until it finds the null byte '\0' character.

Therefore if you pass *kimber to cout instead of an adress you're passing the value of the first element. It's the same as if you're doing kimber[0]. cout then treats this as a lonely char and not a string type.

EDIT: I've only realized this:

1
2
3
4
5
6
7
8
kimber = new string [20];

kimber[0] = "t";
kimber[1] = "r";
kimber[2] = "i";
kimber[3] = "t";
kimber[4] = "a";
cout<<*kimber; 


string objects are different from c style strings.

string[20] is an array of 20strings not a string with 20 chars.

You could initialize kimber[0]="This is a C++ string".

Then the first char of kimber[0] would be kimber[0][0]. What I said earliar applies only to c style strings. Sorry
Last edited on
waw... thanks alot. so i will be using (*ptr).function when i need to access memory and send it to function.sometimes im so lost in these things; you wont learn such specific things anywere. if you can recommend a book or tutor on this exact subject youll help me alot.
key moment here is that dynamic string and static one behave differently when we call it's first element

string first = "hi its first string";
string * dynstring;
dynstring= new string[2]; //dynamic with 2 indipendent strings
dynstring[0] = "bye im dynstring's number one string"; //dynstring's number one string
dynstring[1] = "hello im dynstring's number two string "; //dynstring's number two string


cout<<first; //doesnt reads its address, it reads ins input "hi its first string"
cout<<dynstring; //doesnt reads its input, it reads its address ' 00x something something'

cout<< *dynstring; // reads its input which is "bye im dynstring's number one string"
equivalent is this
cout<< (*dynstring);
or this
cout<< (dynstring[0]);

to access dynstrig's number two string
cout<< (dynstring[1]);
and its number three element
cout<<(dynstring[1][2]);

so sscanf will work like this
sscanf((dynstring[1]).c_str(),"%s",(dynstring[0]).c_str()); //copyes dynstring[1] into dynstring[0];

cout<<(dynstring[0]);

hope this will help to many tortured newcomers too; thank you eidge; you justified my 3 days of hopeless suffer :)))))))))))))))))))
Last edited on
No worries I've been (am) there.

I liked "C++ without fear" the non OOP chapters are great. The autor tries to steer you away from error like this. But on the other hand, the OOP chapter are full of error and bad implementations which have given me (well the members of the forum who worked out my problems) hours of trouble and pure misery.

I've seen people talking about "C++ primer" and "C++ primer plus", there's an entire article on books for every skill level here:

http://www.cplusplus.com/articles/GLzyhbRD/

btw: Don't use scanf those are C io functions.

We've got io classes that do the same or more but more elegantly cin and cout are example of those.

Check them under the documentation section.
Last edited on
Topic archived. No new replies allowed.