Receving an array as parameter

Discussion in 'C Programming' started by metz, Apr 26, 2005.

  1. metz

    metz Guest

    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.
    metz, Apr 26, 2005
    #1
    1. Advertising

  2. metz

    Eric Sosman Guest

    metz wrote:
    > 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

    --
    Eric Sosman, Apr 26, 2005
    #2
    1. Advertising

  3. metz

    Mike Wahler Guest

    "metz" <metz@mtzcom> wrote in message
    news:426e941b$0$6622$...
    > 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
    Mike Wahler, Apr 26, 2005
    #3
  4. Mike Wahler wrote:

    > 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
    E. Robert Tisdale, Apr 26, 2005
    #4
  5. metz

    metz Guest

    "Eric Sosman" <> wrote in message
    news:d4m4tu$nk0$...


    > 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...
    metz, Apr 27, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    499
    Paul Lesnikowski
    Nov 10, 2005
  2. h-h

    Receving usercontrol and page

    h-h, Dec 25, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    345
    =?Utf-8?B?QWFyb24gR3Vv?=
    Dec 26, 2005
  3. Replies:
    4
    Views:
    517
    Matteo
    Mar 29, 2007
  4. Stuart Redmann
    Replies:
    5
    Views:
    476
    Stuart Redmann
    Dec 14, 2007
  5. Colin
    Replies:
    0
    Views:
    130
    Colin
    Dec 1, 2003
Loading...

Share This Page