473,395 Members | 1,742 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,395 software developers and data experts.

Problems with char and strings and pointers

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
7 2068
* 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

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

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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vasko Altiparmakov | last post by:
hello I am very new to C++ (and C) so i need something to clear up. if you write char ch; you can uste it to store null terminated strings with length of 39 chars ascII.
2
by: Tim Partridge | last post by:
Why would this be illegal? #include <list> using namespace std; int main ( char argc, char* argv ) { list< const int * const > l; return 0; }
3
by: pmatos | last post by:
Hi all, I have 2 char* iterators str and end and I'm doing as follows: string id_str(str, end); const char * id = id_str.c_str(); but these has 2 problems afaik. One, I'm generating a string...
11
by: stroker_ace | last post by:
Hi, I am working on a problem where I need to implement a string buffer that I can remove an arbitary length char* from one end and add them to the other. So far the only way I have thought...
5
by: JS | last post by:
I give a function a void pointer as an argument. But in the function I would like to treat this argument as an integer (only pointers to integers will be sent to the function) therefor I would like...
8
by: Lawrie | last post by:
Hi, I have checked the FAQ and can't find an answer to something that has been bugging me for a while....... I am using the socket.h write function in non blocking mode char* message =...
2
by: Wilfried Mestdagh | last post by:
Hi, If I make a DLL in Delphi (5..7, so not NET) to manipulate strings, then I assume I have to do something like this: public static extern TestProc(String C); and in the Delphi DLL:
1
by: H.B. | last post by:
Hi, How can I easily use char strings to display text in listbox. I need to do something like: char MyText = "The text"; MyListBox->Items->Add(MyText); Thanks,
4
by: nass | last post by:
hello everyone, i have a bit of problem reading char * strings from a buffer (a shared memory, pointed to by 'file_memory'). basically i have a structure in memory 'ShMem' that can be accessed by...
21
by: arnuld | last post by:
int main() { const char* arr = {"bjarne", "stroustrup", "c++"}; char* parr = &arr; } this gives an error: $ g++ test.cpp test.cpp: In function 'int main()': test.cpp:4: error: cannot...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.