Problems with char and strings and pointers

J

Jack

Hi,
I am having some problems with char and string datatypes.

Here is an example

int main(){
string str;
cout << "enter string: ";
cin >> str;
}

Is there a way to go look at the 3rd char of the str. So something
like str[2].

What if we have:
int main (int argc, char * argv[])

How do you look at the third char in argv. do u use **argv or
*argv[3].
From what I understand a string is just a array of chars. If this is
true then *argv[] is a pointer to a pointer. Why is this done? why not
just use argv[].

Another question I have lets say you have the following.

int main(){
char * word;
cout << "enter word: ";
cin >> ......
can I some how use malloc or calloc to store the response and return a
pointer to word?

Thanks

J
 
A

Alf P. Steinbach

* Jack:
Hi,
I am having some problems with char and string datatypes.

Here is an example

int main(){
string str;
cout << "enter string: ";
cin >> str;
}

Is there a way to go look at the 3rd char of the str. So something
like str[2].

Yes, you can use str[2].

But better use str.at( 2 ), because it checks whether there /is/ a third
char.

What if we have:
int main (int argc, char * argv[])

How do you look at the third char in argv. do u use **argv or
*argv[3].

There is no such thing as "the third char in argv".

argv is an array of pointers to C-style strings; what those strings are
depends on the implementation, but usually they're separate command line
arguments.

argv[2] yields the third such pointer; argv[2][5] yields the sixth
character in that string.

But instead of doing such unsafe things you should do

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

int main( int nArgs, char* args[] )
{
using namespace std;
vector<string> const arguments( args, args + nArgs );

for( size_t i = 0; i < arguments.size(); ++i )
{
cout << arguments.at(i) << endl;
}
}

From what I understand a string is just a array of chars.

No. Conceptually it's a sequence of characters. Technically it might
be anything (depends on the kind of string).

If this is true then *argv[] is a pointer to a pointer.
No.


Why is this done? why not just use argv[].
Huh.


Another question I have lets say you have the following.

int main(){
char * word;
cout << "enter word: ";
cin >> ......
Don't.

can I some how use malloc or calloc to store the response and return a
pointer to word?

#include <string>
#include <iostream>
#include <ostream>
#include <istream>

int main()
{
using namespace std;
string word;
cout << "enter word: ";
getline( cin, word );
cout << "you wrote: " << word << endl;
}
 
G

Grizlyk

Jack said:
Here is an example

int main(){
string str;
cout << "enter string: ";
cin >> str;
}

Is there a way to go look at the 3rd char of the str. So something
like str[2].

What if we have:
int main (int argc, char * argv[])

How do you look at the third char in argv.

The "argv" is array of pointers to char, you can not "look at the third char
in argv" becasue "argv" does not point to chars.
do u use **argv or *argv[3].

See any about pointers.
**argv == argv[0][0] - first char of first parameter
*argv[3] == argv[3][0] - first char of third parameter
From what I understand a string is just a array of chars. If this is
true then *argv[] is a pointer to a pointer. Why is this done? why not
just use argv[].

Command line divided into some parts is just C and C++ convention with
random rationale.
Another question I have lets say you have the following.

int main(){
char * word;

uninitialized variable "word"
cout << "enter word: ";
cin >> ......
can I some how use malloc or calloc to store the response and return a
pointer to word?

From where and to which place you want to return "a pointer to word?"
See any about C++ standard intput/output stream library.

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
 
J

Jack

Grizlyk said:
*argv[3] == argv[3][0] - first char of third parameter

[3]=0,1,2,3
"4-th" - forth parameter

wow, thanks so much for the reply guys. This is really helpful.
regarding pointers though, let say i have a link list and I want to
add a node to it. it would make sense to me to have my func def to be

int Insert(node head, int data)
rather than
int Insert(node** head, int data)

I would think that with the first func def, I am passing in the
pointer address of the head where as in the second func def, I am
passing in a pointer to the data that head is pointing to.

Is this correct?

int Insert(node head, int data)
where node is a pointer to the pointer of my struct node,
 
J

Jack

Grizlyk said:
*argv[3] == argv[3][0] - first char of third parameter
[3]=0,1,2,3
"4-th" - forth parameter

wow, thanks so much for the reply guys. This is really helpful.
regarding pointers though, let say i have a link list and I want to
add a node to it. it would make sense to me to have my func def to be

int Insert(node head, int data)
rather than
int Insert(node** head, int data)

I would think that with the first func def, I am passing in the
pointer address of the head where as in the second func def, I am
passing in a pointer to the data that head is pointing to.

Is this correct?

int Insert(node head, int data)
where node is a pointer to the pointer of my struct node,

oops in the above reply please ignore everything after "IS THIS
CORRECT".
 
J

Jim Langston

Jack said:
Grizlyk said:
*argv[3] == argv[3][0] - first char of third parameter

[3]=0,1,2,3
"4-th" - forth parameter

wow, thanks so much for the reply guys. This is really helpful.
regarding pointers though, let say i have a link list and I want to
add a node to it. it would make sense to me to have my func def to be

int Insert(node head, int data)
rather than
int Insert(node** head, int data)

I would think that with the first func def, I am passing in the
pointer address of the head where as in the second func def, I am
passing in a pointer to the data that head is pointing to.

Is this correct?

If the parm is
node head
then the node is passed by copy, a copy of the node is made and passed to
the method Insert. So any changed you make to the node won't be seen in the
original.
node** head would be a pointer to a pointer. I don't think you need this.
node* head
should be good enough, since it's a linked list the next node ponter should
be stored in the node itself. Although in C++ it is usually better to pass
a reference for this type of thing than a pointer, so it would actually be
int Insert( node& head, int data )
Any changes made to head inside that method would be changes to the actual
instance, not a copy. A reference I consider a pointer on steroids.
 
M

Marcus Kwok

Jim Langston said:
A reference I consider a pointer on steroids.

I would go the opposite way and say that a *pointer* is a *reference* on
steroids. Pointers can do more than references can, making them more
powerful, but also more dangerous.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top