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

Pointer to Pointer

Hi,

How to manually add an array of string, which is pointer by a pointer,
e.g.

char **str;

// add "this", "is", "a", "test" one by one into the 2d array?

thanks.

Oct 22 '06 #1
11 1464
howa wrote:
Hi,

How to manually add an array of string, which is pointer by a pointer,
In C++ a string means the std::string class:

#include <string>

std::string mystring = "this is a string";

What you probably mean is a C-style array of chars, but it is
recommended that you avoid them in C++.
char **str;

// add "this", "is", "a", "test" one by one into the 2d array?
You have a pointer, not a 2D array. It is very, VERY wrong to think of
double-pointers as arrays.

Anyway, you should use a standard vector of strings instead:

#include <vector>
#include <string>

std::vector<std::stringmystrings;
mystrings.push_back("some string");

mystrings[0]; // <-- you access the vector just like any array

Regards,
Bart.

Oct 22 '06 #2

Bart 寫道:
howa wrote:
Hi,

How to manually add an array of string, which is pointer by a pointer,

In C++ a string means the std::string class:

#include <string>

std::string mystring = "this is a string";

What you probably mean is a C-style array of chars, but it is
recommended that you avoid them in C++.
char **str;

// add "this", "is", "a", "test" one by one into the 2d array?

You have a pointer, not a 2D array. It is very, VERY wrong to think of
double-pointers as arrays.

Anyway, you should use a standard vector of strings instead:

#include <vector>
#include <string>

std::vector<std::stringmystrings;
mystrings.push_back("some string");

mystrings[0]; // <-- you access the vector just like any array

Regards,
Bart.
is it possible to create dynamically using char* ?

just like the int main(char **argv) ?

thanks.

Oct 22 '06 #3
Bart wrote:
howa wrote:
>Hi,

How to manually add an array of string, which is pointer by a pointer,

In C++ a string means the std::string class:

#include <string>

std::string mystring = "this is a string";
Not when the text clearly refers to a C-style string.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 22 '06 #4
howa wrote:
>
is it possible to create dynamically using char* ?

just like the int main(char **argv) ?
Yup.

typedef char *str; // less confusing
str *mystr; // dynamically allocated array
mystr = new str[whatever_you_need];
for (int i = 0; i < whatever_you_need; ++i)
str[i] = new char[whatever_i_needs];

The bookkeeping can be tricky, though, so be careful.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 22 '06 #5
howa wrote:
Hi,

How to manually add an array of string, which is pointer by a pointer,
e.g.

char **str;

// add "this", "is", "a", "test" one by one into the 2d array?

thanks.

Sounds like homework. What do you have so far? What confuses you? Do
you know what dynamic memory allocation is, and how to do it?

Crack your book and get to it!


Brian
Oct 22 '06 #6
Pete Becker napisal(a):
Bart wrote:
howa wrote:
How to manually add an array of string, which is pointer by a pointer,
In C++ a string means the std::string class:

Not when the text clearly refers to a C-style string.
When a poster seems confused and is obviously new to the language I
feel obligated to point them in the right direction. I still maintain
that in the context of C++ the term 'string' by itself should be used
only for std::string and that a more qualified term should be used to
refer to C-style strings.

Regards,
Bart.

Oct 22 '06 #7
howa napisal(a):
is it possible to create dynamically using char* ?

just like the int main(char **argv) ?
That's not a correct declaration of main. And yes, you can allocate it
dynamically, but the question is why? Is this homework? If it's not,
then you should use std::string. Even the argv parameter of main should
not be manipulated as is, but used to initialize a std::string instead.

Regards,
Bart.

Oct 22 '06 #8
Bart wrote:
Pete Becker napisal(a):
>Bart wrote:
>>howa wrote:
How to manually add an array of string, which is pointer by a pointer,
In C++ a string means the std::string class:
Not when the text clearly refers to a C-style string.

When a poster seems confused and is obviously new to the language I
feel obligated to point them in the right direction. I still maintain
that in the context of C++ the term 'string' by itself should be used
only for std::string and that a more qualified term should be used to
refer to C-style strings.
I see. You point them in the right direction by stating your opinion as
if it was a universal truth.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 22 '06 #9
Bart wrote:
Even the argv parameter of main should
not be manipulated as is, but used to initialize a std::string instead.
There's nothing wrong with indexing into argv. Creating unnecessary
objects just bloats your code.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 22 '06 #10
Pete Becker wrote:
Bart wrote:
Even the argv parameter of main should
not be manipulated as is, but used to initialize a std::string instead.

There's nothing wrong with indexing into argv. Creating unnecessary
objects just bloats your code.
Feel free to call a single instance in your main function "bloat" if
you like. Some prefer the added convenience.

Regards,
Bart.

Oct 23 '06 #11

Pete Becker 寫道:
howa wrote:

is it possible to create dynamically using char* ?

just like the int main(char **argv) ?

Yup.

typedef char *str; // less confusing
str *mystr; // dynamically allocated array
mystr = new str[whatever_you_need];
for (int i = 0; i < whatever_you_need; ++i)
str[i] = new char[whatever_i_needs];

The bookkeeping can be tricky, though, so be careful.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
thanks!

Oct 23 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carsten Spie | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
8
by: Martin Jrgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.