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

passing arg char[X][Y] to function expecting (char**)

I've come across the following difficulty, related to questions 6.12,
6.13 and 6.18 in the FAQ, which I am unable to overcome:

void fun(char **array_of_strings, int num_elements);

int main(void)
{
char static_array_of_strings[NUM_STRINGS][MAX_STRING_LEN+1];

fun ((char**)&static_array_of_strings, NUM_STRINGS);

return 0;
}

This code compiles but is wrong (I get a segmentation fault). How can I
correctly call fun on static_array_of_strings?

I can't modify the prototype of fun() because it also receives "true"
char** (in the sense of dynamically allocated arrays of strings, ie,
both the number of elements in the array as well as the length of each
string are unknown size at compile time).

How can I pass static_array_of_strings to it? The way I read FAQ 6.18
suggests this is impossible and the prototype of fun() would have to
modified to include MAX_STRING_LEN+1.
Thank you for any help,

Mack

Nov 14 '05 #1
11 2038
Let me add that, in the mean time, the work-around I have found is

void fun(char **array_of_strings, int num_elements);

int main(void)
{
char static_array_of_strings[NUM_STRINGS][MAX_STRING_LEN+1];

/* do other stuff * /

{
char* tmp_array_of_strings[NUM_STRINGS];
int i = 0;
for (; i < NUM_STRINGS; i++)
tmp_array_of_strings[i] = static_array_of_strings[i];

fun (tmp_array_of_strings, NUM_STRINGS);

}

return 0;
}

Since fun() doesn't need to modify the (char*) pointers themselves,
this works. But is there a more elegant way of doing this?

Mack

Nov 14 '05 #2
MackS wrote:
I've come across the following difficulty, related to questions 6.12,
6.13 and 6.18 in the FAQ, which I am unable to overcome:

void fun(char **array_of_strings, int num_elements);
This is the same as

void fun(char* array_of_strings[], int num_elements);
int main(int argc, char* argv[]) {

char static_array_of_strings[NUM_STRINGS][MAX_STRING_LEN+1];

fun((char**)&static_array_of_strings, NUM_STRINGS);
This should be something like

char* static_array_of_strings = {"s1", "s2", "s3"};
fun(static_array_of_strings, 3);
return 0;
}

This code compiles but is wrong (I get a segmentation fault).
How can I correctly call fun on static_array_of_strings?

I can't modify the prototype of fun() because it also receives "true"
char** (in the sense of dynamically allocated arrays of strings, ie,
both the number of elements in the array
as well as the length of each string
are unknown size at compile time).

How can I pass static_array_of_strings to it?
The way I read FAQ 6.18 suggests this is impossible
and the prototype of fun() would have to modified
to include MAX_STRING_LEN+1.


You can write

char* array_of_strings
= (char*)malloc(NUM_STRINGS*sizeof(char*));
for (size_t i = 0; i < NUM_STRINGS; ++i)
array_of_strings[i] = &(static_array_of_strings[i][0]);
fun(array_of_strings, NUM_STRINGS);
free((void*)array_of_strings);

assuming that each array of char in static_array_of_strings
contains at least one '\0'.

If you have a C99 compiler, you could write a wrapper function:

void
fun2(size_t strings, size_t length,
char* static_array_of_strings[strings][length]) {
char* array_of_strings[strings];
for (size_t i = 0; i < strings; ++i)
array_of_strings[i] = &(static_array_of_strings[i][0]);
fun(array_of_strings, strings);
}

using variable size arrays.
Nov 14 '05 #3
"MackS" <ma***********@hotmail.com> writes:
I've come across the following difficulty, related to questions 6.12,
6.13 and 6.18 in the FAQ, which I am unable to overcome:

void fun(char **array_of_strings, int num_elements);

int main(void)
{
char static_array_of_strings[NUM_STRINGS][MAX_STRING_LEN+1];

fun ((char**)&static_array_of_strings, NUM_STRINGS);

return 0;
}

This code compiles but is wrong (I get a segmentation fault). How can I
correctly call fun on static_array_of_strings?


fun() expects a pointer to an array of pointers.

static_array_of_strings is an array of array of characters; it
contains no pointers.

(Incidentally, the name "static_array_of_strings" is a bit misleading,
since it isn't static in any of the senses that C uses the term.)

If you want an array of pointers for fun() to chew on, you're going to
have to build it yourself. The workaround you describe in your
followup is actually a good solution.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
[...]
char* array_of_strings
= (char*)malloc(NUM_STRINGS*sizeof(char*));
char *array_of_strings = malloc(NUM_STRINGS * sizeof *array_of_strings);

[...] free((void*)array_of_strings);
free(array_of_strings);

The cast is useless.

[...] If you have a C99 compiler, you could write a wrapper function:

[...]

And if you don't, much of ERT's other code (which I haven't quoted)
won't compile (unless your compiler implements some C99 features as
extensions).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Keith Thompson wrote:
E. Robert Tisdale writes:
free((void*)array_of_strings);
free(array_of_strings);

The cast is useless.

cat main.c #include <stdlib.h>

int main(int argc, char* argv[]) {
const
char* p =(const char*)malloc(13);
free(p);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c

main.c: In function `main':
main.c:6: warning: passing arg 1 of `free' \
discards qualifiers from pointer target type
If you have a C99 compiler,


And if you don't,


get one.

Nov 14 '05 #6
MackS wrote:

I've come across the following difficulty, related to questions
6.12, 6.13 and 6.18 in the FAQ, which I am unable to overcome:

void fun(char **array_of_strings, int num_elements);

int main(void)
{
char static_array_of_strings[NUM_STRINGS][MAX_STRING_LEN+1];

fun ((char**)&static_array_of_strings, NUM_STRINGS);
return 0;
}

This code compiles but is wrong (I get a segmentation fault).
How can I correctly call fun on static_array_of_strings?
.... snip ...
How can I pass static_array_of_strings to it? The way I read FAQ
6.18 suggests this is impossible and the prototype of fun() would
have to modified to include MAX_STRING_LEN+1.


You can't. Fun is expecting a pointer to a pointer to a char. I
see no pointers in static_array_of_strings. (henceforth to be known
as sas)

If you don't need to modify the strings, you could use:

static const char *sas[] = {"string1",
"string2",
"string3",
"laststring"};

and call with fun(&sas, sizeof(sas)/sizeof(sas[0]));

If you do need to modify the strings, create the modifiable strings
and then gather pointers to them in sas.

char string1[MAX_STR_LEN] = "string1";
char string2[MAX_STR_LEN] = "string2";
char string3[MAX_STR_LEN] = "string3";
char laststring[MAX_STR_LEN] = "laststring";

char *sas[] = {&string1, &string2, &string3, &laststring};

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #7
CBFalconer wrote:
char string1[MAX_STR_LEN] = "string1";
char string2[MAX_STR_LEN] = "string2";
char string3[MAX_STR_LEN] = "string3";
char laststring[MAX_STR_LEN] = "laststring";

char *sas[] = {&string1, &string2, &string3, &laststring};


The address operators in the last initializer seem wrong to me. They
indicate that we have an array of pointers to arrays, which is not
compatible with the left hand side. I'm curious, though: If we wanted to
leave the right hand side as it is, what would the lhs need to look like? I
can't seem to figure it out.
Christian
Nov 14 '05 #8
Christian Kandeler wrote:
char *sas[] = {&string1, &string2, &string3, &laststring};

[ ... ]
If we wanted to leave the right hand side as it is, what would the lhs
need to look like? I can't seem to figure it out.


Okay, I remembered there's cdecl. It looks like this:
char (*sas[])[MAX_STR_LEN]
According to the precedence rules, this makes perfect sense, but looking at
it, I never would have guessed that the outer brackets refer to the inner
arrays. I hope I'll never have to use such a construct.
Christian
Nov 14 '05 #9
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
Keith Thompson wrote:
E. Robert Tisdale writes:
free((void*)array_of_strings);

free(array_of_strings);
The cast is useless.

> cat main.c

#include <stdlib.h>

int main(int argc, char* argv[]) {
const
char* p =(const char*)malloc(13);
free(p);
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c

main.c: In function `main':
main.c:6: warning: passing arg 1 of `free' \
discards qualifiers from pointer target type


So what? There was no const in the previous code; the cast in your
free((void*)array_of_strings);
is indeed useless.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10
Keith Thompson wrote:
The cast in your

free((void*)array_of_strings);

is indeed useless.


So what?
It doesn't hurt. It always works.
And I don't need to consider
whether array_of_strings points to a const or not.
Nov 14 '05 #11
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
Keith Thompson wrote:
The cast in your

free((void*)array_of_strings);

is indeed useless.


So what?
It doesn't hurt.


_All_ superfluous casts hurt. Even if they do not hurt the compiler,
they hurt the programmer and the maintainer.

Richard
Nov 14 '05 #12

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
5
by: Oeleboele | last post by:
OK, I can't understand this exactly: I have this function: int howdy(void *) I first past this to the function &aCharArray I realized later that this should be aCharArray but... the former...
7
by: Jeff K | last post by:
Can you pass an int array by reference to a function and modify selective elements? Here is my code: #include <stdio.h> #define COLUMNSIZE 30 #define ASIZE 5...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
3
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/...
11
by: Bob Yang | last post by:
Hi, I have this in C++ and I like to call it from c# to get the value but I fail. it will be good if you can give me some information. I tried it in VB.net it works but I use almost the same way as...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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...
0
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...
0
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...

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.