one question about string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omryk
    New Member
    • Oct 2008
    • 7

    one question about string

    I want to write function that take array of strings and return to me one string that contain all of the words i passed to this function
    i have problem with how to pass to function (prototype)

    here is the main :

    void main()
    {
    char *words[]={{"a"},{"pictu re"},{"is"},{"w orth"},{"a"},{" thousand"},{"wo rds"}};


    char *word=sentence( words);
    puts(word);

    }

    and the prototpye of sentence function is like this

    char *sentence(char ** words);

    is it ok? or wrong and how can i get the first word
    is it *words[i] ?
    best regards
  • curiously enough
    New Member
    • Aug 2008
    • 79

    #2
    You CANNOT make a function return a string or or any array. What you can do is pass a one dimensional array of charachters as another parameter to the function and alter it inside this function, and make the return type void( void sentence(char ** words,char *word) ). But there isn't anything else wrong.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      1. Function sentence has no way of inferring the dimension of the passed array. That's why, for instance, the standard prototype for main has the argc argument.

      2. You're declaring variable word with an initializer, but the value of the initializer isn't known until run-time. Don't know about C++, but you can't do that in C. Separate the declaration from the assignment.

      3. Realize that if you declare word as a char* then it will only be a pointer. That leaves it up to somebody else (sentence?) to allocate the memory needed to hold the concatenation of all the strings in the words array.

      Comment

      • omryk
        New Member
        • Oct 2008
        • 7

        #4
        tnx but is it the same solution as u offered in C and C++ or maybe in C++ i can do something else?

        Comment

        Working...