LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2014, 10:21 AM   #1
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 266

Rep: Reputation: Disabled
initializing a multidimensional array of strings


Initializing an array of unequal length strings can be done:
Code:
const char *string[] = {"s1", "s2.", "s3.."};
for(i=0; i<3; i++) printf("string[%d] = %s\n", i, string[i]);
What I really want to do is to have a multidimensional array of strings which will allow:

Code:
printf("subtopic %d of topic %d = %s\n", i_subtopic, topic, subtopic[topic][i_subtopic]);
I do know the size of topic, but I have an unequal length of strings
How do I initialize the character array subtopic?
 
Old 03-19-2014, 10:27 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by piobair View Post
How do I initialize the character array subtopic?
When posting on this forum it is always helpful to identify which programming language you are using.

Daniel B. Martin
 
Old 03-19-2014, 10:34 AM   #3
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 266

Original Poster
Rep: Reputation: Disabled
Sorry, I am writing in C. I thought that would be obvious from my example.

Quote:
Originally Posted by danielbmartin View Post
When posting on this forum it is always helpful to identify which programming language you are using.

Daniel B. Martin
 
Old 03-19-2014, 11:21 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by piobair View Post
I thought that would be obvious from my example.
Obvious to someone who knows C. Some of us don't. Participants in this forum use a wide variety of languages.

Daniel B. Martin
 
Old 03-19-2014, 12:10 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You can initialize a two-dimensional array; however in my test code I had to put a dimension on the array and specifically place it on the second dimension.

Code:
#include <stdio.h>

void main(void)
{
    const char *test[][3] = { { "1", "2", "3" }, { "a", "b", "c" } };

    printf("Test strings are: { %s %s %s } { %s %s %s }\n",
           test[0][0], test[0][1], test[0][2],
           test[1][0], test[1][1], test[1][2]);
}
 
Old 03-19-2014, 04:50 PM   #6
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 266

Original Poster
Rep: Reputation: Disabled
Unfortunately, the sub-arrays are not the same size.
I did figure out how to do it:

Code:
define n_subtopics 4
char **subtopic[n_subtopics];
char *subtopic_0[] = ("s0", "s0.", "s0.."};
char *subtopic_1[] = ("s1", "s1.", "s1..", "s1..."};
char *subtopic_2[] = ("s2....", "s0...", "s0.."};.
char *subtopic_3[] = {"s3...", "s3."};
subtopic[0] = &subtopic_0[0];
subtopic[1] = &subtopic_1[0];
subtopic[2] = &subtopic_2[0];
subtopic[3] = &subtopic_3[0];
Quote:
Originally Posted by rtmistler View Post
You can initialize a two-dimensional array; however in my test code I had to put a dimension on the array and specifically place it on the second dimension.

Code:
#include <stdio.h>

void main(void)
{
    const char *test[][3] = { { "1", "2", "3" }, { "a", "b", "c" } };

    printf("Test strings are: { %s %s %s } { %s %s %s }\n",
           test[0][0], test[0][1], test[0][2],
           test[1][0], test[1][1], test[1][2]);
}
 
Old 03-19-2014, 05:11 PM   #7
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 266

Original Poster
Rep: Reputation: Disabled
I thought it was solved. Further testing found otherwise.
Code:
printf("%s =? %s\n", subtopic[0], subtopic_0[0]); // this works
printf("%s =? %s\n", subtopic[2], subtopic_2[1]); // this doesn't work
 
Old 03-19-2014, 07:04 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Surely you mean
Code:
printf("%s =? %s\n", subtopic[0][0], subtopic_0[0]);
printf("%s =? %s\n", subtopic[2][1], subtopic_2[1]);
You'll get a type mismatch warning if you turn up the warning settings: -Wall -Wextra -Wformat=2
 
Old 03-19-2014, 09:03 PM   #9
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 266

Original Poster
Rep: Reputation: Disabled
Yes, I did mean that. First, I had an incorrect index in my test problem, then a typo in transcribing.
On further testing, the problem solution that I offered earlier really does work. As red as my face is, I will wait a while before I declare that the problem is solved.

Quote:
Originally Posted by ntubski View Post
Surely you mean
Code:
printf("%s =? %s\n", subtopic[0][0], subtopic_0[0]);
printf("%s =? %s\n", subtopic[2][1], subtopic_2[1]);
You'll get a type mismatch warning if you turn up the warning settings: -Wall -Wextra -Wformat=2
 
Old 03-20-2014, 07:39 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
That's why it's always best to write a small piece of test code to validate exactly what you want to accomplish.

