Receving an array as parameter

M

metz

Hello,

I would want to pass a string array as a receive parameter of a function.
The function must fill the array with a given number of strings.
Something like that :

foo(*sArray[], nNbValues)

And I should get : sArray[0] = "foo0", sArray[1] = "foo1", sArray[2] =
"foo2", ... sArray[nNbValues-1] = "foon"...

Is it possible or must I use a structure ?

Thanks in advance.
 
E

Eric Sosman

metz said:
Hello,

I would want to pass a string array as a receive parameter of a function.
The function must fill the array with a given number of strings.
Something like that :

foo(*sArray[], nNbValues)

And I should get : sArray[0] = "foo0", sArray[1] = "foo1", sArray[2] =
"foo2", ... sArray[nNbValues-1] = "foon"...

Is it possible or must I use a structure ?

void foo(char *sptr[], int nNbValues) {
char buff[50]; /* there are safer ways to write "50" */
while (--nNbValues >= 0) {
sprintf (buff, "foo%d", nNbValues);
sptr[nNbValues] = malloc(strlen(buff) + 1);
if (sptr[nNbValues] == NULL)
die_horribly();
strcpy (sptr[nNbValues], buff);
}
}

If any of this is the slightest bit confusing, I'd suggest
reading Section 6 of the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
 
M

Mike Wahler

metz said:
Hello,

I would want to pass a string array as a receive parameter of a function.
The function must fill the array with a given number of strings.
Something like that :

foo(*sArray[], nNbValues)

And I should get : sArray[0] = "foo0", sArray[1] = "foo1", sArray[2] =
"foo2", ... sArray[nNbValues-1] = "foon"...

Is it possible
Certainly.

or must I use a structure ?

No need.

Just make sure that the pointers in your array point to
sufficient memory to store your strings, or only assign
the addresses of string literals to them.

void foo(char *array[], size_t count)
{
if(count >= 3)
{
array[0] = "Tom";
array[1] = "Dick";
array[2] = "Harry";
}
}

int main()
{
char *arr[] = {0,0,0};
foo(arr, sizeof arr / sizeof *arr);
return 0;
}

-Mike
 
E

E. Robert Tisdale

Mike said:
void foo(char *array[], size_t count)
{
if(count >= 3)
{
array[0] = "Tom";
array[1] = "Dick";
array[2] = "Harry";
}
}

int main()
{
char *arr[] = {0,0,0};
foo(arr, sizeof arr / sizeof *arr);
return 0;
}
> cat main.c
#include <stdio.h>
#include <stdlib.h>

const char** foo(const char *array[], const size_t count) {
if (0 < count) {
array[0] = "Tom";
if (1 < count) {
array[1] = "Dick";
if (2 < count) {
array[2] = "Harry";
}
}
}
return array;
}

int main(int argc, char* argv[]) {
const
char* arr[] = {0, 0, 0};
const
size_t names = sizeof(arr)/sizeof(*arr);
foo(arr, names);
fprintf(stdout, "names = %u\n", names);
for (size_t name = 0; name < names; ++name)
fprintf(stdout, "arr[%u] = %s\n", name, arr[name]);
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main
names = 3
arr[0] = Tom
arr[1] = Dick
arr[2] = Harry
 
M

metz

sptr[nNbValues] = malloc(strlen(buff) + 1);

Thanks to all.
If fact the problem was that my strings in the function are receive
parameters of specific functions.
Then I couldn't use "array[0] = "xxx";
It works now with malloc() then strcpy()
(I just did't understand why this is not important to use malloc() without
free().. but I read 7.24 section that says the OS should do it....)

Thnaks again...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top