Thread: returning a char array

  1. #1
    Registered User OxYgEn-22's Avatar
    Join Date
    Apr 2002
    Posts
    36

    returning a char array

    Heya all, i was having a few problems passing a char array back to my program.. is this the way the definition is suposed to look like?

    Code:
    char **Addtxt(char box**)
    {
    	return box;
    }
    because this does not compile.. also box is declaired like so..

    box[11][11];
    Is that air you're breathing?

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    What is the reason for the double indirection operator after the variable name?
    Code:
    (char box**)

  3. #3
    Registered User OxYgEn-22's Avatar
    Join Date
    Apr 2002
    Posts
    36
    is that not just another way to say?
    Code:
    box[][]
    Is that air you're breathing?

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Not to my knowledge.

    The indirection operator is used like this:
    Code:
    int i1; 
    int *i2; //declares i2 as a pointer to an int
    i1=6;
    i2=&i1;
    *i2=5;  //since i2 is a pointer, the * in this case dereferences it and assigns 5 as the value held at that memory address.
    printf("%d",*i2);
    I've never heard of using * to represent elements of an array.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    presumably box will be manipulated in some way in Addtxt(), so you want the changes to be available in the function that called Addtxt(). You might be happy to know that just the act of sending the array as an argument to Addtxt() allows all that to happen and that you don't need to return anythingl.

    void Addtxt(char ** box)

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Write instead
    Code:
    char** box

    But you should consider using std::string in <string>

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you have

    box[11][11];

    The correct prototype is
    void Addtxt(char box[11][11]);

    Which you declare as
    void Addtxt(char box[11][11]) {
    // your code
    }


    Which you call as
    Addtxt( box );

    And when you return from the function, the array will have been updated by the Addtxt() function

















    PS
    You can also write
    void Addtxt(char box[][11]);
    void Addtxt(char (*box)[11]);
    All three variations mean the same thing, but the first is a simple copy/paste

  8. #8
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    wait, let me get this straight. you have a global function that is outside main() and you want it to edit an array? I don't get what you're trying to do there but here are my solutions:

    solution 1:
    make the array global so it can be accessed by all functions.

    solution 2:
    make the variable reference ( if it's inside main() ) and also a pointer (since it is an array).
    can this help?
    void function_name ( &*array_variable_name )
    {
    //add your code here
    }
    think only with code.
    write only with source.

  9. #9
    Registered User OxYgEn-22's Avatar
    Join Date
    Apr 2002
    Posts
    36
    thanks guys for all your help here ,

    so you want the changes to be available in the function that called Addtxt(). You might be happy to know that just the act of sending the array as an argument to Addtxt() allows all that to happen and that you don't need to return anythingl.
    I did not know about this, thanks for the info!!

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    63

    Just a thought

    This is just a thought:

    char **Addtxt(char box**)
    {
    return box;
    }

    My understanding of this function is to add some text to a character array, which is in theory a character pointer, is it not?

    Therefore, since I was doing something similar recently I have a solution which might come in handy later on.

    Here is my solution:
    Open the attachment and compile, you shall see
    It adds the box to itself.

    I think that is what you want the program to do, except maybe add other stuff to box, and not box to itself.

    I hope that this will help you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  2. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  3. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM