By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Problems with char and strings and pointers

P: n/a
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

Feb 26 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* 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<stringconst 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;
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 26 '07 #2

P: n/a

Jack wrote:
>
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/
Feb 26 '07 #3

P: n/a

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

Feb 26 '07 #4

P: n/a
On Feb 25, 6:50 pm, "Grizlyk" <grizl...@yandex.ruwrote:
Grizlyk wrote:
*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,

Feb 26 '07 #5

P: n/a
On Feb 25, 9:50 pm, "Jack" <accpac...@hotmail.comwrote:
On Feb 25, 6:50 pm, "Grizlyk" <grizl...@yandex.ruwrote:
Grizlyk wrote:
*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".

Feb 26 '07 #6

P: n/a
"Jack" <ac*******@hotmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
On Feb 25, 6:50 pm, "Grizlyk" <grizl...@yandex.ruwrote:
>Grizlyk wrote:
*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.
Feb 26 '07 #7

P: n/a
Jim Langston <ta*******@rocketmail.comwrote:
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 27 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.