473,581 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple question with array of strings

Hi,

This may look as a smiple task to most of you, but to me (a beginner
with C), it drives me crazy.

All I want is that one function passes a two dimensional array of
strings to another function.

example:
NOTE: the strings can be of different length and the array will be
initialized many times to differnt values.

void fun_1()
{
char **Arr[3][2]= {'\0'}; //I want to define a 3x2 array of strings
of
//different length (max 32 ch)
//Arr initialization
// Arr ["ONE", "1"]
// ["Two", "2"]
// ["THREE", "3"]

fun2(Arr);
}

void fun_2(char **rcvArr)
{

}

CAn anybody help me with how to define it, how to initialize it and
hwo to send and receive it as a parameter.

I suspect that I must define some buffer length. The question is
should I select fixed length (e.g. 32 ch per string) or a veriable
length. In the other option I would probably need to use malloc in
order to allocate memory...

I would be happy to be advised of the best way to do it. Also, when I
initialize the array, I preffer that it will take as little text space
as possible (for example, the best will be to define it all in one
line!).

Thanks for helping
Rafi
Jul 22 '05 #1
9 2725
ra*******@telra d.co.il (Rafi Kfir) writes:
Hi, This may look as a smiple task to most of you, but to me (a beginner
with C), it drives me crazy. All I want is that one function passes a two dimensional array of
strings to another function.
... I would be happy to be advised of the best way to do it.

Look up <vector> and <string> in a C++ book - or online.
Jul 22 '05 #2
Rafi Kfir wrote:
This may look as a smiple task to most of you, but to me (a beginner
with C), it drives me crazy.
If you need help with C, you should consider comp.lang.c as your NG of
choice.
All I want is that one function passes a two dimensional array of
strings to another function.

example:
NOTE: the strings can be of different length and the array will be
initialized many times to differnt values.

void fun_1()
{
char **Arr[3][2]= {'\0'}; //I want to define a 3x2 array of strings
of
//different length (max 32 ch)
In C++ one should always consider standard containers for that:

vector<vector<s tring> > Arr;

In C you probably will want to declare an array of pointers to char:

char *Arr[3][2] = { 0 };

and then somehow allocate the memory. Although, I'd susupect that to
have an array of 33-char arrays would be easier (since you limited your
strings to 32 bytes):

char Arr[3][2][33];
//Arr initialization
// Arr ["ONE", "1"]
// ["Two", "2"]
// ["THREE", "3"]
If you declare your array as 3-dimensional, you still should be able to
initialise it as

char Arr[3][2][33] =
{ { "ONE", "1" }, { "Two", "2" }, { "Three", "3" } };

fun2(Arr);
}

void fun_2(char **rcvArr)
Just repeat the declaration of the Arr in your function declaration (but
pick the right name for it first, 'fun2' or 'fun_2'):

void funX(char Arr[3][2][33])
{

}

CAn anybody help me with how to define it, how to initialize it and
hwo to send and receive it as a parameter.
How about a book on C? Do you have any?
I suspect that I must define some buffer length. The question is
should I select fixed length (e.g. 32 ch per string) or a veriable
length. In the other option I would probably need to use malloc in
order to allocate memory...
Right.
I would be happy to be advised of the best way to do it.
There is no "best" way to skin the proverbial cat. There is always more
than one way and each has its advantages and drawbacks.
Also, when I
initialize the array, I preffer that it will take as little text space
as possible (for example, the best will be to define it all in one
line!).


Yes, you could do that too if your array doesn't change. But it's usually
a maintenance nightmare.