I recommend the following compile flags:

-ggdb -Werror -Wall -Wextra -Wswitch-default -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn

I like ntubski's suggestion of -Wformat=2, I'll have to read up on that one and consider adding it to my common build process.
 
1 members found this post helpful.
Old 03-20-2014, 09:44 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by piobair View Post
then a typo in transcribing.
Yes, I find if I'm not copy+pasting directly from tested code, I'm all but guaranteed to make a mistake.

Quote:
Originally Posted by rtmistler
I recommend the following compile flags:

-ggdb -Werror -Wall -Wextra -Wswitch-default -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn
Don't -Wall and -Wextra already cover the following ones? I dropped -Wformat=2 into my tmp/Makefile a while ago, I think it was for printf(a_string_variable) cases.
 
Old 03-20-2014, 10:35 AM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by ntubski View Post
Yes, I find if I'm not copy+pasting directly from tested code, I'm all but guaranteed to make a mistake.



Don't -Wall and -Wextra already cover the following ones? I dropped -Wformat=2 into my tmp/Makefile a while ago, I think it was for printf(a_string_variable) cases.

No -Wall doesn't cover all of those. The documentation for gcc describes what flags are covered by -Wall, in fact -Wformat is covered, but the variations on -Wformat such as =2 is not covered by -Wall.

Easiest two things to do are to look at the manpage for gcc and search for -Wall, but that easily shows what flags are covered. I comprehensive viewing of the flags will show you the list of all flags and when you see ones which you feel are good ideas, you can view the fine print which will say whether or not it is included by -Wall. That second option is what I did a while ago, but it's always worth a continued look.

Last edited by rtmistler; 03-20-2014 at 10:37 AM.
 
Old 03-20-2014, 01:51 PM   #13
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 266

Original Poster
Rep: Reputation: Disabled
Cut and paste of a program does work better than transcribing. Baby steps: I am a self-taught C programmer, and compiling with -Wall is not something that I have done before. I will have to study the rest of that list.

The following, using const char, is (IMHO) better than my previous offering:

Code:
// multidim_strings.c
#include <stdio.h>
extern void exit(int);
const char *subtopic_0[] = {"s0", "s0.", "s0.."};
const char *subtopic_1[] = {"s1", "s1.", "s1..", "s1..."};
const char *subtopic_2[] = {"s2....", "s2...", "s2.."};
const char *subtopic_3[] = {"s3...", "s3."};
const char **subtopic[] = {&subtopic_0[0], &subtopic_1[0], &subtopic_2[0], &subtopic_3[0]};

int main(){
  int i, j, size[] = {3, 4, 3, 2};
  for(i=0; i<4; i++){
    printf("N=%d ",(int)(sizeof(subtopic[i])));
    for(j=0; j< size[i]; j++)
      printf("%s  ", subtopic[i][j]);
    printf("\n");
  }
  exit(0);
}
I thought to use sizeof(subtopic[i]) in lieu of size[i], but sizeof(subtopic[i]) returns 8 for all cases of [i]. I do not understand why that is so.
 
Old 03-20-2014, 01:59 PM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I don't know the exact code syntax, but C++ is eminently better qualified to manage lists and arrays; similarly is python. For instance, just me tossing it out having not checked, you have a list declared, you also have methods for it such as .size() or .length(), I believe it would be .size() where that will give you an integer representation of the size of the list. Further, the greater part of C++ is that things like String types or something similar; classes actually; allow you to declare something as a String class or list of String classes and not worry about pre-declaring the dimensions. So you might want to keep what you do have, but look a bit at some of the available C++ classes you could use.

And to note further, gcc on a general Linux system should be able to deal with C++ code. Not sure if you're working on that type of platform, but just pointing that detail out.
 
Old 03-20-2014, 02:17 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by piobair View Post
sizeof(subtopic[i]) returns 8 for all cases of [i]. I do not understand why that is so.
sizeof returns size in bytes, your size[] array is counting the number of elements. subtopic is an array of pointers, all pointers are 8 bytes (or 4 bytes on a 32 bit system).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl - multidimensional array toredo Programming 3 06-11-2010 08:29 AM
Perl multidimensional array zhjim Programming 9 08-21-2009 09:59 AM
Initializing Array of Strings jrtayloriv Programming 10 02-03-2005 12:22 PM
How to allocate memory for a multidimensional array ? indian Programming 12 11-19-2004 03:37 AM
multidimensional array (php) niehls Programming 2 01-31-2003 05:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration