473,326 Members | 2,813 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,326 software developers and data experts.

How to initialize an array of pointers to an array of characters?

Hello,

I am trying to initialize an array of pointers to an array of characters, I can do it in 3 lines but I really want to do it in one line at the same time keeping the #define.

3 lines initialization (can compile)
======================
#define A 1
#define B 2
char row1[] = {A|B, B, A};
char row2[] = {B, A};
char *test[]= {row1, row2};

1 line initialization (failed)
===============================
char *test[] = { {A|B, B, A}, {B, A} }; // <- how do i do this??

I do not want this because it waste ROM space
=============================================
char test[][3] = { {A|B, B, A}, {B, A} };


Much appreciate if anyone can give me some pointers, thnx - paul.
May 21 '14 #1
18 2241
donbock
2,426 Expert 2GB
Row 1 has three elements, but row 2 only has two elements. How do you plan to keep track of how many elements are in each row? You basically have two options: specify the row size explicitly (add a field to specify it) or implicitly (hard-coded into the logic of your software.
May 21 '14 #2
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. char *test[] = { {A|B, B, A}, {B, A} }; // <- how do i do this??
  2.  
Here test is an array of char pointers. Unfortunately, A and B are integers so they can't used as char pointers.

BTW: The nested braces don't do anything other than group items for the human eye. Braces do not create arrays.

You will need to:
1) Create your char arrays.
2) Create your char pointer array
3) assign the address of element 0 of each char array to and element of your char pointer array.

You might want to read: http://bytes.com/topic/c/insights/77...rrays-revealed
May 21 '14 #3
Hi donbock,

thanks for your reply. yes, i intend to add a termination character to mark the end of the row. That aside, are you aware of how this could be done in one line?
May 21 '14 #4
Hi weaknessforcats,

You will need to:
1) Create your char arrays.
2) Create your char pointer array
3) assign the address of element 0 of each char array to and element of your char pointer array.


That is exactly what i did in my example but I need to do it in one line something like this:

char *test[] = { {A|B, B, A}, {B, A} };

I know it is not the correct syntax, but if it can be done in 3 lines surely it can be done in one line. Is there no way?
May 21 '14 #5
donbock
2,426 Expert 2GB
Why is it so important to write this on one line?

You already know one way to do it: declare test as a 2-dimensional char array. However, then all rows will be the same length. You don't like this approach.

I can think of one other way to do it -- by declaring each row as a string. However, it would be hard to still use the #defines. Also, with strings you have no choice in what the row-termination character will be.
May 21 '14 #6
@donbock


you do not appreciate the magnitude of the problem with the 5 line code i provided.
instead of 2 rows, say I have 100s of rows of different sizes. That means I have to use 100s of row labels and the last line would have to look something like this:

char *test[] = {row1, row2, .. row 999};

If i can do it in a "one-line" syntax, I can do it in a tabular form and that save a lot of work. of course i can write a pre-processor but that is another discussion.

char test[][5]

is no good because a few of my rows are 100s char long and the rest are 10s char long, i would have wasted a lot of space which I cannot afford.

I cannot do this:

char *test[] = { "x\abx\12", "x\ed" }

because I need the #define.
May 22 '14 #7
donbock
2,426 Expert 2GB
I can't think of a clever way to do what you want in C.

Take a look at C Preprocessing with TCL from 1998 article in Dr Dobbs.
May 22 '14 #8
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. char *test[] = {row1, row2, .. row 999};
You are forcing yourself into this by having the arrays all available at compile time.

Consider creating the arrays on the heap at run time and storing the address of the array in your array of char pointers.

None of the char arrays would need a label.

Note also that having everything on the stack may cause a stack boundary overflow and crash your program.

Extremely large database machines, like Oracle, are written in C and do not have problems with labels.
May 22 '14 #9
@ weaknessforcats

you are making things more complicated than it need be. perhaps it is my fault not to make things absolutely clear. The initialization values would go into ROM. I should have added the "const" in my declaration. There will be no problems with heaps or stacks, i know that.

i don't have a problem with labels per see. they are needed to initialize the final constant array. How do you suggest i

"assign the address of element 0 of each char array to and element of your char pointer array." without a label? how do i reference that char array to do this without a label?

i knew about the solutions offered by yourself and donbock but have discounted them before i posted this question, nevertheless, i appreciate your effort. thank you. what i am really looking for is a one-line syntax because if it can be done on multiple lines, it can usually be done in one line perhaps with some ugly casting and braces.

I would like to hear from C experts or c compilers writers if this is indeed possible (ie within the ANSI C specification), if the language does not support it, so be it.
May 22 '14 #10
@ donbock

thanks for the heads up. I am not stuck, i do have a solution, just that it is not "nice" and I am curious if it can be done in a "one-line" syntax, if it cannot be done, so be it, no worries. thnx
May 22 '14 #11
donbock
2,426 Expert 2GB
@paul321thnx
The following example relies on implicit string concatenation. Would something like this work for you:
Expand|Select|Wrap|Line Numbers
  1. #define A "\x01"
  2. #define B "\x02"
  3. #define AorB "\x03"    // A|B
  4. ...
  5.  
  6. char *test[] = {
  7.      AorB B A,
  8.      B A
  9.      ...
  10. };
By the way, you are using char as an integral type. This is inadvisable because whether or not char is signed is implementation-dependent. You will get more reliable and portable behavior using unsigned char. However, that isn't compatible with the string approach.
May 22 '14 #12
@donbock

