473,786 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to define this complex declaration and how to use it

Hi,

how can be the declaration of

a pointer which points to dynamic array of pointers and each pointer
from this array points to an dynamic array of pointers which points to
character strings

p-->p-->p
p
p-->p
p
p
p-->p
p
p
p

How to use such storage to fill the strings and then retrieve. Does
anybody has the example
upto this deep pointing algorithm.

Thanks,

Cric

Sep 5 '07 #1
6 1668
On Tue, 04 Sep 2007 21:08:45 -0700, Cric wrote:
Hi,

how can be the declaration of

a pointer which points to dynamic array of pointers and each pointer
from this array points to an dynamic array of pointers which points to
character strings
Usually you'd better have a pointer to the first element of an
array than to the whole array, especially if its size is unknown
at compile time. So you want a pointer to pointer to char, or
pointer to pointer to pointer to char, depending on what you mean
by "points to character strings".
So: char **ptr; or char ***ptr.

--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower
$ cd /pub
$ more beer
Sep 5 '07 #2
Cric <fr*******@yaho o.comwrote:
# Hi,
#
# how can be the declaration of
#
# a pointer which points to dynamic array of pointers and each pointer
# from this array points to an dynamic array of pointers which points to
# character strings

Also known as Iliffe vectors <http://www.google.com/search?q=iliffe +vector>.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.
Sep 5 '07 #3
Sorry may be my description is not clear

I want data storage as
------------------------
<first block>

<char string>
<char string>
<char string>
<char string>
....
....<dynamic>
------------------------
<second block>

<char string>
<char string>
<char string>
<char string>
....
....<dynamic>
--------------------------
<Third block>

<char string>
<char string>
<char string>
<char string>
....
....<dynamic>
----------------------------
many more dynamic blocks may be addedd

so how to have this kind of data structure with reference to pointers

Thanks

Cric

Sep 5 '07 #4
On Wed, 05 Sep 2007 05:01:12 -0700, Cric <fr*******@yaho o.comwrote:
>Sorry may be my description is not clear

I want data storage as
------------------------
<first block>

<char string>
<char string>
<char string>
<char string>
...
...<dynamic>
------------------------
Are all of the multiple strings the same size (or the same maximum
size)? Equivalently, given the address of the first string, can I
compute the address of the second without evaluating the actual length
of the string?

If the answer is yes, then you can point to this block of strings
(actually an array of strings) with a pointer to the first string.
char (*p1)[MAX_STRING_SIZE] = malloc(N * sizeof *p1); would allocate
space for N such strings, each occupying an array of MAX_STRING_SIZE .
p1[0] would be the first array, p1[1] the second, etc. p1 can be
reallocated to allow more strings.

If you do this, you need some way to tell when you are at the last
p[i].

You either keep track of the maximum i p[i] is valid for
elsewhere or

You set the last string to a sentinel value, such as
strcpy(p1[N-1],"");

If the strings are not the same size (often called a jagged or ragged
array), then you basically need one pointer for each string. Since
the string sizes vary, the simplest approach is a char* that points to
the start of the string. The obvious solution is a dynamic array of
such pointers. Something along the lines of

char **p2 = malloc(N * sizeof *p2);
p2[0] = &string0;
p2[1] = &string1; ...

You have the same choices as above for determining the number or
strings plus the option of setting p2[N-1] to NULL. p2 can be
reallocated to allow more strings. Furthermore, if any string is also
dynamically allocated, p2[i] can be reallocated as well.

snip second block
><Third block>

<char string>
<char string>
<char string>
<char string>
...
...<dynamic>
----------------------------
many more dynamic blocks may be addedd
Now you have a dynamic number of blocks. You want a dynamic array
where each element is a pointer to a block.

If you chose the p1 approach above, then the pointer to the block has
type char(*)[MAX_STRING_SIZE]. The simplest approach is to change the
definition of p1 to

typedef char (*T1)[MAX_STRING_SIZE];
T1 p1 = malloc ...;

and your pointer for allocating a dynamic array of T1's is

T1 *da1 = malloc(M * sizeof *da1);

On the other hand, if you are a glutton for punishment, you can omit
the typedef and code

char (*da1)(*)[MAX_STRING_SIZE] = malloc ...

If you chose the p2 approach above, the declarations are much simpler.

char ***da2 = malloc(M * sizeof *da2);
Remove del for email
Sep 6 '07 #5
Cric:
how can be the declaration of

a pointer which points to dynamic array of pointers and each pointer
from this array points to an dynamic array of pointers which points to
character strings

Use typedef's if you're brain hurts. Then try to wean yourself off
them.

Work backwards, reading from right to left:

1: character strings = arrays of char = char arr[NUM_1];
2: dynamic array of pointers which points to = char **p = malloc(NUM_2
* sizeof(char*));
3: a pointer which points to a dynamic array of pointer and each
pointer from this array points to:
char ***p = malloc(NUM_3 * sizeof(char**)) ;

Martin

Sep 10 '07 #6
Cric wrote:
>
Hi,

how can be the declaration of

a pointer which points to dynamic array of pointers and each pointer
from this array points to an dynamic array of pointers which points to
character strings

p-->p-->p
p
p-->p
p
p
p-->p
p
p
p

How to use such storage to fill the strings and then retrieve. Does
anybody has the example
upto this deep pointing algorithm.
How do you feel about using a list of lists of pointers to strings
instead?

--
pete
Sep 10 '07 #7

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

Similar topics

34
6472
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
14
1660
by: Blue Ocean | last post by:
My c++ text tells me that I should define methods this way: class Stack { int method(double t); Stack(int s); ... } int Stack::method(double t)
7
1451
by: PengYu.UT | last post by:
Hi, Suppose I have the following code. I want A::show() behaves differently for template argument with or without std::complex. But it doesn't work. Do you know how to do that? Best wishes, Peng #include <iostream>
7
6144
by: Brian Sammon | last post by:
According to my programming textbook, "int a;" at the top level of a file is a definition which shouldn't go in a header file. However, I'm running into a lot of online documentation that treats that kind of statement as a declaration that can be in a header file used in multiple compilation units. Which of these is correct? Is this something that has changed between different standards?
42
5631
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
28
2416
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: int main() {
3
9808
by: robin liu | last post by:
What's the difference between these two declarations ? 1) typedef void (*pf)(void); 2) typedef void f(void); the first declaration is define a function pointer, what is the second ? define a function model? And can use the second declaration to define a function pointer as follow:
28
6052
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
7
2778
by: Chris Saunders | last post by:
I'm not very familiar with C#. I have a static class and would like to define some constants similarily to Math.PI. Could someone show me how to go about doing this. So far no luck searching. Regards Chris Saunders
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.