const char arr_[] = "ONE\01\0Two\02 \0THREE\03\0";
/* ^0 ^4 ^6 ^10^12 ^18
const char* const Arr[3][2] =
{{ arr_, arr_+ 4 }, { arr_+ 6, arr_+ 10 }, { arr_+12, arr_+18 }};

V
Jul 22 '05 #3

"Rafi Kfir" <ra*******@telr ad.co.il> wrote in message
news:f7******** *************** ***@posting.goo gle.com...
Hi,

This may look as a smiple task to most of you, but to me (a beginner
with C), it drives me crazy.
news:comp.lang. c if you really are interested in C and not C++.

All I want is that one function passes a two dimensional array of
strings to another function.
It's impossible to pass arrays in C or C++. You have to use pointers
instead. This is one reason (among many) to use vectors instead of arrays in
C++.

example:
NOTE: the strings can be of different length and the array will be
initialized many times to differnt values.

void fun_1()
{
char **Arr[3][2]= {'\0'}; //I want to define a 3x2 array of strings
char* Arr[3][2] = { { "ONE", "1" }, { "TWO", "2" }, { "THREE", "3" } };
of
//different length (max 32 ch)
//Arr initialization
// Arr ["ONE", "1"]
// ["Two", "2"]
// ["THREE", "3"]

fun2(Arr);
}

void fun_2(char **rcvArr)
void fun_2(char* (*rcvArr)[2])

Horrible.
{

}

CAn anybody help me with how to define it, how to initialize it and
hwo to send and receive it as a parameter.

I suspect that I must define some buffer length. The question is
should I select fixed length (e.g. 32 ch per string) or a veriable
length. In the other option I would probably need to use malloc in
order to allocate memory...
Yes, you are right.

I would be happy to be advised of the best way to do it. Also, when I
initialize the array, I preffer that it will take as little text space
as possible (for example, the best will be to define it all in one
line!).


Decide what you are programming, C or C++. Post to the appropriate
newsgroup.

If you are programming C then you have to do something like the rubbish
above.

If you are programming C++, then you can use vector and string and forget
about the rubbish above.

#include <vector>
#include <string>
using namespace std;

struct StringPair
{
StringPair(stri ng f, string s)
{
first = f;
second = s;
}
string first;
string second;
};

typedef vector<StringPa ir> StringArray;

void fun_2(StringArr ay& Arr)
{
// do something with Arr
}

int main()
{
StringArray Arr;
Arr.push_back(S tringPair("ONE" , "1"));
Arr.push_back(S tringPair("TWO" , "2"));
Arr.push_back(S tringPair("THRE E", "3"));
}

Now don't you think that a bit easier. Everything is dynamic, everything
gets freed automatically.

john
Jul 22 '05 #4
#include <stdlib.h>
#include <stdio.h>

void print(char *arg[])
{
int i,j;
for( i = 0; i < 3; i++)
{
for( j = 0; j < 4; j++)
{
printf("%c",arg[i][j]);
}
printf("\n");
}
}

int main()
{
char **ch;
int i,j;

ch = (char**)calloc( 3,sizeof(char*) );
for(i = 0; i < 3; i++)
{
ch[i] = (char *)calloc(4,size of(char));
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
ch[i][j] = 'c';
}
}
print(ch);

return 0;
}

Jul 22 '05 #5
#include <stdlib.h>
#include <stdio.h>

void print(char *arg[])
{
int i,j;
for( i = 0; i < 3; i++)
{
for( j = 0; j < 4; j++)
{
printf("%c",arg[i][j]);
}
printf("\n");
}
}

int main()
{
char **ch;
int i,j;

ch = (char**)calloc( 3,sizeof(char*) );
for(i = 0; i < 3; i++)
{
ch[i] = (char *)calloc(4,size of(char));
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
ch[i][j] = 'c';
}
}
print(ch);

return 0;
}

Jul 22 '05 #6
#include <stdlib.h>
#include <stdio.h>

void print(char *arg[])
{
int i,j;
for( i = 0; i < 3; i++)
{
for( j = 0; j < 4; j++)
{
printf("%c",arg[i][j]);
}
printf("\n");
}
}

int main()
{
char **ch;
int i,j;

ch = (char**)calloc( 3,sizeof(char*) );
for(i = 0; i < 3; i++)
{
ch[i] = (char *)calloc(4,size of(char));
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
ch[i][j] = 'c';
}
}
print(ch);

return 0;
}

Jul 22 '05 #7
oops, guess i should have read the bright red instruction at the top of the
page telling me not to resubmit if my reply doent show up right away. sorry
about that

Jul 22 '05 #8
In article <f7************ **************@ posting.google. com>,
Rafi Kfir <ra*******@telr ad.co.il> wrote:

This may look as a smiple task to most of you, but to me (a beginner
with C), it drives me crazy.

All I want is that one function passes a two dimensional array of
strings to another function.
Doing this using old-style arrays and char* "strings" is *not* a simple
task, even for experienced programmers. C++ has tools that make it much
easier, namely the standard 'vector' and 'string' data types.
example:
NOTE: the strings can be of different length and the array will be
initialized many times to differnt values.
No problem. The standard 'string' type automatically resizes itself
appropriately.

#include <vector>
#include <string>

using namespace std;
void fun_1()
{
char **Arr[3][2]= {'\0'}; //I want to define a 3x2 array of strings
of
//different length (max 32 ch)
//Arr initialization
// Arr ["ONE", "1"]
// ["Two", "2"]
// ["THREE", "3"]
vector<vector<s tring> > Arr (3, vector<string>( 2));
Arr[0][0] = "ONE";
Arr[0][1] = "1";
// etc. for the other elements

fun2(Arr);
}

void fun_2(char **rcvArr)
void fun_2 (vector<vector< string> >& rcvArr)
// and make it const if you don't plan to modify the data inside the
// function{

}


--
Jon Bell <jt*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #9

void Func(char**);
//I don't work with pointers to pointers much
//so I'd have to look-up the correct way to make
//the above argument const-correct.

int main()
{
char* string_array[3][2];

string_array[0][0] = "One";
string_array[0][1] = "1";
string_array[1][0] = "Two";
string_array[1][1] = "2";
string_array[2][0] = "Three";
string_array[2][1] = "3";

Func(string_arr ay);
}

void Func(char** string_array);
{

}
-JKop
Jul 22 '05 #10

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

Similar topics

7
1952
by: news.hku.hk | last post by:
is there any way to make a loop to store strings?? e.g. abc.txt contains 3 lines i.e. this is line 1. this is line 2. this is line 3. i want to create a loop that can make the three strings contains the corresponding lines,
4
4982
by: Anthony | last post by:
Hello, I am writing a function that populates an array of pointers to strings. Both the number of strings in the array, and the lengths of the strings, are dynamic; in particular, the number of strings won't be known until just before the function returns. The problem is that in the calling function, I need to know both the address of...
51
8250
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
9
2019
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple function I'm trying to write. I'm writing a function that takes a string, and returns an array of strings which are the result of splitting the...
6
1388
by: Robert W. | last post by:
I have a situation where I have pairs of strings that I want to put into an ArrayList and then later pull out of it. To keep things simple I tried this to put them in: arrayList.Add(new object {stringA, stringB}); That works fine. But I can't figure out how to unbox them into their constituent components. Might anyone know a way? ...
24
3423
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
2
22588
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the...
6
3787
by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by:
Hi, I'd appreciae any advice on how to do this in VB 2005. I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and etc... I like to determine how many unqiue "States" are in this array...ie., duplicate entires, are igorned...
0
7792
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...
0
8304
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7899
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...
0
8175
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...
0
5364
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3805
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2301
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
1
1403
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1138
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...

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.