almost but not quite, i need the compiler to do those AND OR ADD etc for me at compile time and i want to leave them as A|B, A&B for easy reference (yes, i can put a comment like what you did). and, yes i am picky! ;)

thanks for the unsigned char tip though.
May 22 '14 #13
weaknessforcats
9,208 Expert Mod 8TB
Maybe you could try:

Expand|Select|Wrap|Line Numbers
  1. char array[10] = { A | B, B, A,B,A,A,B,B,A&B };
  2. char *test[] = { &array[0], &array[2], &array[4]};
  3.  
There really is no such thing as an array in C. All you have is a memory allocation that you can view however you like. You are allowed the [] syntax but that is just so the compiler can do pointer arithmetic.

There is an example of this in the insight I recommended.

Here the array test has 3 pointers to char arrays.
May 23 '14 #14
donbock
2,426 Expert 2GB
@paul321thnx
I'm out of ideas. Good luck.
May 23 '14 #15
@weaknessforcats

Thanks, but I don’t see how your latest proposal is any better. In fact, it is worse because I have to calculate the pointer offsets manually … it is not good practice to do this where possible.

It is factually wrong to say there is no such thing as array in C. The C compiler can and will calculate the correct position based on your declared dimension and size. Ultimately everything will be converted to indirect address op code or base offset op code. If by your definition C has no array, then on the same token, there is no such thing as array in any computer language.

The [] syntax is a directive to the compiler to pack the array, whereas for example, [10] will ask the compile to reserve 10 locations. The [] has nothing to do pointer arithmetic. For pointer array initialization as in our case, the compiler just get the address of the first element, followed by the address of the second element and so on, there are no arithmetic involved.

Btw, your earlier statement about nested braces having no functionality other than to group items for the human eye is also factually wrong. The casual C user might be led to believe the following arrays, "test1" and "test2" produce the same result …they do not. Both array initialisations are perfectly legal C syntax and are use routinely. "test2" array initialization also prove that the C language is aware of the dimension of the declared array, and not just a flat memory. Unlike high level language like Java or BASIC, the C language empowers the user to do almost "anything" but that does not mean it is "stupid".

char test1[2][3]= {1,2,3,4,5,6};
char test2[2][3]= { {1,2}, {3,4,5} };
May 23 '14 #16
@ donbock

no worries, maybe there is no way. I am not stuck, just looking for a better solution. thanks for the effort anyway.

i am waiting to hear from some c grammar expert to tell me definitively if it can or cannot be done. anyone ? ... :-)
May 23 '14 #17
weaknessforcats
9,208 Expert Mod 8TB
also prove that the C language is aware of the dimension of the declared array, and not just a flat memory.

C is not aware of the array size. It is only aware of the size of elements on the stack. Pass the array to a function and all you have is the address of element 0. Check out decay of array.

C doesn't care about the number of elements. It just uses that to allocate flat memory. All allocations are flat memory. That's all malloc() can do.

Please read the insight recommended in post #3.
May 23 '14 #18
@ weaknessforcats

C is has no bounds checking, I give you that. All memory allocations are flat, 3 dimension RAM and ROM does not exist physically. It is how you interpret the data that counts. So “there is no such thing as an array in C” , then would you enlighten us which computer language has array then? Do you actually know what is meant by "array" in the first place?

My question is about array initialisation, parameter passing and dynamic allocation (malloc) is off topic. FYI, when passing parameters to subroutines, you can either pass as address or as value, that is what ALL computer language do, C is no different.

Anyway, I think we are getting off topic, unless you have some insights to my original questions, I thank you for your effort. If you wish to talk about arrays, parameter passing, or the C language, you should start anther thread.
May 24 '14 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Kyle | last post by:
Hi, Is it possible to initialize a constant memeber array in a class? I tried several syntax but all failed. class A { public: A(); ~A(); private:
0
by: Kueishiong Tu | last post by:
How do I initialize a char array with null characters? Is there an equivalent function to bzero() in VC++.net?
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
7
by: cdg | last post by:
Would anyone explain how to return an array that wasn`t passed from "main" using a pointer. I'm not understanding how scope and memory are involved in this. However, I do know that there are two...
3
by: pedestrian via DotNetMonster.com | last post by:
Let's say I declares an array of string (of size 1000), how to fill every elements of it with random characters? Dim myStrArray(999) As String ... Thanks... -- Pedestrian, Penang.
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
4
by: sam_cit | last post by:
Hi Everyone, I understand the meaning and usage of pointers to constant characters and constant pointers to characters, however i often get confused with the syntax of expressing them, can...
8
by: =?big5?B?r0W84Q==?= | last post by:
Hi All C gurus: Below is a small program to print out the address of array and address of array variable: int main() { char array = "haha"; printf("array:%x\n", array); printf("&array:%x\n",...
2
by: Olaf P. | last post by:
Hi, I'm writing an FIR filter. The coeffs for low/high/band pass and band stop (LP,HP,BP,BS) are calculated by matlab and exported as header, e.g. const int LP_SZ = 33; const real64_T LP_H =...
1
by: Lisa L | last post by:
Hello all! I was wondering why I cannot initialize a 2d array like this? my @array1; my @array2; foreach my $obj (0..$#valueList){ $array1 = ($varible1, $variable2, $variable3); ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.